Skip to content

Commit 44dce33

Browse files
authored
Merge pull request #1179 from lidge-jun/fix/bun-isolate-worker-hangs
fix(ci): harden Bun isolate worker teardown and probe timeouts
2 parents 6d04574 + a53163f commit 44dce33

3 files changed

Lines changed: 32 additions & 15 deletions

File tree

‎src/storage/worker-lifecycle.ts‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111
* time (so we cannot miss `self.close()` / early exit), stays in `liveWorkers`
1212
* until that close settles, and `drainStorageWorkers()` joins every in-flight
1313
* 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).
1817
*/
1918

2019
import { createAdmissionGate, type AdmissionMetrics, type AdmissionReservation } from "../lib/admission";
@@ -52,15 +51,15 @@ let spawnCancelEpoch = 0;
5251
/**
5352
* OS-join gap after the `close` event on platforms where Bun's Worker reclaim
5453
* 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.
5958
*/
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;
6463
}
6564

6665
/** Invalidate spawn callbacks still waiting on the gate (reset / server drain). */
@@ -181,9 +180,10 @@ export function terminateStorageWorker(worker: Worker, timeoutMs = 5_000): Promi
181180
// only forces `closed`, it does not prove the OS thread has exited.
182181
// Callers that catch and continue (e.g. drainAndShutdown) still need
183182
// that gap before the next isolate reclaim or server.stop.
184-
if (needsWorkerOsJoinSettle()) {
183+
const settleMs = storageWorkerOsJoinSettleMs();
184+
if (settleMs > 0) {
185185
await Bun.sleep(0);
186-
await Bun.sleep(WORKER_OS_JOIN_MS);
186+
await Bun.sleep(settleMs);
187187
}
188188
if (timedOut) {
189189
throw new Error(`storage worker did not exit within ${timeoutMs}ms`);

‎tests/claude-dotenv-provenance-transport.test.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { tmpdir } from "node:os";
55
import { join } from "node:path";
66
import { pathToFileURL } from "node:url";
77

8+
const PROBE_TIMEOUT_MS = 3_000;
9+
810
/**
911
* Project dotenv can write environment variables before OpenCodex evaluates,
1012
* but it cannot add the random proof argument emitted by the plain-Node npm
@@ -35,7 +37,13 @@ describe("Node launcher context transport", () => {
3537
delete env.OCX_PRE_BUN_ANTHROPIC_ENV;
3638
if (contextEnv === undefined) delete env.OCX_NODE_LAUNCH_CONTEXT;
3739
else env.OCX_NODE_LAUNCH_CONTEXT = contextEnv;
38-
const result = spawnSync(process.execPath, [probe, ...args], { encoding: "utf8", env });
40+
const result = spawnSync(process.execPath, [probe, ...args], {
41+
encoding: "utf8",
42+
env,
43+
timeout: PROBE_TIMEOUT_MS,
44+
killSignal: "SIGKILL",
45+
});
46+
if (result.error) throw result.error;
3947
expect(result.status).toBe(0);
4048
return JSON.parse(result.stdout) as {
4149
context: { anthropicEnvSlots: string[] } | null;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { expect, test } from "bun:test";
2+
import { storageWorkerOsJoinSettleMs } from "../src/storage/worker-lifecycle";
3+
4+
test("storage worker OS-join settle covers every Bun 1.3.14 isolate platform", () => {
5+
expect(storageWorkerOsJoinSettleMs("win32")).toBe(1_500);
6+
expect(storageWorkerOsJoinSettleMs("darwin")).toBe(250);
7+
expect(storageWorkerOsJoinSettleMs("linux")).toBe(250);
8+
expect(storageWorkerOsJoinSettleMs("freebsd")).toBe(0);
9+
});

0 commit comments

Comments
 (0)