Skip to content

Commit ce50df4

Browse files
authored
gh-162764: Avoid 1 ms STW delay when a thread detaches (gh-162771)
Reduce the latency of stop-the-world pauses in the free-threaded build. Threads that detach (for example, to block on a lock or around a blocking call) while a pause is pending now notify the requesting thread, and threads waiting to re-attach after a previous pause are asked to stop. Previously, such pauses could be delayed by about one millisecond.
1 parent d0a6d4d commit ce50df4

2 files changed

Lines changed: 79 additions & 12 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Reduce the latency of stop-the-world pauses in the :term:`free-threaded build`.
2+
Threads that detach (for example, to block on a lock or around a blocking
3+
call) while a pause is pending now notify the requesting thread, and threads
4+
waiting to re-attach after a previous pause are asked to stop. Previously,
5+
such pauses could be delayed by about one millisecond.

‎Python/pystate.c‎

Lines changed: 74 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2457,10 +2457,56 @@ detach_thread(PyThreadState *tstate, int detached_state)
24572457
_PyEval_ReleaseLock(tstate->interp, tstate, 0);
24582458
}
24592459

2460+
#ifdef Py_GIL_DISABLED
2461+
static void
2462+
stop_after_detach(PyThreadState *tstate)
2463+
{
2464+
_PyRuntimeState *runtime = &_PyRuntime;
2465+
PyInterpreterState *interp = tstate->interp;
2466+
2467+
HEAD_LOCK(runtime);
2468+
struct _stoptheworld_state *stw = NULL;
2469+
if (runtime->stoptheworld.requested) {
2470+
stw = &runtime->stoptheworld;
2471+
}
2472+
else if (interp->stoptheworld.requested) {
2473+
stw = &interp->stoptheworld;
2474+
}
2475+
if (stw != NULL && stw->requester != tstate) {
2476+
int expected = _Py_THREAD_DETACHED;
2477+
if (!_Py_atomic_compare_exchange_int(&tstate->state, &expected,
2478+
_Py_THREAD_SUSPENDED)) {
2479+
// BRC may have suspended this thread without counting it for STW.
2480+
// Leave the stop bit set so it sees the request when it reattaches.
2481+
HEAD_UNLOCK(runtime);
2482+
return;
2483+
}
2484+
decrement_stoptheworld_countdown(stw);
2485+
}
2486+
// The request was handled or the bit was stale. Requests set the bit
2487+
// while holding HEAD_LOCK, so clearing it here cannot lose one.
2488+
_Py_unset_eval_breaker_bit(tstate, _PY_EVAL_PLEASE_STOP_BIT);
2489+
#if defined(Py_REMOTE_DEBUG) && defined(Py_SUPPORTS_REMOTE_DEBUG)
2490+
if (tstate->remote_debugger_support.debugger_pending_call) {
2491+
// A remote debugger also sets the bit, to get the thread to call
2492+
// _Py_HandlePending().
2493+
_Py_set_eval_breaker_bit(tstate, _PY_EVAL_PLEASE_STOP_BIT);
2494+
}
2495+
#endif
2496+
HEAD_UNLOCK(runtime);
2497+
}
2498+
#endif
2499+
24602500
void
24612501
_PyThreadState_Detach(PyThreadState *tstate)
24622502
{
24632503
detach_thread(tstate, _Py_THREAD_DETACHED);
2504+
#ifdef Py_GIL_DISABLED
2505+
uintptr_t breaker = _Py_atomic_load_uintptr(&tstate->eval_breaker);
2506+
if ((breaker & _PY_EVAL_PLEASE_STOP_BIT) != 0) {
2507+
stop_after_detach(tstate);
2508+
}
2509+
#endif
24642510
}
24652511

24662512
void
@@ -2572,6 +2618,32 @@ interp_for_stop_the_world(struct _stoptheworld_state *stw)
25722618
i != NULL; i = ((stw->is_global) ? i->next : NULL))
25732619

25742620

2621+
static bool
2622+
park_or_ask_to_stop(struct _stoptheworld_state *stw, PyThreadState *t)
2623+
{
2624+
if (t == stw->requester) {
2625+
return false;
2626+
}
2627+
int state = _Py_THREAD_DETACHED;
2628+
if (_Py_atomic_compare_exchange_int(&t->state, &state,
2629+
_Py_THREAD_SUSPENDED)) {
2630+
return true;
2631+
}
2632+
if (state != _Py_THREAD_ATTACHED && state != _Py_THREAD_DETACHED_WAITING) {
2633+
return false;
2634+
}
2635+
2636+
// Let a DETACHED_WAITING thread attach before stopping it, so repeated
2637+
// pauses cannot starve its attach attempt.
2638+
_Py_set_eval_breaker_bit(t, _PY_EVAL_PLEASE_STOP_BIT);
2639+
2640+
// The thread may have detached before the bit was set and missed it.
2641+
// Try to park it once more; a later _PyThreadState_Detach() sees the bit.
2642+
state = _Py_THREAD_DETACHED;
2643+
return _Py_atomic_compare_exchange_int(&t->state, &state,
2644+
_Py_THREAD_SUSPENDED);
2645+
}
2646+
25752647
// Try to transition threads atomically from the "detached" state to the
25762648
// "gc stopped" state. Returns true if all threads are in the "gc stopped"
25772649
static bool
@@ -2580,18 +2652,8 @@ park_detached_threads(struct _stoptheworld_state *stw)
25802652
int num_parked = 0;
25812653
_Py_FOR_EACH_STW_INTERP(stw, i) {
25822654
_Py_FOR_EACH_TSTATE_UNLOCKED(i, t) {
2583-
int state = _Py_atomic_load_int_relaxed(&t->state);
2584-
// DETACHED_WAITING threads remain counted until they attach and
2585-
// stop, so repeated pauses cannot prevent them from attaching.
2586-
if (state == _Py_THREAD_DETACHED) {
2587-
// Atomically transition to "suspended" if in "detached" state.
2588-
if (_Py_atomic_compare_exchange_int(
2589-
&t->state, &state, _Py_THREAD_SUSPENDED)) {
2590-
num_parked++;
2591-
}
2592-
}
2593-
else if (state == _Py_THREAD_ATTACHED && t != stw->requester) {
2594-
_Py_set_eval_breaker_bit(t, _PY_EVAL_PLEASE_STOP_BIT);
2655+
if (park_or_ask_to_stop(stw, t)) {
2656+
num_parked++;
25952657
}
25962658
}
25972659
}

0 commit comments

Comments
 (0)