Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix data race in compile_template in :file:`sre.c`.
10 changes: 9 additions & 1 deletion Modules/_sre/sre.c
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
Loading