diff options
author | Sam Gross <colesbury@gmail.com> | 2024-03-21 14:01:16 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-21 14:01:16 (GMT) |
commit | e728303532168efab7694c55c82ea19b18bf8385 (patch) | |
tree | e715ba949d2dff22a62c42b175b4e4676f68565e /Python/pystate.c | |
parent | 1f8b24ef69896680d6ba6005e75e1cc79a744f9e (diff) | |
download | cpython-e728303532168efab7694c55c82ea19b18bf8385.zip cpython-e728303532168efab7694c55c82ea19b18bf8385.tar.gz cpython-e728303532168efab7694c55c82ea19b18bf8385.tar.bz2 |
gh-116522: Stop the world before fork() and during shutdown (#116607)
This changes the free-threaded build to perform a stop-the-world pause
before deleting other thread states when forking and during shutdown.
This fixes some crashes when using multiprocessing and during shutdown
when running with `PYTHON_GIL=0`.
This also changes `PyOS_BeforeFork` to acquire the runtime lock
(i.e., `HEAD_LOCK(&_PyRuntime)`) before forking to ensure that data
protected by the runtime lock (and not just the GIL or stop-the-world)
is in a consistent state before forking.
Diffstat (limited to 'Python/pystate.c')
-rw-r--r-- | Python/pystate.c | 6 |
1 files changed, 6 insertions, 0 deletions
diff --git a/Python/pystate.c b/Python/pystate.c index eedcb92..5a334e8 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -1692,6 +1692,10 @@ _PyThreadState_DeleteExcept(PyThreadState *tstate) PyInterpreterState *interp = tstate->interp; _PyRuntimeState *runtime = interp->runtime; +#ifdef Py_GIL_DISABLED + assert(runtime->stoptheworld.world_stopped); +#endif + HEAD_LOCK(runtime); /* Remove all thread states, except tstate, from the linked list of thread states. This will allow calling PyThreadState_Clear() @@ -1710,6 +1714,8 @@ _PyThreadState_DeleteExcept(PyThreadState *tstate) interp->threads.head = tstate; HEAD_UNLOCK(runtime); + _PyEval_StartTheWorldAll(runtime); + /* Clear and deallocate all stale thread states. Even if this executes Python code, we should be safe since it executes in the current thread, not one of the stale threads. */ |