Skip to content

Commit f864c6c

Browse files
ashm-devmiss-islington
authored andcommitted
pythongh-140257: fix data race on eval_breaker during finalization (pythonGH-140265)
(cherry picked from commit c8729c9) Co-authored-by: Shamil <[email protected]>
1 parent b2d3957 commit f864c6c

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix data race between interpreter_clear() and take_gil() on eval_breaker
2+
during finalization with daemon threads.

Python/pystate.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -860,10 +860,11 @@ interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate)
860860

861861
Py_CLEAR(interp->audit_hooks);
862862

863-
// At this time, all the threads should be cleared so we don't need atomic
864-
// operations for instrumentation_version or eval_breaker.
863+
// gh-140257: Threads have already been cleared, but daemon threads may
864+
// still access eval_breaker atomically via take_gil() right before they
865+
// hang. Use an atomic store to prevent data races during finalization.
865866
interp->ceval.instrumentation_version = 0;
866-
tstate->eval_breaker = 0;
867+
_Py_atomic_store_uintptr(&tstate->eval_breaker, 0);
867868

868869
for (int i = 0; i < _PY_MONITORING_UNGROUPED_EVENTS; i++) {
869870
interp->monitors.tools[i] = 0;

0 commit comments

Comments
 (0)