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@gmail.com>
diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py
index 684e308..0ad13d5 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)
 
 
+@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"):