Commit 5d926471 authored by Victor Stinner's avatar Victor Stinner Committed by GitHub

bpo-33005: Fix _PyGILState_Reinit() (#6001)

Fix a crash on fork when using a custom memory allocator (ex: using
PYTHONMALLOC env var).

_PyGILState_Reinit() and _PyInterpreterState_Enable() now use the
default RAW memory allocator to allocate a new interpreters mutex on
fork.
parent efd2bac1
Fix a crash on fork when using a custom memory allocator (ex: using
PYTHONMALLOC env var). _PyGILState_Reinit() and _PyInterpreterState_Enable()
now use the default RAW memory allocator to allocate a new interpreters mutex
on fork.
...@@ -103,15 +103,24 @@ _PyInitError ...@@ -103,15 +103,24 @@ _PyInitError
_PyInterpreterState_Enable(_PyRuntimeState *runtime) _PyInterpreterState_Enable(_PyRuntimeState *runtime)
{ {
runtime->interpreters.next_id = 0; runtime->interpreters.next_id = 0;
/* Since we only call _PyRuntimeState_Init() once per process
(see _PyRuntime_Initialize()), we make sure the mutex is /* Py_Finalize() calls _PyRuntimeState_Fini() which clears the mutex.
initialized here. */ Create a new mutex if needed. */
if (runtime->interpreters.mutex == NULL) { if (runtime->interpreters.mutex == NULL) {
/* Force default allocator, since _PyRuntimeState_Fini() must
use the same allocator than this function. */
PyMemAllocatorEx old_alloc;
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
runtime->interpreters.mutex = PyThread_allocate_lock(); runtime->interpreters.mutex = PyThread_allocate_lock();
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
if (runtime->interpreters.mutex == NULL) { if (runtime->interpreters.mutex == NULL) {
return _Py_INIT_ERR("Can't initialize threads for interpreter"); return _Py_INIT_ERR("Can't initialize threads for interpreter");
} }
} }
return _Py_INIT_OK(); return _Py_INIT_OK();
} }
...@@ -933,9 +942,19 @@ _PyGILState_Fini(void) ...@@ -933,9 +942,19 @@ _PyGILState_Fini(void)
void void
_PyGILState_Reinit(void) _PyGILState_Reinit(void)
{ {
/* Force default allocator, since _PyRuntimeState_Fini() must
use the same allocator than this function. */
PyMemAllocatorEx old_alloc;
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
_PyRuntime.interpreters.mutex = PyThread_allocate_lock(); _PyRuntime.interpreters.mutex = PyThread_allocate_lock();
if (_PyRuntime.interpreters.mutex == NULL)
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
if (_PyRuntime.interpreters.mutex == NULL) {
Py_FatalError("Can't initialize threads for interpreter"); Py_FatalError("Can't initialize threads for interpreter");
}
PyThreadState *tstate = PyGILState_GetThisThreadState(); PyThreadState *tstate = PyGILState_GetThisThreadState();
PyThread_tss_delete(&_PyRuntime.gilstate.autoTSSkey); PyThread_tss_delete(&_PyRuntime.gilstate.autoTSSkey);
if (PyThread_tss_create(&_PyRuntime.gilstate.autoTSSkey) != 0) { if (PyThread_tss_create(&_PyRuntime.gilstate.autoTSSkey) != 0) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment