Race condition in make_pending_calls
in free-threaded build
#122201
Labels
3.13
bugs and security fixes
3.14
new features, bugs and security fixes
topic-free-threading
type-bug
An unexpected behavior, bug, or error
Bug report
make_pending_calls
uses a mutex and the thehandling_thread
field to ensure that only one thread per-interpreter is handling pending calls at a time:cpython/Python/ceval_gil.c
Lines 911 to 928 in 41a91bd
However, the clearing of
handling_thread
is done outside of the mutex:cpython/Python/ceval_gil.c
Lines 959 to 960 in 41a91bd
There are two problems with this (for the free-threaded build):
handling_thread
(in the mutex) concurrently with a write (outside the mutex)_PY_CALLS_TO_DO_BIT
onpending->handling_thread
is subject to a time-of-check to time-of-use hazard:pending->handling_thread
may be non-NULL when evaluating the if-statement, but then cleared before setting the eval breaker bit.Relevant unit test
TSan catches this race when running
test_subthreads_can_handle_pending_calls
fromtest_capi.test_misc
.Suggested Fix
We should set
pending->handling_thread = NULL;
while holdingpending->mutex
, at least in the free-threaded buildLinked PRs
The text was updated successfully, but these errors were encountered: