gh-154044: Fix data race on descriptor __qualname__ in free-threading build (#154691)

kumaraditya303 <[email protected]> Tue, 04 Aug 2026 05:26:25 -0400 (EDT)
Newsgroups gmane.comp.python.cvs
Message-ID <[email protected]>
https://github.com/python/cpython/commit/2b91da7cd4114e7b264ab4a7b7a1c42b79663a6c
commit: 2b91da7cd4114e7b264ab4a7b7a1c42b79663a6c
branch: main
author: Timofei Ivankov <[email protected]>
committer: kumaraditya303 <[email protected]>
date: 2026-08-04T14:54:02+05:30
summary:

gh-154044: Fix data race on descriptor __qualname__ in free-threading build (#154691)

files:
A Misc/NEWS.d/next/Core_and_Builtins/2026-07-25-12-44-13.gh-issue-154044.GBa2wP.rst
M Objects/descrobject.c

diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-07-25-12-44-13.gh-issue-154044.GBa2wP.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-07-25-12-44-13.gh-issue-154044.GBa2wP.rst
new file mode 100644
index 00000000000000..7338ae0210450a
--- /dev/null
+++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-07-25-12-44-13.gh-issue-154044.GBa2wP.rst
@@ -0,0 +1,2 @@
+Fix a data race on a descriptor's ``__qualname__`` cache in the
+:term:`free-threaded build`.
diff --git a/Objects/descrobject.c b/Objects/descrobject.c
index 568d978d27d12f..8ceef648988167 100644
--- a/Objects/descrobject.c
+++ b/Objects/descrobject.c
@@ -621,9 +621,17 @@ static PyObject *
 descr_get_qualname(PyObject *self, void *Py_UNUSED(ignored))
 {
     PyDescrObject *descr = (PyDescrObject *)self;
-    if (descr->d_qualname == NULL)
-        descr->d_qualname = calculate_qualname(descr);
-    return Py_XNewRef(descr->d_qualname);
+    PyObject *qualname;
+    Py_BEGIN_CRITICAL_SECTION(self);
+    if (descr->d_qualname == NULL) {
+        PyObject *new_qualname = calculate_qualname(descr);
+        if (new_qualname != NULL) {
+            Py_XSETREF(descr->d_qualname, new_qualname);
+        }
+    }
+    qualname = Py_XNewRef(descr->d_qualname);
+    Py_END_CRITICAL_SECTION();
+    return qualname;
 }
 
 static PyObject *

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]