gh-116522: Refactor _PyThreadState_DeleteExcept (#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.
This commit is contained in:
Sam Gross 2024-03-21 14:21:02 -04:00 committed by GitHub
parent 50369e6c34
commit 1f72fb5447
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 41 additions and 23 deletions

View file

@ -579,9 +579,8 @@ PyEval_ReleaseThread(PyThreadState *tstate)
}
#ifdef HAVE_FORK
/* This function is called from PyOS_AfterFork_Child to destroy all threads
which are not running in the child process, and clear internal locks
which might be held by those threads. */
/* This function is called from PyOS_AfterFork_Child to re-initialize the
GIL and pending calls lock. */
PyStatus
_PyEval_ReInitThreads(PyThreadState *tstate)
{
@ -598,8 +597,6 @@ _PyEval_ReInitThreads(PyThreadState *tstate)
struct _pending_calls *pending = &tstate->interp->ceval.pending;
_PyMutex_at_fork_reinit(&pending->mutex);
/* Destroy all threads except the current one */
_PyThreadState_DeleteExcept(tstate);
return _PyStatus_OK();
}
#endif