]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.9] bpo-42540: reallocation of id_mutex should not force default allocator (GH...
authorSam Gross <colesbury@gmail.com>
Wed, 17 Nov 2021 22:53:33 +0000 (17:53 -0500)
committerGitHub <noreply@github.com>
Wed, 17 Nov 2021 22:53:33 +0000 (23:53 +0100)
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@gmail.com>
Lib/test/test_os.py
Misc/NEWS.d/next/Core and Builtins/2021-11-15-12-08-27.bpo-42540.V2w107.rst [new file with mode: 0644]
Python/pystate.c

index 35933e9c8c3a971f7ef2a23497331ffb09a8c1b9..59ddf9e0b3c9193af2182417a43bf418b2f74c7e 100644 (file)
@@ -4273,6 +4273,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"):
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 (file)
index 0000000..9116059
--- /dev/null
@@ -0,0 +1,2 @@
+Fix crash when :func:`os.fork` is called with an active non-default
+memory allocator.
index 9beefa8e20c444d1664f65c70782bf1239e61bdf..c3520c336a794e6807cc2ae7515d4c02a2e2c4ef 100644 (file)
@@ -139,11 +139,14 @@ _PyRuntimeState_ReInitThreads(_PyRuntimeState *runtime)
     _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
 
     int interp_mutex = _PyThread_at_fork_reinit(&runtime->interpreters.mutex);
-    int main_interp_id_mutex = _PyThread_at_fork_reinit(&runtime->interpreters.main->id_mutex);
     int xidregistry_mutex = _PyThread_at_fork_reinit(&runtime->xidregistry.mutex);
 
     PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
 
+    /* bpo-42540: id_mutex is freed by _PyInterpreterState_Delete, which does
+     * not force the default allocator. */
+    int main_interp_id_mutex = _PyThread_at_fork_reinit(&runtime->interpreters.main->id_mutex);
+
     if (interp_mutex < 0) {
         Py_FatalError("Can't initialize lock for runtime interpreters");
     }