Skip to content

Commit b471c2b

Browse files
fix(opencode): catch completed MCP browser launcher failures (anomalyco#51538)
Co-authored-by: rekram1-node <[email protected]>
1 parent a42f393 commit b471c2b

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

‎packages/opencode/src/mcp/browser.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@ const layer = Layer.succeed(
2222
clearTimeout(timer)
2323
resume(Effect.fail(error))
2424
})
25-
subprocess.on("exit", (code) => {
25+
const onExit = (code: number | null) => {
2626
if (code === null || code === 0) return
2727
clearTimeout(timer)
2828
resume(Effect.fail(new Error(`Browser open failed with exit code ${code}`)))
29-
})
29+
}
30+
subprocess.on("exit", onExit)
31+
// On Windows and WSL, open() can return only after the launcher has exited.
32+
onExit(subprocess.exitCode)
3033
})
3134
}),
3235
}),
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { expect, mock, test } from "bun:test"
2+
import { spawn } from "node:child_process"
3+
import { once } from "node:events"
4+
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
5+
import { Effect } from "effect"
6+
7+
await mock.module("@opencode-ai/core/open", () => ({
8+
openUrl: async (url: string) => {
9+
const code = new URL(url).pathname === "/successful" ? 0 : 23
10+
const subprocess = spawn(process.execPath, ["-e", `process.exit(${code})`], { stdio: "ignore" })
11+
await once(subprocess, "close")
12+
return subprocess
13+
},
14+
}))
15+
16+
const { McpBrowser } = await import("../../src/mcp/browser")
17+
18+
test("reports a browser launcher that exited before openUrl returned", async () => {
19+
const error = await Effect.runPromise(
20+
Effect.gen(function* () {
21+
const browser = yield* McpBrowser.Service
22+
return yield* Effect.flip(browser.open("https://example.com/authorize"))
23+
}).pipe(Effect.provide(LayerNode.compile(McpBrowser.node))),
24+
)
25+
26+
expect(error.message).toBe("Browser open failed with exit code 23")
27+
})
28+
29+
test("accepts a browser launcher that exited successfully before openUrl returned", async () => {
30+
await Effect.runPromise(
31+
Effect.gen(function* () {
32+
const browser = yield* McpBrowser.Service
33+
yield* browser.open("https://example.com/successful")
34+
}).pipe(Effect.provide(LayerNode.compile(McpBrowser.node))),
35+
)
36+
})

0 commit comments

Comments
 (0)