Commit c974afb
committed
gh-146313: Fix ResourceTracker deadlock after os.fork()
Problem
ResourceTracker.__del__ (added in gh-88887) 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.
Root cause
Three requirements conflict:
- gh-88887 wants the parent to reap the tracker to prevent zombies
- gh-80849 wants mp.Process(fork) children to reuse the parent's
tracker via the inherited pipe fd
- gh-146313 shows the parent can't block in waitpid() if a child
holds the fd -- the tracker won't see EOF until all copies close
Fix
Two layers:
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.
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.
Result
Scenario Before After
Raw os.fork(), parent exits while child alive deadlock ~30ms reap
mp.Process(fork), parent joins then exits ~30ms reap ~30ms reap
mp.Process(fork), parent exits abnormally deadlock 1s bounded wait
No fork (gh-88887 scenario) ~30ms reap ~30ms reap
The at-fork handler makes the timeout unreachable in all well-behaved
paths. The timeout remains as a safety net for abnormal shutdowns.1 parent 4561f64 commit c974afb
File tree
4 files changed
+331
-9
lines changed- Lib
- multiprocessing
- test
- Misc/NEWS.d/next/Library
4 files changed
+331
-9
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
70 | | - | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
71 | 81 | | |
72 | 82 | | |
73 | 83 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| 23 | + | |
23 | 24 | | |
24 | 25 | | |
25 | 26 | | |
| |||
75 | 76 | | |
76 | 77 | | |
77 | 78 | | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
78 | 83 | | |
79 | 84 | | |
80 | 85 | | |
| |||
87 | 92 | | |
88 | 93 | | |
89 | 94 | | |
90 | | - | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
91 | 130 | | |
92 | | - | |
| 131 | + | |
93 | 132 | | |
94 | 133 | | |
95 | | - | |
| 134 | + | |
96 | 135 | | |
97 | 136 | | |
98 | 137 | | |
99 | | - | |
| 138 | + | |
100 | 139 | | |
101 | 140 | | |
102 | 141 | | |
| |||
106 | 145 | | |
107 | 146 | | |
108 | 147 | | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
109 | 152 | | |
110 | 153 | | |
111 | 154 | | |
| |||
122 | 165 | | |
123 | 166 | | |
124 | 167 | | |
125 | | - | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
126 | 192 | | |
127 | 193 | | |
128 | 194 | | |
| |||
308 | 374 | | |
309 | 375 | | |
310 | 376 | | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
311 | 385 | | |
312 | 386 | | |
313 | 387 | | |
314 | 388 | | |
315 | 389 | | |
316 | 390 | | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
317 | 395 | | |
318 | 396 | | |
319 | 397 | | |
| |||
0 commit comments