diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-02-14-09-17-54.bpo-35993.Bvm3fP.rst b/Misc/NEWS.d/next/Core and Builtins/2019-02-14-09-17-54.bpo-35993.Bvm3fP.rst new file mode 100644 index 00000000000..3966f292ea8 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2019-02-14-09-17-54.bpo-35993.Bvm3fP.rst @@ -0,0 +1 @@ +Fix a crash on fork when using subinterpreters. Contributed by Stéphane Wirtel diff --git a/Python/pystate.c b/Python/pystate.c index f0b2a6729f3..acb672bdd9b 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -281,10 +281,11 @@ _PyInterpreterState_DeleteExceptMain() HEAD_LOCK(); PyInterpreterState *interp = _PyRuntime.interpreters.head; _PyRuntime.interpreters.head = NULL; - for (; interp != NULL; interp = interp->next) { + while (interp != NULL) { if (interp == _PyRuntime.interpreters.main) { _PyRuntime.interpreters.main->next = NULL; _PyRuntime.interpreters.head = interp; + interp = interp->next; continue; } @@ -293,7 +294,9 @@ _PyInterpreterState_DeleteExceptMain() if (interp->id_mutex != NULL) { PyThread_free_lock(interp->id_mutex); } - PyMem_RawFree(interp); + PyInterpreterState *prev_interp = interp; + interp = interp->next; + PyMem_RawFree(prev_interp); } HEAD_UNLOCK();