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..8dfe9f2a497101 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -2457,10 +2457,56 @@ detach_thread(PyThreadState *tstate, int detached_state) _PyEval_ReleaseLock(tstate->interp, tstate, 0); } +#ifdef Py_GIL_DISABLED +static void +stop_after_detach(PyThreadState *tstate) +{ + _PyRuntimeState *runtime = &_PyRuntime; + PyInterpreterState *interp = tstate->interp; + + 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) { + int expected = _Py_THREAD_DETACHED; + 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 + void _PyThreadState_Detach(PyThreadState *tstate) { detach_thread(tstate, _Py_THREAD_DETACHED); +#ifdef Py_GIL_DISABLED + uintptr_t breaker = _Py_atomic_load_uintptr(&tstate->eval_breaker); + if ((breaker & _PY_EVAL_PLEASE_STOP_BIT) != 0) { + stop_after_detach(tstate); + } +#endif } void @@ -2572,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 @@ -2580,18 +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_relaxed(&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 && t != stw->requester) { - _Py_set_eval_breaker_bit(t, _PY_EVAL_PLEASE_STOP_BIT); + if (park_or_ask_to_stop(stw, t)) { + num_parked++; } } }