@@ -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.
24622461static 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"
26132649static 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