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

miss-islington webhook-mailer at python.org
Wed Nov 17 16:16:11 EST 2021


https://github.com/python/cpython/commit/1079b3e3cb3eba7062e174ecc6c0ab20c2d0722e
commit: 1079b3e3cb3eba7062e174ecc6c0ab20c2d0722e
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2021-11-17T13:16:01-08:00
summary:

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.
(cherry picked from commit 736684b1bb67369a2e95a9f621752deead44e7ef)

Co-authored-by: Sam Gross <colesbury at gmail.com>

files:
A Misc/NEWS.d/next/Core and Builtins/2021-11-15-12-08-27.bpo-42540.V2w107.rst
M Lib/test/test_os.py
M Python/pystate.c

diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py
index 684e308ad3a05..0ad13d59ded37 100644
--- a/Lib/test/test_os.py
+++ b/Lib/test/test_os.py
@@ -4501,6 +4501,22 @@ def test_times(self):
             self.assertEqual(times.elapsed, 0)
 
 
+ at 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"):
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-11-15-12-08-27.bpo-42540.V2w107.rst b/Misc/NEWS.d/next/Core and Builtins/2021-11-15-12-08-27.bpo-42540.V2w107.rst
new file mode 100644
index 0000000000000..91160598bd3f4
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2021-11-15-12-08-27.bpo-42540.V2w107.rst	
@@ -0,0 +1,2 @@
+Fix crash when :func:`os.fork` is called with an active non-default
+memory allocator.
diff --git a/Python/pystate.c b/Python/pystate.c
index aeebd6f61c6d7..df98eb11bb0a4 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -147,12 +147,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



More information about the Python-checkins mailing list