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 |
|
This also fixes the
Is this still the case? Only
If this can't be backported, can we do something for those branches? Maybe cut the number of iterations in the test, like @cmaloney suggested in #162364? |
No, that statement is no longer true (should be easy backport now). On Sunday I got Claude to build a TLA+ version of the new STW logic, to give me more confidence this doesn't introduce deadlocks or starvation issues. That checks out. |
|
re: |
In the free-threaded build, a stop-the-world (STW) request can wait for the 1 ms fallback rescan even though the thread it is waiting for has already detached. This happens when a thread detaches to block on a lock or around a C call without first handling the stop request at an eval-breaker check.
Implementation
DETACHED,_PyThreadState_Detach()checks the thread’s own_PY_EVAL_PLEASE_STOP_BIT. If it is set, the slow path takesHEAD_LOCK, checks for an active STW request, and decrements the countdown only if it successfully transitions the thread fromDETACHEDtoSUSPENDED. The common path does not read shared STW flags or takeHEAD_LOCK.ATTACHEDorDETACHED_WAITING, it sets the please-stop bit and makes one more attempt to park it. That second attempt covers the race where the thread detached and checked the bit before the requester set it.DETACHED_WAITINGthreads are still allowed to finish attaching so consecutive pauses cannot starve them; the bit asks them to stop at their next eval-breaker check (or when they detach again).The 1 ms rescan remains as a fallback. The current patch does not change the runtime or interpreter structure layout, so the original backport caveat no longer applies.
Benchmarks
Updated measurements use a free-threaded release build on Linux x86-64 (Clang 20.1.8,
-O3, no PGO, i7-14700K).Five interleaved runs of
test_opcache.test_binary_subscr_getitemandtest_opcache.test_load_attr_getattribute_overridden. A pause counts here only if it triggers at least one 1 ms fallback rescan, not simply if it lasts longer than 1 ms.mainIn C loops pinned to one P-core, the median paired overhead versus
mainwas +0.23 ns (+1.4%) for a barePyEval_SaveThread()/PyEval_RestoreThread()pair and +0.12 ns (+0.8%) for aPy_BEGIN_ALLOW_THREADSblock around a trivial C call. CI measurements also show that the patch resolves the Windows free-threaded debug timeouts intest_unregister_lingering_tasks_of_exited_thread.An LLM assisted in writing and benchmarking this PR.
Original approach (for context on earlier review comments)
The first revision atomically read the shared runtime and interpreter
stoptheworld.requestedflags after detaching. The requester set its flag before scanning threads, so a thread that detached during the scan could see the request even if it had not yet been signalled. That approach changedrequestedfrombooltointand added shared loads to every detach. Earlier comments about a please-stop-bit check missing the race refer to a version without the requester’s second parking attempt. The current version uses that retry instead of reading the shared flags on the fast path.