Skip to content

Commit 4cb251d

Browse files
[3.13] gh-129983: fix data race in compile_template in sre.c (#130038)
gh-129983: fix data race in compile_template in sre.c (#130015) (cherry picked from commit 3cf68cd) Co-authored-by: Tomasz Pytel <[email protected]>
1 parent fbe18bd commit 4cb251d

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
@@ -1160,13 +1160,21 @@ compile_template(_sremodulestate *module_state,
11601160
PatternObject *pattern, PyObject *template)
11611161
{
11621162
/* delegate to Python code */
1163-
PyObject *func = module_state->compile_template;
1163+
PyObject *func = FT_ATOMIC_LOAD_PTR(module_state->compile_template);
11641164
if (func == NULL) {
11651165
func = _PyImport_GetModuleAttrString("re", "_compile_template");
11661166
if (func == NULL) {
11671167
return NULL;
11681168
}
1169+
#ifdef Py_GIL_DISABLED
1170+
PyObject *other_func = NULL;
1171+
if (!_Py_atomic_compare_exchange_ptr(&module_state->compile_template, &other_func, func)) {
1172+
Py_DECREF(func);
1173+
func = other_func;
1174+
}
1175+
#else
11691176
Py_XSETREF(module_state->compile_template, func);
1177+
#endif
11701178
}
11711179

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

0 commit comments

Comments
 (0)