]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
[3.14] gh-146313: Fix multiprocessing ResourceTracker deadlock after os.fork() (GH...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sun, 12 Apr 2026 17:08:23 +0000 (19:08 +0200)
committerGitHub <noreply@github.com>
Sun, 12 Apr 2026 17:08:23 +0000 (10:08 -0700)
commit7ecd85c03219650c30d91d48e5a4a7fcb040148b
tree84aeabe6652c30f1f24309759bc71b1428a25897
parenta89b2419e063b19c6d59428c40cd1ab58606e0b2
[3.14] gh-146313: Fix multiprocessing ResourceTracker deadlock after os.fork() (GH-146316) (#148425)

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.
(cherry picked from commit 3a7df632c96eb6c5de12fac08d1da42df9e25334)

Co-authored-by: Gregory P. Smith <68491+gpshead@users.noreply.github.com>
Co-authored-by: Itamar Oren <itamarost@gmail.com>
Lib/multiprocessing/popen_fork.py
Lib/multiprocessing/resource_tracker.py
Lib/test/_test_multiprocessing.py
Misc/NEWS.d/next/Library/2026-03-22-23-42-22.gh-issue-146313.RtDeAd.rst [new file with mode: 0644]