[3.11]GH-112275: Fix HEAD_LOCK deadlock in child process after fork (#112336)

HEAD_LOCK is called from _PyEval_ReInitThreads->_PyThreadState_DeleteExcept before _PyRuntimeState_ReInitThreads reinit runtime->interpreters.mutex which might be locked before fork.

Co-authored-by: Łukasz Langa <lukasz@langa.pl>
This commit is contained in:
ChuBoning 2024-09-05 01:14:59 +08:00 committed by GitHub
parent 851821d871
commit 522799a05e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 8 additions and 5 deletions

View file

@ -0,0 +1,3 @@
A deadlock involving ``pystate.c``'s ``HEAD_LOCK`` in ``posixmodule.c``
at fork is now fixed. Patch by ChuBoning based on previous Python 3.12
fix by Victor Stinner.

View file

@ -603,6 +603,11 @@ PyOS_AfterFork_Child(void)
goto fatal_error;
}
status = _PyRuntimeState_ReInitThreads(runtime);
if (_PyStatus_EXCEPTION(status)) {
goto fatal_error;
}
PyThreadState *tstate = _PyThreadState_GET();
_Py_EnsureTstateNotNULL(tstate);
@ -622,11 +627,6 @@ PyOS_AfterFork_Child(void)
_PySignal_AfterFork();
status = _PyRuntimeState_ReInitThreads(runtime);
if (_PyStatus_EXCEPTION(status)) {
goto fatal_error;
}
status = _PyInterpreterState_DeleteExceptMain(runtime);
if (_PyStatus_EXCEPTION(status)) {
goto fatal_error;