bpo-42540: reallocation of id_mutex should not force default allocator (GH-29564)

Unlike the other locks reinitialized by _PyRuntimeState_ReInitThreads,
the "interpreters.main->id_mutex" is not freed by _PyRuntimeState_Fini
and should not force the default raw allocator.
This commit is contained in:
Sam Gross 2021-11-17 15:51:03 -05:00 committed by GitHub
parent b919d8105c
commit 736684b1bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 1 deletions

View File

@ -4500,6 +4500,22 @@ class TimesTests(unittest.TestCase):
self.assertEqual(times.elapsed, 0)
@requires_os_func('fork')
class ForkTests(unittest.TestCase):
def test_fork(self):
# bpo-42540: ensure os.fork() with non-default memory allocator does
# not crash on exit.
code = """if 1:
import os
from test import support
pid = os.fork()
if pid != 0:
support.wait_process(pid, exitcode=0)
"""
assert_python_ok("-c", code)
assert_python_ok("-c", code, PYTHONMALLOC="malloc_debug")
# Only test if the C version is provided, otherwise TestPEP519 already tested
# the pure Python implementation.
if hasattr(os, "_fspath"):

View File

@ -0,0 +1,2 @@
Fix crash when :func:`os.fork` is called with an active non-default
memory allocator.

View File

@ -148,12 +148,15 @@ _PyRuntimeState_ReInitThreads(_PyRuntimeState *runtime)
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
int reinit_interp = _PyThread_at_fork_reinit(&runtime->interpreters.mutex);
int reinit_main_id = _PyThread_at_fork_reinit(&runtime->interpreters.main->id_mutex);
int reinit_xidregistry = _PyThread_at_fork_reinit(&runtime->xidregistry.mutex);
int reinit_unicode_ids = _PyThread_at_fork_reinit(&runtime->unicode_ids.lock);
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
/* bpo-42540: id_mutex is freed by _PyInterpreterState_Delete, which does
* not force the default allocator. */
int reinit_main_id = _PyThread_at_fork_reinit(&runtime->interpreters.main->id_mutex);
if (reinit_interp < 0
|| reinit_main_id < 0
|| reinit_xidregistry < 0