Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions packages/opencode/src/cli/tui/worker-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ export interface WorkerProcessOptions {
spawn?: Spawn
/** Thread-mode worker factory (tests). Defaults to the global Worker. */
spawnWorker?: (target: string, init: { env?: Record<string, string> }) => ThreadWorkerLike
/** Thread-mode bootstrap watchdog timeout in ms (tests). Default 10_000. */
bootstrapTimeoutMs?: number
}

/**
Expand Down Expand Up @@ -320,15 +322,40 @@ export function createThreadWorker(target: string, options: WorkerProcessOptions
transport.terminate = async () => {
// Bun's Worker.terminate resolves after the thread finishes flushing;
// awaiting it keeps in-flight RPC emits from being cut off (#401).
clearTimeout(bootTimer)
await worker.terminate()
finish({ code: 0, signal: null })
}
transport.waitForRestart = () => Promise.resolve()

// Bootstrap watchdog (#398): the thread worker's identity depends on
// SILVERCODE_TUI_WORKER surviving into the thread's env, which Bun's
// Worker env option does not guarantee. If the thread never registers its
// RPC listener it also never emits a message, and the parent's first RPC
// call would hang forever. Any first message (the worker.ready beacon or
// a global event) proves bootstrap; otherwise fail fast with a clear
// error instead of a silent hang.
let booted = false
const bootTimer = setTimeout(() => {
if (booted) return
transport.onclose?.(
new Error(
"Bun worker bootstrap timed out: the thread never registered its RPC listener (worker identity env did not propagate)",
),
)
void worker.terminate()
finish({ code: 1, signal: null })
}, options.bootstrapTimeoutMs ?? 10_000)

worker.onmessage = (evt) => {
if (!booted) {
booted = true
clearTimeout(bootTimer)
}
transport.onmessage?.({ data: evt.data as string } as MessageEvent<string>)
}
worker.onerror = (event) => {
clearTimeout(bootTimer)
const message = event instanceof MessageEvent ? String(event.data ?? "unknown worker error") : "unknown worker error"
transport.onclose?.(new Error(`Bun worker exited with error: ${message}`))
finish({ code: 1, signal: null })
Expand Down
7 changes: 7 additions & 0 deletions packages/opencode/src/cli/tui/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,5 +228,12 @@ if (isTuiWorker) {
}
} else {
stopRpcListener = Rpc.listen(rpc)
// Bootstrap beacon (#398): a thread worker's identity rests entirely on
// SILVERCODE_TUI_WORKER surviving into the thread's env, which Bun's
// Worker env option does not guarantee. Emitting ready proves to the
// parent that the RPC listener registered; the parent fails fast with a
// clear error when no message ever arrives instead of hanging on the
// first RPC call.
Rpc.emit("worker.ready", {})
}
}
65 changes: 65 additions & 0 deletions packages/opencode/test/cli/worker-process.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,4 +322,69 @@ describe("TUI thread worker", () => {
await Bun.sleep(0)
expect(exits).toHaveLength(1)
})

test("fails fast when the thread never bootstraps its RPC listener (#398)", async () => {
const fake = {
onmessage: null as null | ((event: { data: unknown }) => void),
onerror: null as null | ((event: unknown) => void),
terminated: false,
postMessage() {},
terminate() {
this.terminated = true
},
}
const closes: Error[] = []
const exits: WorkerExit[] = []
const worker = createThreadWorker("worker.ts", {
spawnWorker: () => fake as never,
log: () => {},
bootstrapTimeoutMs: 30,
onExit: (exit) => {
exits.push(exit)
},
})
worker.onclose = (error) => {
closes.push(error)
}

await expect(worker.closed).resolves.toEqual({ code: 1, signal: null })
expect(closes).toHaveLength(1)
expect(closes[0]!.message).toContain("bootstrap timed out")
expect(fake.terminated).toBe(true)
await Bun.sleep(0)
expect(exits).toEqual([{ code: 1, signal: null }])
})

test("a first message marks the thread booted and silences the watchdog (#398)", async () => {
const fake = {
onmessage: null as null | ((event: { data: unknown }) => void),
onerror: null as null | ((event: unknown) => void),
terminated: false,
postMessage() {},
terminate() {
this.terminated = true
},
}
const closes: Error[] = []
const messages: string[] = []
const worker = createThreadWorker("worker.ts", {
spawnWorker: () => fake as never,
log: () => {},
bootstrapTimeoutMs: 30,
})
worker.onclose = (error) => {
closes.push(error)
}
worker.onmessage = (evt) => {
messages.push(String(evt.data))
}

fake.onmessage?.({ data: "{\"type\":\"rpc.event\",\"event\":\"worker.ready\",\"data\":{}}" })
await Bun.sleep(80)
expect(messages).toHaveLength(1)
expect(closes).toHaveLength(0)
expect(fake.terminated).toBe(false)
await worker.terminate()
await expect(worker.closed).resolves.toEqual({ code: 0, signal: null })
})
})
Loading