Skip to content

Commit 2c014b5

Browse files
colesburydiegorusso
authored andcommitted
pythongh-116522: Refactor _PyThreadState_DeleteExcept (python#117131)
Split `_PyThreadState_DeleteExcept` into two functions: - `_PyThreadState_RemoveExcept` removes all thread states other than one passed as an argument. It returns the removed thread states as a linked list. - `_PyThreadState_DeleteList` deletes those dead thread states. It may call destructors, so we want to "start the world" before calling `_PyThreadState_DeleteList` to avoid potential deadlocks.
1 parent 41c02ea commit 2c014b5

File tree

5 files changed

+41
-23
lines changed

5 files changed

+41
-23
lines changed

Include/internal/pycore_pystate.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,8 @@ extern PyThreadState * _PyThreadState_New(
218218
PyInterpreterState *interp,
219219
int whence);
220220
extern void _PyThreadState_Bind(PyThreadState *tstate);
221-
extern void _PyThreadState_DeleteExcept(PyThreadState *tstate);
221+
extern PyThreadState * _PyThreadState_RemoveExcept(PyThreadState *tstate);
222+
extern void _PyThreadState_DeleteList(PyThreadState *list);
222223
extern void _PyThreadState_ClearMimallocHeaps(PyThreadState *tstate);
223224

224225
// Export for '_testinternalcapi' shared extension

Modules/posixmodule.c

+8
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,14 @@ PyOS_AfterFork_Child(void)
664664
goto fatal_error;
665665
}
666666

667+
// Remove the dead thread states. We "start the world" once we are the only
668+
// thread state left to undo the stop the world call in `PyOS_BeforeFork`.
669+
// That needs to happen before `_PyThreadState_DeleteList`, because that
670+
// may call destructors.
671+
PyThreadState *list = _PyThreadState_RemoveExcept(tstate);
672+
_PyEval_StartTheWorldAll(&_PyRuntime);
673+
_PyThreadState_DeleteList(list);
674+
667675
status = _PyImport_ReInitLock(tstate->interp);
668676
if (_PyStatus_EXCEPTION(status)) {
669677
goto fatal_error;

Python/ceval_gil.c

+2-5
Original file line numberDiff line numberDiff line change
@@ -579,9 +579,8 @@ PyEval_ReleaseThread(PyThreadState *tstate)
579579
}
580580

581581
#ifdef HAVE_FORK
582-
/* This function is called from PyOS_AfterFork_Child to destroy all threads
583-
which are not running in the child process, and clear internal locks
584-
which might be held by those threads. */
582+
/* This function is called from PyOS_AfterFork_Child to re-initialize the
583+
GIL and pending calls lock. */
585584
PyStatus
586585
_PyEval_ReInitThreads(PyThreadState *tstate)
587586
{
@@ -598,8 +597,6 @@ _PyEval_ReInitThreads(PyThreadState *tstate)
598597
struct _pending_calls *pending = &tstate->interp->ceval.pending;
599598
_PyMutex_at_fork_reinit(&pending->mutex);
600599

601-
/* Destroy all threads except the current one */
602-
_PyThreadState_DeleteExcept(tstate);
603600
return _PyStatus_OK();
604601
}
605602
#endif

Python/pylifecycle.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -1934,8 +1934,11 @@ Py_FinalizeEx(void)
19341934
will be called in the current Python thread. Since
19351935
_PyRuntimeState_SetFinalizing() has been called, no other Python thread
19361936
can take the GIL at this point: if they try, they will exit
1937-
immediately. */
1938-
_PyThreadState_DeleteExcept(tstate);
1937+
immediately. We start the world once we are the only thread state left,
1938+
before we call destructors. */
1939+
PyThreadState *list = _PyThreadState_RemoveExcept(tstate);
1940+
_PyEval_StartTheWorldAll(runtime);
1941+
_PyThreadState_DeleteList(list);
19391942

19401943
/* At this point no Python code should be running at all.
19411944
The only thread state left should be the main thread of the main

Python/pystate.c

+24-15
Original file line numberDiff line numberDiff line change
@@ -1763,15 +1763,17 @@ PyThreadState_DeleteCurrent(void)
17631763
}
17641764

17651765

1766-
/*
1767-
* Delete all thread states except the one passed as argument.
1768-
* Note that, if there is a current thread state, it *must* be the one
1769-
* passed as argument. Also, this won't touch any other interpreters
1770-
* than the current one, since we don't know which thread state should
1771-
* be kept in those other interpreters.
1772-
*/
1773-
void
1774-
_PyThreadState_DeleteExcept(PyThreadState *tstate)
1766+
// Unlinks and removes all thread states from `tstate->interp`, with the
1767+
// exception of the one passed as an argument. However, it does not delete
1768+
// these thread states. Instead, it returns the removed thread states as a
1769+
// linked list.
1770+
//
1771+
// Note that if there is a current thread state, it *must* be the one
1772+
// passed as argument. Also, this won't touch any interpreters other
1773+
// than the current one, since we don't know which thread state should
1774+
// be kept in those other interpreters.
1775+
PyThreadState *
1776+
_PyThreadState_RemoveExcept(PyThreadState *tstate)
17751777
{
17761778
assert(tstate != NULL);
17771779
PyInterpreterState *interp = tstate->interp;
@@ -1783,8 +1785,7 @@ _PyThreadState_DeleteExcept(PyThreadState *tstate)
17831785

17841786
HEAD_LOCK(runtime);
17851787
/* Remove all thread states, except tstate, from the linked list of
1786-
thread states. This will allow calling PyThreadState_Clear()
1787-
without holding the lock. */
1788+
thread states. */
17881789
PyThreadState *list = interp->threads.head;
17891790
if (list == tstate) {
17901791
list = tstate->next;
@@ -1799,11 +1800,19 @@ _PyThreadState_DeleteExcept(PyThreadState *tstate)
17991800
interp->threads.head = tstate;
18001801
HEAD_UNLOCK(runtime);
18011802

1802-
_PyEval_StartTheWorldAll(runtime);
1803+
return list;
1804+
}
1805+
1806+
// Deletes the thread states in the linked list `list`.
1807+
//
1808+
// This is intended to be used in conjunction with _PyThreadState_RemoveExcept.
1809+
void
1810+
_PyThreadState_DeleteList(PyThreadState *list)
1811+
{
1812+
// The world can't be stopped because we PyThreadState_Clear() can
1813+
// call destructors.
1814+
assert(!_PyRuntime.stoptheworld.world_stopped);
18031815

1804-
/* Clear and deallocate all stale thread states. Even if this
1805-
executes Python code, we should be safe since it executes
1806-
in the current thread, not one of the stale threads. */
18071816
PyThreadState *p, *next;
18081817
for (p = list; p; p = next) {
18091818
next = p->next;

0 commit comments

Comments
 (0)