https://github.com/python/cpython/commit/3cf68cdd3e1809df4e426c61f6990de63747ec6f
commit: 3cf68cdd3e1809df4e426c61f6990de63747ec6f
branch: main
author: Tomasz Pytel <[email protected]>
committer: kumaraditya303 <[email protected]>
date: 2025-02-12T18:34:44+05:30
summary:

gh-129983: fix data race in compile_template in sre.c (#130015)

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2025-02-11-20-38-37.gh-issue-129983._1Fujo.rst
M Modules/_sre/sre.c

diff --git 
a/Misc/NEWS.d/next/Core_and_Builtins/2025-02-11-20-38-37.gh-issue-129983._1Fujo.rst
 
b/Misc/NEWS.d/next/Core_and_Builtins/2025-02-11-20-38-37.gh-issue-129983._1Fujo.rst
new file mode 100644
index 00000000000000..9b435703eb734a
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2025-02-11-20-38-37.gh-issue-129983._1Fujo.rst
@@ -0,0 +1 @@
+Fix data race in compile_template in :file:`sre.c`.
diff --git a/Modules/_sre/sre.c b/Modules/_sre/sre.c
index 0d8d4843d33c1b..54f0e64d4819f1 100644
--- a/Modules/_sre/sre.c
+++ b/Modules/_sre/sre.c
@@ -1167,13 +1167,21 @@ compile_template(_sremodulestate *module_state,
                  PatternObject *pattern, PyObject *template)
 {
     /* delegate to Python code */
-    PyObject *func = module_state->compile_template;
+    PyObject *func = FT_ATOMIC_LOAD_PTR(module_state->compile_template);
     if (func == NULL) {
         func = PyImport_ImportModuleAttrString("re", "_compile_template");
         if (func == NULL) {
             return NULL;
         }
+#ifdef Py_GIL_DISABLED
+        PyObject *other_func = NULL;
+        if (!_Py_atomic_compare_exchange_ptr(&module_state->compile_template, 
&other_func, func))  {
+            Py_DECREF(func);
+            func = other_func;
+        }
+#else
         Py_XSETREF(module_state->compile_template, func);
+#endif
     }
 
     PyObject *args[] = {(PyObject *)pattern, template};

_______________________________________________
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]

Reply via email to