Skip to content

Commit c8729c9

Browse files
authored
gh-140257: fix data race on eval_breaker during finalization (#140265)
1 parent 78e1d65 commit c8729c9

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
@@ -763,10 +763,11 @@ interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate)
763763

764764
Py_CLEAR(interp->audit_hooks);
765765

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

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

0 commit comments

Comments
 (0)