Skip to content

Commit

Permalink
Stop the world before fork() and Python shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
colesbury committed Apr 23, 2023
1 parent 2864b6b commit c1befd7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
20 changes: 15 additions & 5 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -572,11 +572,18 @@ PyOS_BeforeFork(void)
run_at_forkers(_PyInterpreterState_GET()->before_forkers, 1);

_PyImport_AcquireLock();

// Stop all other threads still attached to the Python VM.
_PyRuntimeState_StopTheWorld(&_PyRuntime);
HEAD_LOCK(&_PyRuntime);
}

void
PyOS_AfterFork_Parent(void)
{
HEAD_UNLOCK(&_PyRuntime);
_PyRuntimeState_StartTheWorld(&_PyRuntime);

if (_PyImport_ReleaseLock() <= 0)
Py_FatalError("failed releasing import lock after fork");

Expand Down Expand Up @@ -606,6 +613,12 @@ PyOS_AfterFork_Child(void)
tstate->native_thread_id = PyThread_get_thread_native_id();
#endif

// re-creates runtime->interpreters.mutex (HEAD_UNLOCK)
status = _PyRuntimeState_ReInitThreads(runtime);
if (_PyStatus_EXCEPTION(status)) {
goto fatal_error;
}

status = _PyEval_ReInitThreads(tstate);
if (_PyStatus_EXCEPTION(status)) {
goto fatal_error;
Expand All @@ -619,13 +632,10 @@ PyOS_AfterFork_Child(void)
_PySignal_AfterFork();
_Py_queue_after_fork();

status = _PyRuntimeState_ReInitThreads(runtime);
if (_PyStatus_EXCEPTION(status)) {
goto fatal_error;
}

PyThreadState *garbage = _PyThreadState_UnlinkExcept(runtime, tstate, 1);

_PyRuntimeState_StartTheWorld(runtime);

status = _PyInterpreterState_DeleteExceptMain(runtime);
if (_PyStatus_EXCEPTION(status)) {
goto fatal_error;
Expand Down
6 changes: 6 additions & 0 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -1867,6 +1867,12 @@ Py_FinalizeEx(void)
runtime->initialized = 0;
runtime->core_initialized = 0;

/* Wait until all daemon threads exit. Release the stoptheworld_mutex
afterwards because we're going to attempt further stop-the-worlds
via GC calls. */
_PyRuntimeState_StopTheWorld(runtime);
_PyMutex_unlock(&runtime->stoptheworld_mutex);

/* Destroy the state of all threads of the interpreter, except of the
current thread. In practice, only daemon threads should still be alive,
except if wait_for_thread_shutdown() has been cancelled by CTRL+C.
Expand Down

0 comments on commit c1befd7

Please sign in to comment.