Skip to content

Commit fa5057b

Browse files
committed
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.
1 parent 6aa9d1d commit fa5057b

2 files changed

Lines changed: 54 additions & 33 deletions

File tree

‎Include/internal/pycore_interp_structs.h‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -413,9 +413,8 @@ struct _stoptheworld_state {
413413
PyMutex mutex; // Serializes stop-the-world attempts.
414414

415415
// NOTE: The below fields are protected by HEAD_LOCK(runtime), not by the
416-
// above mutex. `requested` is written atomically so that detaching
417-
// threads can check it without holding HEAD_LOCK.
418-
int requested; // Set when a pause is requested.
416+
// above mutex.
417+
bool requested; // Set when a pause is requested.
419418
bool world_stopped; // Set when the world is stopped.
420419
bool is_global; // Set when contained in PyRuntime struct.
421420

‎Python/pystate.c‎

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2458,18 +2458,12 @@ detach_thread(PyThreadState *tstate, int detached_state)
24582458
}
24592459

24602460
#ifdef Py_GIL_DISABLED
2461-
// Called after the current thread switched to the "detached" state.
24622461
static void
2463-
notify_stw_on_detach(PyThreadState *tstate)
2462+
stop_after_detach(PyThreadState *tstate)
24642463
{
24652464
_PyRuntimeState *runtime = &_PyRuntime;
24662465
PyInterpreterState *interp = tstate->interp;
24672466

2468-
if (!_Py_atomic_load_int(&runtime->stoptheworld.requested) &&
2469-
!_Py_atomic_load_int(&interp->stoptheworld.requested)) {
2470-
return;
2471-
}
2472-
24732467
HEAD_LOCK(runtime);
24742468
struct _stoptheworld_state *stw = NULL;
24752469
if (runtime->stoptheworld.requested) {
@@ -2479,13 +2473,26 @@ notify_stw_on_detach(PyThreadState *tstate)
24792473
stw = &interp->stoptheworld;
24802474
}
24812475
if (stw != NULL && stw->requester != tstate) {
2482-
// If the CAS fails, the requester already parked this thread.
24832476
int expected = _Py_THREAD_DETACHED;
2484-
if (_Py_atomic_compare_exchange_int(&tstate->state, &expected,
2485-
_Py_THREAD_SUSPENDED)) {
2486-
decrement_stoptheworld_countdown(stw);
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;
24872483
}
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);
24882494
}
2495+
#endif
24892496
HEAD_UNLOCK(runtime);
24902497
}
24912498
#endif
@@ -2495,7 +2502,10 @@ _PyThreadState_Detach(PyThreadState *tstate)
24952502
{
24962503
detach_thread(tstate, _Py_THREAD_DETACHED);
24972504
#ifdef Py_GIL_DISABLED
2498-
notify_stw_on_detach(tstate);
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+
}
24992509
#endif
25002510
}
25012511

@@ -2608,6 +2618,32 @@ interp_for_stop_the_world(struct _stoptheworld_state *stw)
26082618
i != NULL; i = ((stw->is_global) ? i->next : NULL))
26092619

26102620

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+
26112647
// Try to transition threads atomically from the "detached" state to the
26122648
// "gc stopped" state. Returns true if all threads are in the "gc stopped"
26132649
static bool
@@ -2616,22 +2652,8 @@ park_detached_threads(struct _stoptheworld_state *stw)
26162652
int num_parked = 0;
26172653
_Py_FOR_EACH_STW_INTERP(stw, i) {
26182654
_Py_FOR_EACH_TSTATE_UNLOCKED(i, t) {
2619-
int state = _Py_atomic_load_int(&t->state);
2620-
// DETACHED_WAITING threads remain counted until they attach and
2621-
// stop, so repeated pauses cannot prevent them from attaching.
2622-
if (state == _Py_THREAD_DETACHED) {
2623-
// Atomically transition to "suspended" if in "detached" state.
2624-
if (_Py_atomic_compare_exchange_int(
2625-
&t->state, &state, _Py_THREAD_SUSPENDED)) {
2626-
num_parked++;
2627-
}
2628-
}
2629-
else if ((state == _Py_THREAD_ATTACHED ||
2630-
state == _Py_THREAD_DETACHED_WAITING) &&
2631-
t != stw->requester) {
2632-
// Ask attached threads to stop. A DETACHED_WAITING thread
2633-
// will attach soon, so ask it to stop once it is attached.
2634-
_Py_set_eval_breaker_bit(t, _PY_EVAL_PLEASE_STOP_BIT);
2655+
if (park_or_ask_to_stop(stw, t)) {
2656+
num_parked++;
26352657
}
26362658
}
26372659
}
@@ -2656,7 +2678,7 @@ stop_the_world(struct _stoptheworld_state *stw)
26562678
PyMutex_Lock(&stw->mutex);
26572679

26582680
HEAD_LOCK(runtime);
2659-
_Py_atomic_store_int(&stw->requested, 1);
2681+
stw->requested = 1;
26602682
stw->thread_countdown = 0;
26612683
stw->stop_event = (PyEvent){0}; // zero-initialize (unset)
26622684
stw->requester = _PyThreadState_GET(); // may be NULL
@@ -2705,7 +2727,7 @@ start_the_world(struct _stoptheworld_state *stw)
27052727
assert(PyMutex_IsLocked(&stw->mutex));
27062728

27072729
HEAD_LOCK(runtime);
2708-
_Py_atomic_store_int(&stw->requested, 0);
2730+
stw->requested = 0;
27092731
stw->world_stopped = 0;
27102732
// Switch threads back to the detached state.
27112733
_Py_FOR_EACH_STW_INTERP(stw, i) {

0 commit comments

Comments
 (0)