Repository navigation
Conversation
When detaching, check if STW is requested. If so, move from DETACHED to SUSPENDED and decrement countdown. Also, threads waiting to re-attach after a previous pause are asked to stop.
|
CC @colesbury |
| _PyRuntimeState *runtime = &_PyRuntime; | ||
| PyInterpreterState *interp = tstate->interp; | ||
|
|
||
| if (!_Py_atomic_load_int(&runtime->stoptheworld.requested) && |
There was a problem hiding this comment.
Can we just check the _PY_EVAL_PLEASE_STOP_BIT. on this threads eval-breaker?
Would this work:
void
_PyThreadState_Detach(PyThreadState *tstate)
{
if (_Py_eval_breaker_bit_is_set(tstate, _PY_EVAL_PLEASE_STOP_BIT) {
_PyThreadState_Suspend(tstate);
}
else {
detach_thread(tstate, _Py_THREAD_DETACHED);
}
}There was a problem hiding this comment.
That helps but doesn't always avoid the 1 ms delay. The requester can see the thread as ATTACHED, then the thread can check the bit and detach before the requester sets it. Checking the bit after detaching doesn’t fix this either: the requester can still set it after that check, based on its earlier ATTACHED read. The requested flag works because the requester sets it before scanning thread states. If the scan sees ATTACHED, the request is already visible to the detaching thread’s post-detach check.
Benchmarking: the please-stop bit check still hit the retry about once every 360 pauses, in exchange for saving about 0.2 ns per detach/reattach pair on this machine.
There was a problem hiding this comment.
I think we're always going to have a race here, right?
There was a problem hiding this comment.
I think it's important that detach and attach remain as inexpensive as possible. There are a lot of Py_BEGIN_ALLOW_THREADS type things around really simple around really simple functions, especially in things like PyTorch. I'm not sure ctypes captures that well because you have all the overhead of ctypes.
There was a problem hiding this comment.
Using Hugo's bug with test_opcache slowdown (test_binary_subscr_getitem + test_load_attr_getattribute_overridden, 22,545 pauses):
| variant | wall | retried pauses |
|---|---|---|
| base | 18.18 s | 73.6% |
| please-stop (bit) | 0.89 s (0.74–0.94) | 1.5–2.2% |
| requested (flag) | 0.21 s | 0.01% |
I measured detach/attach cost directly in C, with no ctypes in the loop. For a PyTorch type pattern, checking requested costs +0.13 ns per detach/attach pair (+0.8%), or +0.14 ns on a 14.85 ns detaching call. The trade is about 0.13 ns per detach vs 1 ms per missed pause. So I lean to the requested check as the fix.
However, checking please-stop is simpler and does help. So, if you prefer that, I'll revise the PR.
There was a problem hiding this comment.
I figured out how to use the please-stop bit, getting the low % of "retried pauses". It's a more code change vs your suggestion but it avoids extra cost for detach/attach and also avoids the 1 ms pause nearly always.
Replace the check of the `requested` flags in _PyThreadState_Detach() with a check of the thread's own _PY_EVAL_PLEASE_STOP_BIT. This avoids reading the shared runtime and interpreter stop-the-world state on every detach.
|
Re-done benchmarks, for e49b99c version. Done with free-threaded release build ( 1 ms STW fallbackFive interleaved runs of the two
Detach/attach costC loops pinned to one P-core, eight interleaved runs per variant. Times are ns per detach/attach pair.
The median paired |
When detaching, check if STW is requested. If so, switch to
SUSPENDEDstate and then decrement the STW countdown. Also,park_detached_threads()now sets the please-stop bit on threads in theDETACHED_WAITINGstate, telling them to stop immediately. Otherwise they can start executing Python code again when waking.Note: this change cannot be backported as is since it changes the interp structure (
requestedmember goes from bool to int). Also, note that an LLM assisted in writing and benchmarking this PR.Benchmarks
Stop-the-world latency
The reproducer from the issue calls
_testinternalcapi.stop_the_world()1000 times while worker threads run in different modes. Free-threaded debug build, Linux x86-64, us per stop-the-world:time.sleep(0)loopthreading.LockThe first change fixes the
sleepmode. The second change fixesspinandlock: there the straggler was a thread that had beenDETACHED_WAITINGwhen the pause started, and gdb showed it running Python code while the requester waited inPyEvent_WaitTimed(). Thenonecolumn is within noise; it does not touch the changed code.Detach overhead
The new check adds two loads and two branches to every
_PyThreadState_Detach(). On x86-64 the seq_cst loads are plainmovs;HEAD_LOCKand the CAS are only reached while a stop is pending.Measured with a ctypes call to libc
abs():ctypes.CDLLcalls detach and re-attach around the C call,ctypes.PyDLLcalls do not, and the difference is the detach/attach cost. Release free-threaded build, pinned to one CPU, best of 15 runs of 200,000 calls, 4 alternating runs per build:CDLLcallPyDLLcallThat is about 1.5 ns per detach/attach pair, roughly 1% of the cheapest detaching call measured, and close to the noise.
requestedis only written when a pause starts or ends, so between pauses its cache line stays shared and there is no contention between threads.Benchmark script