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):
4500
4500
self .assertEqual (times .elapsed , 0 )
4501
4501
4502
4502
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
+
4503
4519
# Only test if the C version is provided, otherwise TestPEP519 already tested
4504
4520
# the pure Python implementation.
4505
4521
if 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)
148
148
_PyMem_SetDefaultAllocator (PYMEM_DOMAIN_RAW , & old_alloc );
149
149
150
150
int reinit_interp = _PyThread_at_fork_reinit (& runtime -> interpreters .mutex );
151
- int reinit_main_id = _PyThread_at_fork_reinit (& runtime -> interpreters .main -> id_mutex );
152
151
int reinit_xidregistry = _PyThread_at_fork_reinit (& runtime -> xidregistry .mutex );
153
152
int reinit_unicode_ids = _PyThread_at_fork_reinit (& runtime -> unicode_ids .lock );
154
153
155
154
PyMem_SetAllocator (PYMEM_DOMAIN_RAW , & old_alloc );
156
155
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
+
157
160
if (reinit_interp < 0
158
161
|| reinit_main_id < 0
159
162
|| reinit_xidregistry < 0
You can’t perform that action at this time.
0 commit comments