Skip to content

Commit c50b736

Browse files
committed
fix(opencode): answer a failed TUI worker request instead of hanging
A method behind the TUI worker's RPC channel that threw never got a reply, so the caller's promise never settled. The TUI waits on that channel before it draws anything, so for example `opencode --port 4096` with port 4096 already taken stayed on a blank screen forever. The worker now answers a failed request with the error's message, or its cause's when it has none (a taken port fails with an empty ServeError whose cause names the port), and the call rejects with it, so opencode exits with that message. The upstream PR branch rpc-error-reply carries the same change (2cef65a). AI-Tool: opencode 1.18.34-vt-128-907b3bc518 AI-Model: anthropic/claude-opus-5-5 AI-Variant: max AI-Platform: linux AI-Harness: Vibeterm a2d1757 AI-Session-ID: ses_eebf9a83fffeIQQwW9gVFo5jaO
1 parent 22e6326 commit c50b736

3 files changed

Lines changed: 79 additions & 10 deletions

File tree

‎packages/opencode/src/util/rpc.ts‎

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
1+
import { errorMessage } from "@/util/error"
2+
13
type Definition = {
24
[method: string]: (input: any) => any
35
}
46

57
export function listen(rpc: Definition) {
68
onmessage = async (evt) => {
79
const parsed = JSON.parse(evt.data)
8-
if (parsed.type === "rpc.request") {
9-
const result = await rpc[parsed.method](parsed.input)
10-
postMessage(JSON.stringify({ type: "rpc.result", result, id: parsed.id }))
11-
}
10+
if (parsed.type !== "rpc.request") return
11+
// A request that throws must still be answered, or its caller waits forever.
12+
const reply = await Promise.resolve()
13+
.then(() => rpc[parsed.method](parsed.input))
14+
.then(
15+
(result) => ({ type: "rpc.result", result, id: parsed.id }),
16+
(error) => ({
17+
type: "rpc.result",
18+
// Some failures keep their text on the cause: a taken --port rejects with an empty ServeError.
19+
error: errorMessage(error instanceof Error && !error.message && error.cause ? error.cause : error),
20+
id: parsed.id,
21+
}),
22+
)
23+
postMessage(JSON.stringify(reply))
1224
}
1325
}
1426

@@ -20,16 +32,17 @@ export function client<T extends Definition>(target: {
2032
postMessage: (data: string) => void | null
2133
onmessage: ((this: Worker, ev: MessageEvent<any>) => any) | null
2234
}) {
23-
const pending = new Map<number, (result: any) => void>()
35+
const pending = new Map<number, { resolve: (result: any) => void; reject: (error: Error) => void }>()
2436
const listeners = new Map<string, Set<(data: any) => void>>()
2537
let id = 0
2638
target.onmessage = async (evt) => {
2739
const parsed = JSON.parse(evt.data)
2840
if (parsed.type === "rpc.result") {
29-
const resolve = pending.get(parsed.id)
30-
if (resolve) {
31-
resolve(parsed.result)
41+
const request = pending.get(parsed.id)
42+
if (request) {
3243
pending.delete(parsed.id)
44+
if (typeof parsed.error === "string") request.reject(new Error(parsed.error))
45+
else request.resolve(parsed.result)
3346
}
3447
}
3548
if (parsed.type === "rpc.event") {
@@ -44,8 +57,8 @@ export function client<T extends Definition>(target: {
4457
return {
4558
call<Method extends keyof T>(method: Method, input: Parameters<T[Method]>[0]): Promise<ReturnType<T[Method]>> {
4659
const requestId = id++
47-
return new Promise((resolve) => {
48-
pending.set(requestId, resolve)
60+
return new Promise((resolve, reject) => {
61+
pending.set(requestId, { resolve, reject })
4962
target.postMessage(JSON.stringify({ type: "rpc.request", method, input, id: requestId }))
5063
})
5164
},
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Rpc } from "../../src/util/rpc"
2+
3+
export const rpc = {
4+
echo(input: string) {
5+
return input
6+
},
7+
throws() {
8+
throw new Error("thrown in the worker")
9+
},
10+
async rejects() {
11+
throw new Error("rejected in the worker")
12+
},
13+
wrapped() {
14+
throw new Error("", { cause: new Error("the cause says why") })
15+
},
16+
}
17+
18+
Rpc.listen(rpc)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { afterAll, describe, expect, test } from "bun:test"
2+
import { Rpc } from "../../src/util/rpc"
3+
import type { rpc } from "../fixture/rpc-worker"
4+
5+
const worker = new Worker(new URL("../fixture/rpc-worker.ts", import.meta.url))
6+
const client = Rpc.client<typeof rpc>(worker)
7+
8+
afterAll(() => worker.terminate())
9+
10+
// The protocol has no timeout of its own, so a call is raced against one here:
11+
// an unanswered request then fails the assertion instead of stalling the test.
12+
const settle = (call: Promise<unknown>) =>
13+
Promise.race([
14+
call.then(
15+
(value) => ({ value }),
16+
(error: Error) => ({ error: error.message }),
17+
),
18+
Bun.sleep(2000).then(() => "no reply"),
19+
])
20+
21+
describe("util.rpc", () => {
22+
test("resolves with the worker's result", async () => {
23+
expect(await settle(client.call("echo", "hello"))).toEqual({ value: "hello" })
24+
})
25+
26+
test("rejects when the worker's method throws", async () => {
27+
expect(await settle(client.call("throws", undefined))).toEqual({ error: "thrown in the worker" })
28+
})
29+
30+
test("rejects with the cause's message when the error has none of its own", async () => {
31+
expect(await settle(client.call("wrapped", undefined))).toEqual({ error: "the cause says why" })
32+
})
33+
34+
test("rejects when the worker's method rejects, and keeps serving", async () => {
35+
expect(await settle(client.call("rejects", undefined))).toEqual({ error: "rejected in the worker" })
36+
expect(await settle(client.call("echo", "still there"))).toEqual({ value: "still there" })
37+
})
38+
})

0 commit comments

Comments
 (0)