Skip to content

Commit 3cf68cd

Browse files
authored
pythongh-129983: fix data race in compile_template in sre.c (python#130015)
1 parent 469d2e4 commit 3cf68cd

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix data race in compile_template in :file:`sre.c`.

Modules/_sre/sre.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1167,13 +1167,21 @@ compile_template(_sremodulestate *module_state,
11671167
PatternObject *pattern, PyObject *template)
11681168
{
11691169
/* delegate to Python code */
1170-
PyObject *func = module_state->compile_template;
1170+
PyObject *func = FT_ATOMIC_LOAD_PTR(module_state->compile_template);
11711171
if (func == NULL) {
11721172
func = PyImport_ImportModuleAttrString("re", "_compile_template");
11731173
if (func == NULL) {
11741174
return NULL;
11751175
}
1176+
#ifdef Py_GIL_DISABLED
1177+
PyObject *other_func = NULL;
1178+
if (!_Py_atomic_compare_exchange_ptr(&module_state->compile_template, &other_func, func)) {
1179+
Py_DECREF(func);
1180+
func = other_func;
1181+
}
1182+
#else
11761183
Py_XSETREF(module_state->compile_template, func);
1184+
#endif
11771185
}
11781186

11791187
PyObject *args[] = {(PyObject *)pattern, template};

0 commit comments

Comments
 (0)