Commit fa02d40
fix: attach ring buffers in the parent for fork producers
`Producer.__call__` attached the five ring-buffer shared-memory segments from
inside the child for every start method. Under `fork` that is not safe:
`SharedMemory(name=..., create=False)` calls
`multiprocessing.resource_tracker.register`, which takes a module-level
`threading.RLock` that CPython does not reinitialize after `fork`. A child that
inherits that lock held by a thread which does not exist in the child blocks on
the attach forever -- no timeout, nothing to interrupt it.
`WorkerBase.__init__` already handles this: it sets `_lazy_init = start_method
!= "fork"` and, for `fork`, initializes logging and attaches the rings in the
parent so the child inherits the mappings. `Producer` never got the same
treatment. It does now, mirroring the worker exactly.
Also logs the attach on both sides, as the worker already does, so a child that
does wedge there says so instead of going silent between two unrelated lines.
Verified on Linux (Python 3.12.13, TensorFlow 2.21.0):
- The new test counts shared-memory attaches performed outside the process that
set the session up. On `main` a fork producer child performs five; with this
change, none. It fails on `main` and passes here, deterministically -- the
deadlock is a race, so the invariant is what gets asserted, not the hang.
- `fork` lane: 7 passed, 1 skipped.
- `inference` + `inference_pipeline` + `v2_4` + `core_py`, non-fork: 352 passed.
- 18 full `fork`-lane runs on this machine (10 unpinned, 8 pinned to 2 cores)
never reproduced the CI hang, so this is not shown to change its rate.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>1 parent d8b52c3 commit fa02d40
3 files changed
Lines changed: 96 additions & 2 deletions
File tree
- src
- birdnet_tests/acoustic_models/inference/session_py
- birdnet/acoustic/inference/core
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| 24 | + | |
24 | 25 | | |
25 | 26 | | |
26 | 27 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
154 | 154 | | |
155 | 155 | | |
156 | 156 | | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
157 | 170 | | |
| 171 | + | |
158 | 172 | | |
159 | 173 | | |
160 | 174 | | |
| |||
168 | 182 | | |
169 | 183 | | |
170 | 184 | | |
| 185 | + | |
171 | 186 | | |
172 | 187 | | |
173 | 188 | | |
| |||
541 | 556 | | |
542 | 557 | | |
543 | 558 | | |
544 | | - | |
| 559 | + | |
| 560 | + | |
545 | 561 | | |
546 | 562 | | |
547 | | - | |
| 563 | + | |
| 564 | + | |
548 | 565 | | |
549 | 566 | | |
550 | 567 | | |
| |||
Lines changed: 76 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
0 commit comments