From 6aa9d1dd8dd72366e82c2ee59ba972917fb2c02a Mon Sep 17 00:00:00 2001 From: Neil Schemenauer Date: Fri, 27 Nov 2026 14:15:39 -0700 Subject: [PATCH 1/2] gh-162764: Avoid 1 ms STW delay when a thread detaches 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. --- Include/internal/pycore_interp_structs.h | 5 +- ...-11-27-12-00-00.gh-issue-162764.StwDtc.rst | 5 ++ Python/pystate.c | 48 +++++++++++++++++-- 3 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-11-27-12-00-00.gh-issue-162764.StwDtc.rst diff --git a/Include/internal/pycore_interp_structs.h b/Include/internal/pycore_interp_structs.h index c9002081306eb8..6edd526b0c920a 100644 --- a/Include/internal/pycore_interp_structs.h +++ b/Include/internal/pycore_interp_structs.h @@ -413,8 +413,9 @@ struct _stoptheworld_state { PyMutex mutex; // Serializes stop-the-world attempts. // NOTE: The below fields are protected by HEAD_LOCK(runtime), not by the - // above mutex. - bool requested; // Set when a pause is requested. + // above mutex. `requested` is written atomically so that detaching + // threads can check it without holding HEAD_LOCK. + int requested; // Set when a pause is requested. bool world_stopped; // Set when the world is stopped. bool is_global; // Set when contained in PyRuntime struct. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-27-12-00-00.gh-issue-162764.StwDtc.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-27-12-00-00.gh-issue-162764.StwDtc.rst new file mode 100644 index 00000000000000..5e02154e8f011c --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-27-12-00-00.gh-issue-162764.StwDtc.rst @@ -0,0 +1,5 @@ +Reduce the latency of stop-the-world pauses in the :term:`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. diff --git a/Python/pystate.c b/Python/pystate.c index d253f4dd89310d..5f71744bf52b1b 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -2457,10 +2457,46 @@ detach_thread(PyThreadState *tstate, int detached_state) _PyEval_ReleaseLock(tstate->interp, tstate, 0); } +#ifdef Py_GIL_DISABLED +// Called after the current thread switched to the "detached" state. +static void +notify_stw_on_detach(PyThreadState *tstate) +{ + _PyRuntimeState *runtime = &_PyRuntime; + PyInterpreterState *interp = tstate->interp; + + if (!_Py_atomic_load_int(&runtime->stoptheworld.requested) && + !_Py_atomic_load_int(&interp->stoptheworld.requested)) { + return; + } + + HEAD_LOCK(runtime); + struct _stoptheworld_state *stw = NULL; + if (runtime->stoptheworld.requested) { + stw = &runtime->stoptheworld; + } + else if (interp->stoptheworld.requested) { + stw = &interp->stoptheworld; + } + if (stw != NULL && stw->requester != tstate) { + // If the CAS fails, the requester already parked this thread. + int expected = _Py_THREAD_DETACHED; + if (_Py_atomic_compare_exchange_int(&tstate->state, &expected, + _Py_THREAD_SUSPENDED)) { + decrement_stoptheworld_countdown(stw); + } + } + HEAD_UNLOCK(runtime); +} +#endif + void _PyThreadState_Detach(PyThreadState *tstate) { detach_thread(tstate, _Py_THREAD_DETACHED); +#ifdef Py_GIL_DISABLED + notify_stw_on_detach(tstate); +#endif } void @@ -2580,7 +2616,7 @@ park_detached_threads(struct _stoptheworld_state *stw) int num_parked = 0; _Py_FOR_EACH_STW_INTERP(stw, i) { _Py_FOR_EACH_TSTATE_UNLOCKED(i, t) { - int state = _Py_atomic_load_int_relaxed(&t->state); + int state = _Py_atomic_load_int(&t->state); // DETACHED_WAITING threads remain counted until they attach and // stop, so repeated pauses cannot prevent them from attaching. if (state == _Py_THREAD_DETACHED) { @@ -2590,7 +2626,11 @@ park_detached_threads(struct _stoptheworld_state *stw) num_parked++; } } - else if (state == _Py_THREAD_ATTACHED && t != stw->requester) { + else if ((state == _Py_THREAD_ATTACHED || + state == _Py_THREAD_DETACHED_WAITING) && + t != stw->requester) { + // Ask attached threads to stop. A DETACHED_WAITING thread + // will attach soon, so ask it to stop once it is attached. _Py_set_eval_breaker_bit(t, _PY_EVAL_PLEASE_STOP_BIT); } } @@ -2616,7 +2656,7 @@ stop_the_world(struct _stoptheworld_state *stw) PyMutex_Lock(&stw->mutex); HEAD_LOCK(runtime); - stw->requested = 1; + _Py_atomic_store_int(&stw->requested, 1); stw->thread_countdown = 0; stw->stop_event = (PyEvent){0}; // zero-initialize (unset) stw->requester = _PyThreadState_GET(); // may be NULL @@ -2665,7 +2705,7 @@ start_the_world(struct _stoptheworld_state *stw) assert(PyMutex_IsLocked(&stw->mutex)); HEAD_LOCK(runtime); - stw->requested = 0; + _Py_atomic_store_int(&stw->requested, 0); stw->world_stopped = 0; // Switch threads back to the detached state. _Py_FOR_EACH_STW_INTERP(stw, i) { From fa5057bb23aee5f2f970098a4fb6b1a066374a4b Mon Sep 17 00:00:00 2001 From: Neil Schemenauer Date: Sat, 28 Nov 2026 16:27:38 -0700 Subject: [PATCH 2/2] gh-162764: Use the please-stop bit to detect a pause on detach 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. --- Include/internal/pycore_interp_structs.h | 5 +- Python/pystate.c | 82 +++++++++++++++--------- 2 files changed, 54 insertions(+), 33 deletions(-) diff --git a/Include/internal/pycore_interp_structs.h b/Include/internal/pycore_interp_structs.h index 6edd526b0c920a..c9002081306eb8 100644 --- a/Include/internal/pycore_interp_structs.h +++ b/Include/internal/pycore_interp_structs.h @@ -413,9 +413,8 @@ struct _stoptheworld_state { PyMutex mutex; // Serializes stop-the-world attempts. // NOTE: The below fields are protected by HEAD_LOCK(runtime), not by the - // above mutex. `requested` is written atomically so that detaching - // threads can check it without holding HEAD_LOCK. - int requested; // Set when a pause is requested. + // above mutex. + bool requested; // Set when a pause is requested. bool world_stopped; // Set when the world is stopped. bool is_global; // Set when contained in PyRuntime struct. diff --git a/Python/pystate.c b/Python/pystate.c index 5f71744bf52b1b..8dfe9f2a497101 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -2458,18 +2458,12 @@ detach_thread(PyThreadState *tstate, int detached_state) } #ifdef Py_GIL_DISABLED -// Called after the current thread switched to the "detached" state. static void -notify_stw_on_detach(PyThreadState *tstate) +stop_after_detach(PyThreadState *tstate) { _PyRuntimeState *runtime = &_PyRuntime; PyInterpreterState *interp = tstate->interp; - if (!_Py_atomic_load_int(&runtime->stoptheworld.requested) && - !_Py_atomic_load_int(&interp->stoptheworld.requested)) { - return; - } - HEAD_LOCK(runtime); struct _stoptheworld_state *stw = NULL; if (runtime->stoptheworld.requested) { @@ -2479,13 +2473,26 @@ notify_stw_on_detach(PyThreadState *tstate) stw = &interp->stoptheworld; } if (stw != NULL && stw->requester != tstate) { - // If the CAS fails, the requester already parked this thread. int expected = _Py_THREAD_DETACHED; - if (_Py_atomic_compare_exchange_int(&tstate->state, &expected, - _Py_THREAD_SUSPENDED)) { - decrement_stoptheworld_countdown(stw); + if (!_Py_atomic_compare_exchange_int(&tstate->state, &expected, + _Py_THREAD_SUSPENDED)) { + // BRC may have suspended this thread without counting it for STW. + // Leave the stop bit set so it sees the request when it reattaches. + HEAD_UNLOCK(runtime); + return; } + decrement_stoptheworld_countdown(stw); + } + // The request was handled or the bit was stale. Requests set the bit + // while holding HEAD_LOCK, so clearing it here cannot lose one. + _Py_unset_eval_breaker_bit(tstate, _PY_EVAL_PLEASE_STOP_BIT); +#if defined(Py_REMOTE_DEBUG) && defined(Py_SUPPORTS_REMOTE_DEBUG) + if (tstate->remote_debugger_support.debugger_pending_call) { + // A remote debugger also sets the bit, to get the thread to call + // _Py_HandlePending(). + _Py_set_eval_breaker_bit(tstate, _PY_EVAL_PLEASE_STOP_BIT); } +#endif HEAD_UNLOCK(runtime); } #endif @@ -2495,7 +2502,10 @@ _PyThreadState_Detach(PyThreadState *tstate) { detach_thread(tstate, _Py_THREAD_DETACHED); #ifdef Py_GIL_DISABLED - notify_stw_on_detach(tstate); + uintptr_t breaker = _Py_atomic_load_uintptr(&tstate->eval_breaker); + if ((breaker & _PY_EVAL_PLEASE_STOP_BIT) != 0) { + stop_after_detach(tstate); + } #endif } @@ -2608,6 +2618,32 @@ interp_for_stop_the_world(struct _stoptheworld_state *stw) i != NULL; i = ((stw->is_global) ? i->next : NULL)) +static bool +park_or_ask_to_stop(struct _stoptheworld_state *stw, PyThreadState *t) +{ + if (t == stw->requester) { + return false; + } + int state = _Py_THREAD_DETACHED; + if (_Py_atomic_compare_exchange_int(&t->state, &state, + _Py_THREAD_SUSPENDED)) { + return true; + } + if (state != _Py_THREAD_ATTACHED && state != _Py_THREAD_DETACHED_WAITING) { + return false; + } + + // Let a DETACHED_WAITING thread attach before stopping it, so repeated + // pauses cannot starve its attach attempt. + _Py_set_eval_breaker_bit(t, _PY_EVAL_PLEASE_STOP_BIT); + + // The thread may have detached before the bit was set and missed it. + // Try to park it once more; a later _PyThreadState_Detach() sees the bit. + state = _Py_THREAD_DETACHED; + return _Py_atomic_compare_exchange_int(&t->state, &state, + _Py_THREAD_SUSPENDED); +} + // Try to transition threads atomically from the "detached" state to the // "gc stopped" state. Returns true if all threads are in the "gc stopped" static bool @@ -2616,22 +2652,8 @@ park_detached_threads(struct _stoptheworld_state *stw) int num_parked = 0; _Py_FOR_EACH_STW_INTERP(stw, i) { _Py_FOR_EACH_TSTATE_UNLOCKED(i, t) { - int state = _Py_atomic_load_int(&t->state); - // DETACHED_WAITING threads remain counted until they attach and - // stop, so repeated pauses cannot prevent them from attaching. - if (state == _Py_THREAD_DETACHED) { - // Atomically transition to "suspended" if in "detached" state. - if (_Py_atomic_compare_exchange_int( - &t->state, &state, _Py_THREAD_SUSPENDED)) { - num_parked++; - } - } - else if ((state == _Py_THREAD_ATTACHED || - state == _Py_THREAD_DETACHED_WAITING) && - t != stw->requester) { - // Ask attached threads to stop. A DETACHED_WAITING thread - // will attach soon, so ask it to stop once it is attached. - _Py_set_eval_breaker_bit(t, _PY_EVAL_PLEASE_STOP_BIT); + if (park_or_ask_to_stop(stw, t)) { + num_parked++; } } } @@ -2656,7 +2678,7 @@ stop_the_world(struct _stoptheworld_state *stw) PyMutex_Lock(&stw->mutex); HEAD_LOCK(runtime); - _Py_atomic_store_int(&stw->requested, 1); + stw->requested = 1; stw->thread_countdown = 0; stw->stop_event = (PyEvent){0}; // zero-initialize (unset) stw->requester = _PyThreadState_GET(); // may be NULL @@ -2705,7 +2727,7 @@ start_the_world(struct _stoptheworld_state *stw) assert(PyMutex_IsLocked(&stw->mutex)); HEAD_LOCK(runtime); - _Py_atomic_store_int(&stw->requested, 0); + stw->requested = 0; stw->world_stopped = 0; // Switch threads back to the detached state. _Py_FOR_EACH_STW_INTERP(stw, i) {