cpython/Lib/multiprocessing
Gregory P. Smith 3a7df632c9
gh-146313: Fix multiprocessing ResourceTracker deadlock after os.fork() (GH-146316)
`ResourceTracker.__del__` (added in gh-88887 circa Python 3.12) calls
os.waitpid(pid, 0) which blocks indefinitely if a process created via os.fork()
still holds the tracker pipe's write end. The tracker never sees EOF, never
exits, and the parent hangs at interpreter shutdown.

Fix with two layers:

- **At-fork handler.** An os.register_at_fork(after_in_child=...)
  handler closes the inherited pipe fd in the child unless a preserve
  flag is set. popen_fork.Popen._launch() sets the flag before its
  fork so mp.Process(fork) children keep the fd and reuse the parent's
  tracker (preserving gh-80849). Raw os.fork() children close the fd,
  letting the parent reap promptly.

- **Timeout safety-net.** _stop_locked() gains a wait_timeout
  parameter. When called from `__del__`, it polls with WNOHANG using
  exponential backoff for up to 1 second instead of blocking
  indefinitely. The at-fork handler makes this unreachable in
  well-behaved paths; it remains for abnormal shutdowns.

Co-authored-by: Itamar Oren <itamarost@gmail.com>
2026-04-11 23:06:19 -07:00
..
dummy gh-138813: Fix mutable default kwargs={} in multiprocessing BaseProcess and DummyProcess to use None (GH-138814) 2025-09-17 14:45:52 -07:00
__init__.py
connection.py gh-145587: fix busy loop in multiprocessing.connection.wait on Windows (GH-145597) 2026-03-11 13:08:48 +01:00
context.py gh-140814: Fix freeze_support() setting start method as side effect (GH-144608) 2026-03-15 22:50:19 -07:00
forkserver.py gh-144503: Pass sys.argv to forkserver as real argv elements (GH-148194) 2026-04-06 22:41:02 -07:00
heap.py gh-137928: remove redundant size validation in multiprocessing.heap (GH-137929) 2025-11-12 00:36:43 +00:00
managers.py
pool.py
popen_fork.py gh-146313: Fix multiprocessing ResourceTracker deadlock after os.fork() (GH-146316) 2026-04-11 23:06:19 -07:00
popen_forkserver.py
popen_spawn_posix.py
popen_spawn_win32.py
process.py gh-138813: Fix mutable default kwargs={} in multiprocessing BaseProcess and DummyProcess to use None (GH-138814) 2025-09-17 14:45:52 -07:00
queues.py gh-133146: Add the old public get_value method to documentation and refactor code. (GH-133301) 2025-11-29 22:12:48 +00:00
reduction.py
resource_sharer.py
resource_tracker.py gh-146313: Fix multiprocessing ResourceTracker deadlock after os.fork() (GH-146316) 2026-04-11 23:06:19 -07:00
shared_memory.py
sharedctypes.py
spawn.py gh-109263: Start process from spawn context in multiprocessing no longer have side effect (GH-135813) 2025-12-23 19:28:32 +00:00
synchronize.py gh-133146: Add the old public get_value method to documentation and refactor code. (GH-133301) 2025-11-29 22:12:48 +00:00
util.py gh-140734: fix off-by-one error when comparing to _SUN_PATH_MAX (#140903) 2025-11-07 09:54:02 +00:00