Repository navigation
fix(tui): keep the Windows Ctrl+C guard active in mini mode (#441) #562
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
270a2b0
aced1c9
5d3edb2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| // Exercise the actual CJS launcher's signal branch without sending a host OS | ||
| // signal. This policy test runs on Windows too; it is not console-event proof. | ||
| import { expect, test } from "bun:test" | ||
| import { EventEmitter } from "node:events" | ||
| import path from "node:path" | ||
| import vm from "node:vm" | ||
|
|
||
| const filename = path.join(import.meta.dir, "../../bin/silvercode") | ||
| const source = await Bun.file(filename).text() | ||
|
|
||
| for (const platform of ["win32", "linux"]) { | ||
| test(`launcher ${platform} signal policy and exit cleanup`, () => { | ||
| const forwarded: string[] = [] | ||
| const child = Object.assign(new EventEmitter(), { kill: (signal: string) => forwarded.push(signal) }) | ||
| const exits: number[] = [] | ||
| const proc = Object.assign(new EventEmitter(), { | ||
| platform, | ||
| arch: "x64", | ||
| argv: ["node", filename], | ||
| env: { OPENCODE_BIN_PATH: "fixture-binary" }, | ||
| exit: (code: number) => exits.push(code), | ||
| }) | ||
| vm.runInNewContext(source, { | ||
| __filename: filename, | ||
| process: proc, | ||
| console, | ||
| require: (name: string) => { | ||
| if (name === "child_process") return { spawn: () => child } | ||
| if (name === "fs") return { realpathSync: () => filename, existsSync: () => true } | ||
| if (name === "path") return path | ||
| if (name === "os") return { platform: () => platform, arch: () => "x64" } | ||
| throw new Error(`unexpected module ${name}`) | ||
| }, | ||
| }) | ||
| expect(proc.listenerCount("SIGINT")).toBe(1) | ||
| proc.emit("SIGINT") | ||
| expect(forwarded).toEqual(platform === "win32" ? [] : ["SIGINT"]) | ||
| proc.emit("SIGTERM") | ||
| proc.emit("SIGHUP") | ||
| expect(forwarded.slice(-2)).toEqual(["SIGTERM", "SIGHUP"]) | ||
| child.emit("exit", 7, null) | ||
| expect(exits).toEqual([7]) | ||
| for (const signal of ["SIGINT", "SIGTERM", "SIGHUP"]) expect(proc.listenerCount(signal)).toBe(0) | ||
| }) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| // The npm launcher is a plain CJS bin that runs before any package code, so it | ||
| // is driven here as a real subprocess against a stub child. OPENCODE_BIN_PATH | ||
| // replaces the platform binary, and the launcher forwards its own arguments to | ||
| // it, so the stub script is passed as an argument to the launcher entry. | ||
| // | ||
| // Windows has no POSIX signals: Ctrl+C is a console event the console delivers | ||
| // to every attached process, and child.kill("SIGINT") maps to TerminateProcess. | ||
| // Forwarding it therefore kills SilverCode before its handlers can run (#441), | ||
| // so the win32 branch must keep listening (the launcher has to survive the | ||
| // event) without forwarding to the child. A real Ctrl+C console event cannot be | ||
| // raised from a test process, so the Windows case runs through a fixture that | ||
| // pins process.platform and asserts the launcher's policy: the child must not | ||
| // observe a forwarded SIGINT. | ||
| import { expect, test } from "bun:test" | ||
| import path from "node:path" | ||
|
|
||
| const launcher = path.join(import.meta.dir, "../../bin/silvercode") | ||
| const win32Launcher = path.join(import.meta.dir, "../fixture/win32-launcher.cjs") | ||
| const child = path.join(import.meta.dir, "../fixture/launcher-child.js") | ||
|
|
||
| type Result = { | ||
| stdout: string | ||
| stderr: string | ||
| exitCode: number | null | ||
| signalCode: string | null | ||
| } | ||
|
|
||
| async function launch(entry: string): Promise<Result> { | ||
| const proc = Bun.spawn({ | ||
| cmd: [process.execPath, entry, child], | ||
| env: { ...process.env, OPENCODE_BIN_PATH: process.execPath }, | ||
| stdin: "ignore", | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }) | ||
|
|
||
| const decoder = new TextDecoder() | ||
| let stdout = "" | ||
| let signalled = false | ||
| const pump = (async () => { | ||
| const reader = proc.stdout.getReader() | ||
| for (;;) { | ||
| const chunk = await reader.read() | ||
| if (chunk.done) return | ||
| stdout += decoder.decode(chunk.value) | ||
| // Signal once the child exists so a slow launcher start cannot turn this | ||
| // into "no child to signal yet". | ||
| if (!signalled && stdout.includes("child:ready")) { | ||
| signalled = true | ||
| proc.kill("SIGINT") | ||
| } | ||
| } | ||
| })() | ||
|
|
||
| const exitCode = await proc.exited | ||
| await pump | ||
| const stderr = await new Response(proc.stderr).text() | ||
| return { stdout, stderr, exitCode, signalCode: proc.signalCode } | ||
| } | ||
|
|
||
| // process.kill(pid, "SIGINT") terminates the target on Windows instead of | ||
| // delivering a signal, so the launcher's signal handling cannot be driven from | ||
| // a test process there. Both cases run on POSIX hosts; the Windows policy is | ||
| // covered by the platform-pinned fixture instead. | ||
| const noSignalDelivery = process.platform === "win32" | ||
|
|
||
| test.skipIf(noSignalDelivery)("forwards SIGINT to the child on POSIX hosts", async () => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WARNING: Both launcher tests are |
||
| const result = await launch(launcher) | ||
|
|
||
| expect(result.stderr).toBe("") | ||
| expect(result.stdout).toContain("child:sigint") | ||
| expect(result.stdout).toContain("child:done") | ||
| expect(result.exitCode).toBe(7) | ||
| }) | ||
|
|
||
| test.skipIf(noSignalDelivery)("does not force-kill the child on win32 (#441)", async () => { | ||
| const result = await launch(win32Launcher) | ||
|
|
||
| expect(result.stderr).toBe("") | ||
| // The console, not the launcher, delivers Ctrl+C to the child on Windows. | ||
| expect(result.stdout).not.toContain("child:sigint") | ||
| expect(result.stdout).toContain("child:done") | ||
| // The launcher itself still has to survive the console event so the child | ||
| // can finish its cleanup. | ||
| expect(result.signalCode).toBeNull() | ||
| expect(result.exitCode).toBe(7) | ||
| }) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| // Stub child for the npm launcher tests. It stays alive long enough to receive | ||
| // a forwarded signal, records any SIGINT it sees, then exits with a sentinel | ||
| // code so the launcher's exit propagation is observable. | ||
| process.on("SIGINT", () => process.stdout.write("child:sigint\n")) | ||
| process.stdout.write("child:ready\n") | ||
| setTimeout(() => { | ||
| process.stdout.write("child:done\n") | ||
| process.exit(7) | ||
| }, 750) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| // Test-only entry for the npm launcher: process.platform is pinned to win32 | ||
| // before the launcher loads so its Windows signal policy runs on every host. | ||
| // The launcher passes process.argv.slice(2) to the child, so the child script | ||
| // follows this file on the command line unchanged. | ||
| Object.defineProperty(process, "platform", { value: "win32" }) | ||
| require("../../bin/silvercode") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SUGGESTION: The guard is installed in
runMini()butRunCommand.handleris also reachable directly viasilvercode run --mini(the handler isEffect.fnandrunMiniis only a thin wrapper). If a future caller invokesRunCommand.handlerwithmini: truewithout going throughrunMini, the guard will not be installed. Consider centralizing the guard installation so both entry points share it, or documenting thatrunMiniis the only supported mini entry point.