8000 bpo-42540: reallocation of id_mutex should not force default allocato… · python/cpython@736684b · GitHub
[go: up one dir, main page]

Skip to content

Commit 736684b

Browse files
authored
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.
1 parent b919d81 commit 736684b

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

Lib/test/test_os.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4500,6 +4500,22 @@ def test_times(self):
45004500
self.assertEqual(times.elapsed, 0)
45014501

45024502

4503+
@requires_os_func('fork')
4504+
class ForkTests(unittest.TestCase):
4505+
def test_fork(self):
4506+
# bpo-42540: ensure os.fork() with non-default memory allocator does
4507+
# not crash on exit.
4508+
code = """if 1:
4509+
import os
4510+
from test import support
4511+
pid = os.fork()
4512+
if pid != 0:
4513+
support.wait_process(pid, exitcode=0)
4514+
"""
4515+
assert_python_ok("-c", code)
4516+
assert_python_ok("-c", code, PYTHONMALLOC="malloc_debug")
4517+
4518+
45034519
# Only test if the C version is provided, otherwise TestPEP519 already tested
45044520
# the pure Python implementation.
45054521
if hasattr(os, "_fspath"):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix crash when :func:`os.fork` is called with an active non-default
2+
memory allocator.

Python/pystate.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,15 @@ _PyRuntimeState_ReInitThreads(_PyRuntimeState *runtime)
148148
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
149149

150150
int reinit_interp = _PyThread_at_fork_reinit(&runtime->interpreters.mutex);
151-
int reinit_main_id = _PyThread_at_fork_reinit(&runtime->interpreters.main->id_mutex);
152151
int reinit_xidregistry = _PyThread_at_fork_reinit(&runtime->xidregistry.mutex);
153152
int reinit_unicode_ids = _PyThread_at_fork_reinit(&runtime->unicode_ids.lock);
154153

155154
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
156155

156+
/* bpo-42540: id_mutex is freed by _PyInterpreterState_Delete, which does
157+
* not force the default allocator. */
158+
int reinit_main_id = _PyThread_at_fork_reinit(&runtime->interpreters.main->id_mutex);
159+
157160
if (reinit_interp < 0
158161
|| reinit_main_id < 0
159162
|| reinit_xidregistry < 0

0 commit comments

Comments
 (0)
0