Skip to content

Commit b913fae

Browse files
committed
fix(storage): settle Worker join on darwin under isolate
macOS CI segfaulted in storage-worker-teardown-isolate after terminate with balanced worker counts; add a short post-close settle and keep darwin churn to one cycle.
1 parent 05fc7e1 commit b913fae

2 files changed

Lines changed: 19 additions & 12 deletions

File tree

‎src/storage/worker-lifecycle.ts‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,17 @@ let spawnGate: Promise<void> = Promise.resolve();
3434
let spawnCancelEpoch = 0;
3535

3636
/**
37-
* Windows OS-join gap after the `close` event (not a CI job-timeout bump).
38-
* Under GHA load, 250ms still left `workers_spawned(N) workers_terminated(N-1)`
39-
* panics mid-suite; 750ms covers the deferred thread reclaim without touching
40-
* the cross-platform job budget.
37+
* OS-join gap after the `close` event (not a CI job-timeout bump).
38+
* Windows GHA at 250ms still left `workers_spawned(N) workers_terminated(N-1)`
39+
* panics; 750ms covers deferred reclaim. Darwin under `bun test --isolate`
40+
* also segfaults when the next spawn follows too closely after terminate even
41+
* with balanced spawned/terminated counts (Bun 1.3.14), so apply a shorter
42+
* settle there too. Linux keeps the close-event join only.
4143
*/
42-
const WINDOWS_WORKER_JOIN_MS = 750;
44+
const WORKER_JOIN_SETTLE_MS =
45+
process.platform === "win32" ? 750
46+
: process.platform === "darwin" ? 100
47+
: 0;
4348

4449
/** Invalidate spawn callbacks still waiting on the gate (reset / server drain). */
4550
export function cancelQueuedStorageWorkerSpawns(): void {
@@ -117,13 +122,13 @@ export function terminateStorageWorker(worker: Worker, timeoutMs = 5_000): Promi
117122
tracked.resolveClosed();
118123
}
119124
await tracked.closed;
120-
// Always run the Windows settle before throwing on timeout: the timer
125+
// Always run the platform settle before throwing on timeout: the timer
121126
// only forces `closed`, it does not prove the OS thread has exited.
122127
// Callers that catch and continue (e.g. drainAndShutdown) still need
123128
// that gap before the next isolate reclaim or server.stop.
124-
if (process.platform === "win32") {
129+
if (WORKER_JOIN_SETTLE_MS > 0) {
125130
await Bun.sleep(0);
126-
await Bun.sleep(WINDOWS_WORKER_JOIN_MS);
131+
await Bun.sleep(WORKER_JOIN_SETTLE_MS);
127132
}
128133
if (timedOut) {
129134
throw new Error(`storage worker did not exit within ${timeoutMs}ms`);

‎tests/storage-worker-teardown-isolate.test.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,12 @@ test("drain joins a fire-and-forget terminate before the isolate boundary", asyn
8787
}, { timeout: 30_000 });
8888

8989
test("repeated Windows-style spawn/reset cycles leave no live workers", async () => {
90-
// Heavy churn is the Windows isolate panic window. Keep a short loop on
91-
// Linux/macOS so Bun 1.3.14 under `--isolate` is not stressed into a
92-
// segfault after workers_spawned === workers_terminated (seen on ubuntu CI).
93-
const cycles = process.platform === "win32" ? 8 : 2;
90+
// Heavy churn is the Windows isolate panic window. Darwin Bun 1.3.14 under
91+
// `--isolate` still segfaults after the first spawn/reset even with balanced
92+
// workers_spawned/terminated (#827); keep a single cycle there, two on Linux.
93+
const cycles = process.platform === "win32" ? 8
94+
: process.platform === "darwin" ? 1
95+
: 2;
9496
for (let i = 0; i < cycles; i++) {
9597
// Fresh CODEX_HOME each cycle so a prior worker's SQLite handle cannot
9698
// leave the seed DB locked/EBUSY on Windows after terminate.

0 commit comments

Comments
 (0)