@@ -161,11 +161,27 @@ static void unbind_gilstate_tstate(PyThreadState *);
161161static void tstate_mimalloc_bind (PyThreadState * );
162162static void add_threadstate (PyInterpreterState * interp , PyThreadState * tstate ,
163163 PyThreadState * next );
164+ static void unlink_threadstate (PyThreadState * * head , PyThreadState * tstate );
164165
165- static void
166+ // Bind the thread state to the current OS thread and publish it to the
167+ // interpreter's thread list. Returns 0 without binding if the interpreter
168+ // started finalizing before the thread state could be bound.
169+ static int
166170bind_tstate (PyThreadState * tstate )
167171{
168172 assert (tstate != NULL );
173+ // Finalization marks the pending thread states as shutting down with
174+ // the lock held, see _PyThreadState_RemoveExcept(). Hold the lock until
175+ // the thread state is published so that the interpreter cannot be
176+ // finalized (and freed) while it is still being used here.
177+ // Don't use tstate->interp before the check: it may be already freed.
178+ HEAD_LOCK (& _PyRuntime );
179+ int state = _Py_atomic_load_int_relaxed (& tstate -> state );
180+ if (state == _Py_THREAD_SHUTTING_DOWN ) {
181+ HEAD_UNLOCK (& _PyRuntime );
182+ return 0 ;
183+ }
184+
169185 assert (tstate_is_alive (tstate ) && !tstate -> _status .bound );
170186 assert (!tstate -> _status .unbound ); // just in case
171187 assert (!tstate -> _status .bound_gilstate );
@@ -197,13 +213,14 @@ bind_tstate(PyThreadState *tstate)
197213 // that it is fully initialized, so that readers of the list never see
198214 // a partially initialized thread state.
199215 PyInterpreterState * interp = tstate -> interp ;
200- HEAD_LOCK (interp -> runtime );
201216 if (interp -> stoptheworld .requested || _PyRuntime .stoptheworld .requested ) {
202217 // Start in the suspended state if there is an ongoing stop-the-world.
203218 tstate -> state = _Py_THREAD_SUSPENDED ;
204219 }
220+ unlink_threadstate (& interp -> threads .pending , tstate );
205221 add_threadstate (interp , tstate , interp -> threads .head );
206- HEAD_UNLOCK (interp -> runtime );
222+ HEAD_UNLOCK (& _PyRuntime );
223+ return 1 ;
207224}
208225
209226static void
@@ -1073,7 +1090,7 @@ PyInterpreterState_Delete(PyInterpreterState *interp)
10731090 break ;
10741091 }
10751092 }
1076- if (interp -> threads .head != NULL ) {
1093+ if (interp -> threads .head != NULL || interp -> threads . pending != NULL ) {
10771094 Py_FatalError ("remaining threads" );
10781095 }
10791096 * p = interp -> next ;
@@ -1690,6 +1707,40 @@ add_threadstate(PyInterpreterState *interp, PyThreadState *tstate,
16901707 interp -> threads .head = tstate ;
16911708}
16921709
1710+ // Add a new, not yet bound, thread state to the interpreter's list of
1711+ // pending thread states. bind_tstate() moves it to the thread list.
1712+ static void
1713+
1714+ add_pending_threadstate (PyInterpreterState * interp , PyThreadState * tstate )
1715+ {
1716+ assert (tstate -> prev == NULL && tstate -> next == NULL );
1717+ PyThreadState * next = interp -> threads .pending ;
1718+ if (next != NULL ) {
1719+ assert (next -> prev == NULL );
1720+ next -> prev = tstate ;
1721+ }
1722+ tstate -> next = next ;
1723+ interp -> threads .pending = tstate ;
1724+ }
1725+
1726+ // Remove the thread state from the list starting at `*head`, which is
1727+ // either the interpreter's thread list or its list of pending thread states.
1728+ static void
1729+ unlink_threadstate (PyThreadState * * head , PyThreadState * tstate )
1730+ {
1731+ if (tstate -> prev ) {
1732+ tstate -> prev -> next = tstate -> next ;
1733+ }
1734+ else {
1735+ assert (* head == tstate );
1736+ * head = tstate -> next ;
1737+ }
1738+ if (tstate -> next ) {
1739+ tstate -> next -> prev = tstate -> prev ;
1740+ }
1741+ tstate -> prev = tstate -> next = NULL ;
1742+ }
1743+
16931744static PyThreadState *
16941745new_threadstate (PyInterpreterState * interp , int whence )
16951746{
@@ -1729,6 +1780,10 @@ new_threadstate(PyInterpreterState *interp, int whence)
17291780 uint64_t id = interp -> threads .next_unique_id ;
17301781 init_threadstate (tstate , interp , id , whence );
17311782
1783+ // Track the new thread state so that finalization can find it. It is
1784+ // only added to the interpreter's thread list once it is bound.
1785+ add_pending_threadstate (interp , (PyThreadState * )tstate );
1786+
17321787 HEAD_UNLOCK (interp -> runtime );
17331788
17341789#ifdef Py_GIL_DISABLED
@@ -1751,10 +1806,9 @@ _PyThreadState_NewBound(PyInterpreterState *interp, int whence)
17511806{
17521807 PyThreadState * tstate = new_threadstate (interp , whence );
17531808 if (tstate ) {
1754- bind_tstate (tstate );
17551809 // This makes sure there's a gilstate tstate bound
17561810 // as soon as possible.
1757- if (gilstate_get () == NULL ) {
1811+ if (bind_tstate ( tstate ) && gilstate_get () == NULL ) {
17581812 bind_gilstate_tstate (tstate );
17591813 }
17601814 }
@@ -1972,15 +2026,7 @@ tstate_delete_common(PyThreadState *tstate, int release_gil, int is_unlinked)
19722026
19732027 HEAD_LOCK (runtime );
19742028 if (tstate -> _status .bound ) {
1975- if (tstate -> prev ) {
1976- tstate -> prev -> next = tstate -> next ;
1977- }
1978- else {
1979- interp -> threads .head = tstate -> next ;
1980- }
1981- if (tstate -> next ) {
1982- tstate -> next -> prev = tstate -> prev ;
1983- }
2029+ unlink_threadstate (& interp -> threads .head , tstate );
19842030 int state = _Py_atomic_load_int_relaxed (& tstate -> state );
19852031 if (state != _Py_THREAD_SUSPENDED &&
19862032 state != _Py_THREAD_SUSPENDED_WAITING )
@@ -1996,11 +2042,10 @@ tstate_delete_common(PyThreadState *tstate, int release_gil, int is_unlinked)
19962042 }
19972043 }
19982044 else {
1999- // The thread state was never bound, so it was never added to the
2000- // interpreter's thread list and no stop-the-world request is
2001- // waiting on it.
2002- assert (tstate -> prev == NULL && tstate -> next == NULL );
2003- assert (interp -> threads .head != tstate );
2045+ // The thread state was never bound, so it is still on the list of
2046+ // pending thread states and no stop-the-world request is waiting
2047+ // on it.
2048+ unlink_threadstate (& interp -> threads .pending , tstate );
20042049 }
20052050
20062051#if defined(Py_REF_DEBUG ) && defined(Py_GIL_DISABLED )
@@ -2049,7 +2094,9 @@ zapthreads(PyInterpreterState *interp)
20492094 Cannot use _Py_FOR_EACH_TSTATE_UNLOCKED because we are freeing
20502095 the thread states here.
20512096 */
2052- while ((tstate = interp -> threads .head ) != NULL ) {
2097+ while ((tstate = interp -> threads .head ) != NULL ||
2098+ (tstate = interp -> threads .pending ) != NULL )
2099+ {
20532100 tstate_verify_not_active (tstate );
20542101 tstate_delete_common (tstate , /*release_gil=*/ 0 , /*is_unlinked=*/ 0 );
20552102 free_threadstate ((_PyThreadStateImpl * )tstate );
@@ -2125,6 +2172,26 @@ _PyThreadState_RemoveExcept(PyThreadState *tstate)
21252172 }
21262173 tstate -> prev = tstate -> next = NULL ;
21272174 interp -> threads .head = tstate ;
2175+
2176+ /* Also remove the thread states which are not bound yet. Mark them as
2177+ shutting down while holding the lock, so that bind_tstate() does not
2178+ bind them to an interpreter which is being finalized. */
2179+ PyThreadState * pending = interp -> threads .pending ;
2180+ interp -> threads .pending = NULL ;
2181+ for (PyThreadState * p = pending ; p != NULL ; p = p -> next ) {
2182+ _Py_atomic_store_int (& p -> state , _Py_THREAD_SHUTTING_DOWN );
2183+ }
2184+ if (list == NULL ) {
2185+ list = pending ;
2186+ }
2187+ else if (pending != NULL ) {
2188+ PyThreadState * tail = list ;
2189+ while (tail -> next != NULL ) {
2190+ tail = tail -> next ;
2191+ }
2192+ tail -> next = pending ;
2193+ pending -> prev = tail ;
2194+ }
21282195 HEAD_UNLOCK (runtime );
21292196
21302197 return list ;
@@ -2774,7 +2841,11 @@ _PyThreadState_Bind(PyThreadState *tstate)
27742841 // was called, tstate becomes a dangling pointer.
27752842 assert (_PyThreadState_CheckConsistency (tstate ));
27762843
2777- bind_tstate (tstate );
2844+ // If the interpreter is already finalizing, the thread state is left
2845+ // unbound and the thread hangs once it tries to attach.
2846+ if (!bind_tstate (tstate )) {
2847+ return ;
2848+ }
27782849 // This makes sure there's a gilstate tstate bound
27792850 // as soon as possible.
27802851 if (gilstate_get () == NULL ) {
0 commit comments