Commit e80d661
authored
[pipeline] Stop unstopped pipelines at interpreter exit to avoid hang (#1591)
A program that keeps a strong reference to a `Pipeline` and never calls
`stop()` (e.g. a training framework that stores the dataloader for the
whole run) can hang at interpreter exit. The pipeline's background
event-loop thread is non-daemon, so CPython joins it inside
`threading._shutdown()` during finalization; because nothing stopped the
pipeline, that join blocks forever. The existing `weakref.finalize`
cleanup runs too late: it is an `atexit`-module callback (a later
finalization phase), after non-daemon threads have already been joined.
Fix: register a per-pipeline shutdown hook via
`threading._register_atexit` (the same private mechanism
`concurrent.futures` relies on) so it runs inside
`threading._shutdown()` — before non-daemon threads and child processes
are joined — and stops the pipeline first. The hook is a thin weakref
wrapper around the existing `_stop_impl` (the single stop-with-timeout
helper, shared with the GC finalizer), registered in `Pipeline.start()`
after the pipeline's own threads/processes are up, so it lands after the
stdlib's atexit hooks and — being LIFO — runs before them. It holds only
a weak reference so it never keeps the pipeline alive, and is a safe
no-op once the pipeline is stopped or collected. A regular
`atexit.register` hook was verified insufficient: it runs after the
non-daemon thread join and still hangs.
Also make the subprocess-region bridge's blocking queue put
interruptible. A `.to(...)` region feeder parked on a full worker queue
(backpressure once the consumer stopped) cannot be released by pool
teardown, so at exit it is joined — and hangs — by `concurrent.futures`.
It now polls a teardown event and exits promptly. This also fixes a
pre-existing hang that reproduced on an explicit `stop()`.
No public API change; holding a `Pipeline` reference and relying on GC
is unchanged.1 parent cca926b commit e80d661
6 files changed
Lines changed: 527 additions & 61 deletions
File tree
- docs/source/getting_started
- src/spdl/pipeline
- _components
- tests/pipeline
- tools/skills/authoring
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
66 | 66 | | |
67 | 67 | | |
68 | 68 | | |
69 | | - | |
70 | | - | |
71 | | - | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
72 | 76 | | |
73 | 77 | | |
74 | 78 | | |
| |||
97 | 101 | | |
98 | 102 | | |
99 | 103 | | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
109 | 127 | | |
110 | 128 | | |
111 | 129 | | |
112 | 130 | | |
113 | | - | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
114 | 135 | | |
115 | 136 | | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
| 137 | + | |
120 | 138 | | |
121 | | - | |
122 | | - | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
123 | 143 | | |
124 | | - | |
125 | | - | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
126 | 151 | | |
127 | | - | |
| 152 | + | |
128 | 153 | | |
129 | | - | |
130 | | - | |
| 154 | + | |
131 | 155 | | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
139 | 168 | | |
140 | | - | |
141 | | - | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
142 | 177 | | |
143 | | - | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
144 | 183 | | |
145 | 184 | | |
146 | | - | |
| 185 | + | |
147 | 186 | | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
153 | 193 | | |
154 | 194 | | |
155 | 195 | | |
| |||
194 | 234 | | |
195 | 235 | | |
196 | 236 | | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
| 42 | + | |
42 | 43 | | |
43 | 44 | | |
44 | 45 | | |
| |||
78 | 79 | | |
79 | 80 | | |
80 | 81 | | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
81 | 89 | | |
82 | 90 | | |
83 | 91 | | |
84 | 92 | | |
85 | 93 | | |
86 | 94 | | |
87 | | - | |
88 | | - | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
89 | 104 | | |
90 | 105 | | |
91 | 106 | | |
| |||
123 | 138 | | |
124 | 139 | | |
125 | 140 | | |
| 141 | + | |
126 | 142 | | |
127 | 143 | | |
128 | 144 | | |
| |||
165 | 181 | | |
166 | 182 | | |
167 | 183 | | |
168 | | - | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
169 | 187 | | |
170 | 188 | | |
171 | 189 | | |
172 | 190 | | |
173 | 191 | | |
174 | | - | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
175 | 196 | | |
176 | 197 | | |
177 | 198 | | |
| |||
234 | 255 | | |
235 | 256 | | |
236 | 257 | | |
| 258 | + | |
237 | 259 | | |
238 | 260 | | |
239 | 261 | | |
| |||
251 | 273 | | |
252 | 274 | | |
253 | 275 | | |
254 | | - | |
| 276 | + | |
255 | 277 | | |
256 | 278 | | |
257 | 279 | | |
| |||
268 | 290 | | |
269 | 291 | | |
270 | 292 | | |
271 | | - | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
272 | 296 | | |
273 | 297 | | |
274 | 298 | | |
| |||
352 | 376 | | |
353 | 377 | | |
354 | 378 | | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
355 | 383 | | |
356 | 384 | | |
357 | 385 | | |
358 | 386 | | |
359 | 387 | | |
360 | | - | |
| 388 | + | |
361 | 389 | | |
362 | 390 | | |
363 | 391 | | |
| |||
374 | 402 | | |
375 | 403 | | |
376 | 404 | | |
377 | | - | |
| 405 | + | |
378 | 406 | | |
379 | 407 | | |
380 | 408 | | |
| |||
387 | 415 | | |
388 | 416 | | |
389 | 417 | | |
390 | | - | |
391 | | - | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
392 | 422 | | |
0 commit comments