66 * `panic: Internal assertion failure` with `workers_spawned(N)
77 * workers_terminated(N-1)` on Windows and kills the whole run.
88 *
9+ * A second Bun 1.3.14 failure mode is a post-suite segfault with a *balanced*
10+ * `workers_spawned === workers_terminated` count (exit 133 / Trace/BPT on
11+ * macOS Silicon CI). That is a runtime crash after green assertions — keep the
12+ * heavy churn on win32, and leave darwin with a single-cycle proof plus a
13+ * brief settle before the isolate file boundary.
14+ *
915 * These cases hammer the exact failure window: fire-and-forget terminate must
1016 * still be joinable by drain, and repeated spawn → reset cycles must leave the
1117 * registry empty before the next isolate boundary.
1218 */
13- import { afterEach , beforeEach , expect , test } from "bun:test" ;
19+ import { afterAll , afterEach , beforeEach , expect , test } from "bun:test" ;
1420import { mkdirSync , mkdtempSync , rmSync , writeFileSync } from "node:fs" ;
1521import { tmpdir } from "node:os" ;
1622import { join } from "node:path" ;
@@ -32,6 +38,17 @@ let isolatedCodexHome: IsolatedCodexHome | null = null;
3238let testDir = "" ;
3339let previousHome : string | undefined ;
3440
41+ /** Spawn/reset iterations for the heavy churn case — platform-stressed carefully. */
42+ function workerChurnCyclesForIsolate ( ) : number {
43+ if ( process . platform === "win32" ) return 8 ;
44+ // Two cycles still segfaulted Bun 1.3.14 on macOS Silicon under `--isolate`
45+ // after a green suite (balanced worker counts, exit 133). One cycle keeps a
46+ // real spawn/reset proof without the churn that trips the runtime.
47+ if ( process . platform === "darwin" ) return 1 ;
48+ // Linux: short loop — eight cycles segfaulted with balanced counts on ubuntu CI.
49+ return 2 ;
50+ }
51+
3552function seedArchived ( codexHome : string ) : void {
3653 mkdirSync ( join ( codexHome , "archived_sessions" ) , { recursive : true } ) ;
3754 writeFileSync ( join ( codexHome , "archived_sessions" , "rollout-old.jsonl" ) , "o" . repeat ( 100 ) ) ;
@@ -59,6 +76,14 @@ afterEach(async () => {
5976 testDir = "" ;
6077} ) ;
6178
79+ afterAll ( async ( ) => {
80+ await drainStorageWorkers ( ) ;
81+ // macOS Silicon + Bun 1.3.14: even with balanced worker counts the isolate
82+ // realm reclaim can segfault if the OS join has not finished. Brief settle
83+ // only — not a CI timeout bump.
84+ if ( process . platform === "darwin" ) await Bun . sleep ( 250 ) ;
85+ } ) ;
86+
6287async function waitForLiveWorker ( timeoutMs = 10_000 ) : Promise < void > {
6388 const deadline = Date . now ( ) + timeoutMs ;
6489 while ( Date . now ( ) < deadline ) {
@@ -87,10 +112,7 @@ test("drain joins a fire-and-forget terminate before the isolate boundary", asyn
87112} , { timeout : 30_000 } ) ;
88113
89114test ( "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 ;
115+ const cycles = workerChurnCyclesForIsolate ( ) ;
94116 for ( let i = 0 ; i < cycles ; i ++ ) {
95117 // Fresh CODEX_HOME each cycle so a prior worker's SQLite handle cannot
96118 // leave the seed DB locked/EBUSY on Windows after terminate.
@@ -105,10 +127,18 @@ test("repeated Windows-style spawn/reset cycles leave no live workers", async ()
105127 expect ( started . accepted ) . toBe ( true ) ;
106128 await waitForLiveWorker ( ) ;
107129 await resetStorageCleanupPolicyJobForTestsAsync ( ) ;
130+ await drainStorageWorkers ( ) ;
108131 expect ( liveStorageWorkerCount ( ) ) . toBe ( 0 ) ;
109132 }
110133} , { timeout : 60_000 } ) ;
111134
135+ test ( "isolate worker churn stays platform-capped" , ( ) => {
136+ const cycles = workerChurnCyclesForIsolate ( ) ;
137+ if ( process . platform === "win32" ) expect ( cycles ) . toBe ( 8 ) ;
138+ else if ( process . platform === "darwin" ) expect ( cycles ) . toBe ( 1 ) ;
139+ else expect ( cycles ) . toBe ( 2 ) ;
140+ } ) ;
141+
112142test ( "terminateStorageWorker is joinable and idempotent across callers" , async ( ) => {
113143 setStorageCleanupPolicyJobTestHooks ( { blockMs : 500 } ) ;
114144 seedArchived ( isolatedCodexHome ! . path ) ;
0 commit comments