|
11 | 11 | * time (so we cannot miss `self.close()` / early exit), stays in `liveWorkers` |
12 | 12 | * until that close settles, and `drainStorageWorkers()` joins every in-flight |
13 | 13 | * terminate. Spawns are serialized through `withStorageWorkerSpawnGate` so the |
14 | | - * next Worker cannot be created until prior threads have exited. On Windows and |
15 | | - * macOS, a post-close settle covers the OS join gap Bun does not expose |
16 | | - * (Windows unbalanced join panic; macOS Silicon balanced-count segfault under |
17 | | - * `bun test --isolate`). |
| 14 | + * next Worker cannot be created until prior threads have exited. A post-close |
| 15 | + * settle covers the OS/runtime join gap Bun does not expose on Windows, macOS, |
| 16 | + * and Linux (including Bun 1.3.14 isolate crashes and Linux `epoll_ctl` reuse). |
18 | 17 | */ |
19 | 18 |
|
20 | 19 | import { createAdmissionGate, type AdmissionMetrics, type AdmissionReservation } from "../lib/admission"; |
@@ -52,15 +51,15 @@ let spawnCancelEpoch = 0; |
52 | 51 | /** |
53 | 52 | * OS-join gap after the `close` event on platforms where Bun's Worker reclaim |
54 | 53 | * races the isolate/file boundary (not a CI job-timeout bump). |
55 | | - * Windows GHA at 250ms and 750ms still left `workers_spawned(N) |
56 | | - * workers_terminated(N-1)` panics under isolate (seen mid |
57 | | - * `storage-mutation-race` with 11/10). 1500ms covers deferred reclaim under |
58 | | - * stacked policy/restore workers. Darwin uses 250ms. |
| 54 | + * Windows GHA at 250ms and 750ms still left `workers_spawned(N)` |
| 55 | + * `workers_terminated(N-1)` panics under isolate, so Windows keeps 1500ms. |
| 56 | + * Darwin and Linux use a shorter settle for the balanced-count/epoll reclaim |
| 57 | + * window seen on Bun 1.3.14. |
59 | 58 | */ |
60 | | -const WORKER_OS_JOIN_MS = process.platform === "win32" ? 1_500 : 250; |
61 | | - |
62 | | -function needsWorkerOsJoinSettle(): boolean { |
63 | | - return process.platform === "win32" || process.platform === "darwin"; |
| 59 | +export function storageWorkerOsJoinSettleMs(platform = process.platform): number { |
| 60 | + if (platform === "win32") return 1_500; |
| 61 | + if (platform === "darwin" || platform === "linux") return 250; |
| 62 | + return 0; |
64 | 63 | } |
65 | 64 |
|
66 | 65 | /** Invalidate spawn callbacks still waiting on the gate (reset / server drain). */ |
@@ -181,9 +180,10 @@ export function terminateStorageWorker(worker: Worker, timeoutMs = 5_000): Promi |
181 | 180 | // only forces `closed`, it does not prove the OS thread has exited. |
182 | 181 | // Callers that catch and continue (e.g. drainAndShutdown) still need |
183 | 182 | // that gap before the next isolate reclaim or server.stop. |
184 | | - if (needsWorkerOsJoinSettle()) { |
| 183 | + const settleMs = storageWorkerOsJoinSettleMs(); |
| 184 | + if (settleMs > 0) { |
185 | 185 | await Bun.sleep(0); |
186 | | - await Bun.sleep(WORKER_OS_JOIN_MS); |
| 186 | + await Bun.sleep(settleMs); |
187 | 187 | } |
188 | 188 | if (timedOut) { |
189 | 189 | throw new Error(`storage worker did not exit within ${timeoutMs}ms`); |
|
0 commit comments