Repository navigation
Commit 0011d08
committed
feat: Warn when the SDK is used in a forked process without postfork
Threads do not survive a fork. A child process that inherits an LDClient has
no stream thread, no event dispatcher thread, and no timer tasks, so flag
values freeze, no analytics events are sent, and close() can block forever.
The SDK gave no signal. This adds a warning, logged once per process, on the
first use of the client in a process whose pid differs from the pid recorded
when the client was created or when postfork() was last called.
Trigger points: variation, variation_detail, migration_variation (all three
pass through the private hook wrapper, so one check covers them),
all_flags_state, identify, track, track_migration_op, flush, and close.
The warning is skipped when a fork breaks nothing: offline mode, or LDD mode
with events disabled.
Implementation choice: os.getpid() compared with the stored pid on each
trigger. A benchmark with the sdk-stress-tester showed no regression above
noise, so os.register_at_fork() was not needed.
The "already warned" state is keyed by the pid in which the warning was
logged, not stored as a boolean, so a grandchild process warns again. The
state is a dict written with dict.setdefault, which is atomic in CPython,
rather than a threading.Lock. A lock held at the moment of a fork is copied
into the child in the locked state and the child would block forever on its
first evaluation, which is the class of failure this change is meant to
expose, not cause.
AsyncLDClient is unchanged. It has no postfork(), the sync/async parity test
already lists postfork as sync-only, and forking a process that owns a
running asyncio event loop is not a supported pattern. Any fork-related
support for the async client would need its own design.
Mirrors launchdarkly/ruby-server-sdk#430.1 parent 2e095c5 commit 0011d08
2 files changed
Lines changed: 317 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | | - | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
50 | 51 | | |
51 | 52 | | |
52 | 53 | | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
53 | 59 | | |
54 | 60 | | |
55 | 61 | | |
| |||
78 | 84 | | |
79 | 85 | | |
80 | 86 | | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
81 | 90 | | |
82 | 91 | | |
83 | 92 | | |
| |||
100 | 109 | | |
101 | 110 | | |
102 | 111 | | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
103 | 116 | | |
104 | 117 | | |
| 118 | + | |
| 119 | + | |
105 | 120 | | |
106 | 121 | | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
107 | 156 | | |
108 | 157 | | |
109 | 158 | | |
| |||
220 | 269 | | |
221 | 270 | | |
222 | 271 | | |
| 272 | + | |
223 | 273 | | |
224 | 274 | | |
225 | 275 | | |
| |||
247 | 297 | | |
248 | 298 | | |
249 | 299 | | |
| 300 | + | |
250 | 301 | | |
251 | 302 | | |
252 | 303 | | |
| |||
271 | 322 | | |
272 | 323 | | |
273 | 324 | | |
| 325 | + | |
274 | 326 | | |
275 | 327 | | |
276 | 328 | | |
| |||
289 | 341 | | |
290 | 342 | | |
291 | 343 | | |
292 | | - | |
| 344 | + | |
293 | 345 | | |
294 | 346 | | |
295 | 347 | | |
| |||
331 | 383 | | |
332 | 384 | | |
333 | 385 | | |
| 386 | + | |
334 | 387 | | |
335 | 388 | | |
336 | 389 | | |
| |||
480 | 533 | | |
481 | 534 | | |
482 | 535 | | |
| 536 | + | |
| 537 | + | |
483 | 538 | | |
484 | 539 | | |
485 | 540 | | |
| |||
572 | 627 | | |
573 | 628 | | |
574 | 629 | | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
575 | 634 | | |
576 | 635 | | |
577 | 636 | | |
| |||
0 commit comments