File tree Expand file tree Collapse file tree 3 files changed +22
-1
lines changed
Misc/NEWS.d/next/Core and Builtins Expand file tree Collapse file tree 3 files changed +22
-1
lines changed Original file line number Diff line number Diff 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.
45054521if hasattr (os , "_fspath" ):
Original file line number Diff line number Diff line change 1+ Fix crash when :func: `os.fork ` is called with an active non-default
2+ memory allocator.
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments