Preserve the exit summary when Ctrl+C is pressed again on Windows (#65038)

## Why

Once the TUI restores the terminal, another Ctrl+C becomes a Windows
console event and can terminate Codex before it prints the exit summary,
including the resume hint when available. The npm launcher can also
force-terminate the child by forwarding a Ctrl+C that Windows already
delivered to it.

## What changed

Register a Windows Ctrl+C listener before restoring the terminal and keep
it alive until the CLI or standalone TUI finishes printing the exit
summary. Skip forwarding `SIGINT` from the npm launcher on Windows.

## Testing

Add a Windows regression test that simulates `SIGINT` in the npm launcher
and checks that the child can finish and report its own exit code.

GitOrigin-RevId: cd73a4a2ecc22812bc301787e67c349f7720a93b
This commit is contained in:
Eric Traut
2026-12-01 17:07:18 +00:00
committed by copyberry
parent 3da5a55b5b
commit e14750e0b9
10 changed files with 93 additions and 118 deletions
+5
View File
@@ -261,6 +261,11 @@ child.on("error", (err) => {
// Keep forwarding signals until the child exits so a second signal can
// interrupt a graceful shutdown.
const forwardSignal = (signal) => {
// Windows sends console Ctrl+C to the child too. child.kill("SIGINT")
// would force-terminate it even if it handles the console event.
if (process.platform === "win32" && signal === "SIGINT") {
return;
}
try {
child.kill(signal);
} catch {
+40 -2
View File
@@ -17,10 +17,15 @@ import test from "node:test";
async function installWrapper(directory, executable) {
const arch = process.arch === "arm64" ? "aarch64" : "x86_64";
const target = `${arch}-${process.platform === "darwin" ? "apple-darwin" : "unknown-linux-musl"}`;
const platform = { darwin: "apple-darwin", win32: "pc-windows-msvc" };
const target = `${arch}-${platform[process.platform] ?? "unknown-linux-musl"}`;
const bin = path.join(directory, "vendor", target, "bin");
await mkdir(bin, { recursive: true });
await symlink(executable, path.join(bin, "codex"));
if (process.platform === "win32") {
await copyFile(executable, path.join(bin, "codex.exe"));
} else {
await symlink(executable, path.join(bin, "codex"));
}
await mkdir(path.join(directory, "bin"));
await writeFile(path.join(directory, "package.json"), '{"type":"module"}');
const wrapper = path.join(directory, "bin", "codex.js");
@@ -28,6 +33,39 @@ async function installWrapper(directory, executable) {
return wrapper;
}
test(
"Windows console SIGINT lets the child finish and report its exit code",
{ skip: process.platform !== "win32" },
async () => {
const directory = await mkdtemp(path.join(tmpdir(), "codex-npm-signal-"));
try {
const wrapper = await installWrapper(directory, process.execPath);
// Deliver SIGINT to the launcher without signaling the test runner's console.
const preload = path.join(directory, "interrupt.cjs");
await writeFile(
preload,
'process.once("message", () => { process.emit("SIGINT"); process.send("done"); });',
);
const launcher = spawn(
process.execPath,
[
"--require",
preload,
wrapper,
"-e",
'process.stdin.once("data", () => process.exit(17)); console.log("ready");',
],
{ stdio: ["pipe", "pipe", "inherit", "ipc"], timeout: 10_000 },
);
launcher.stdout.once("data", () => launcher.send("interrupt"));
launcher.once("message", () => launcher.stdin.end("finish"));
assert.deepEqual(await once(launcher, "exit"), [17, null]);
} finally {
await rm(directory, { recursive: true, force: true });
}
},
);
test(
"the npm spawn fallback forwards a second SIGTERM to a running child",
{ skip: process.platform === "win32" },