From 1d9b96a9c497694592137cf95bea2af0a190abe7 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Fri, 9 Oct 2026 07:01:38 +0800 Subject: [PATCH] fix(cli): preserve web cancellation exit status --- src/cli/webRuntimeCommands.ts | 28 +++++----- tests/acceptance/webRuntimeCli.test.ts | 76 ++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 14 deletions(-) diff --git a/src/cli/webRuntimeCommands.ts b/src/cli/webRuntimeCommands.ts index 4843c0e23..9f52397fc 100644 --- a/src/cli/webRuntimeCommands.ts +++ b/src/cli/webRuntimeCommands.ts @@ -37,9 +37,9 @@ export const registerWebRuntimeCommands = ( observationMs: observationDuration(10_000, 1), }), run: ({ args, options }) => - logCliCommand(logger, "observe-web-execution", async () => { - const result = await withCommandCancellation((signal) => - createWebRuntimeService().observe( + withCommandCancellation((signal) => + logCliCommand(logger, "observe-web-execution", async () => { + const result = await createWebRuntimeService().observe( { cdp_endpoint: args.endpoint, target_id: args.targetId, @@ -47,10 +47,10 @@ export const registerWebRuntimeCommands = ( observation_ms: options.observationMs, }, { progress: runtimeProgress, signal }, - ), - ); - return result.ok ? result.value : browserCliError(result.error); - }), + ); + return result.ok ? result.value : browserCliError(result.error); + }), + ), }); cli.command(CLI_COMMANDS.inspectWebEventListeners, { description: @@ -66,9 +66,9 @@ export const registerWebRuntimeCommands = ( }), options: z.object({ ...browserScopeOptions }), run: ({ args, options }) => - logCliCommand(logger, "inspect-web-event-listeners", async () => { - const result = await withCommandCancellation((signal) => - createWebRuntimeService().inspect( + withCommandCancellation((signal) => + logCliCommand(logger, "inspect-web-event-listeners", async () => { + const result = await createWebRuntimeService().inspect( { cdp_endpoint: args.endpoint, target_id: args.targetId, @@ -76,9 +76,9 @@ export const registerWebRuntimeCommands = ( allowed_origins: options.allowedOrigins, }, { signal }, - ), - ); - return result.ok ? result.value : browserCliError(result.error); - }), + ); + return result.ok ? result.value : browserCliError(result.error); + }), + ), }); }; diff --git a/tests/acceptance/webRuntimeCli.test.ts b/tests/acceptance/webRuntimeCli.test.ts index 9b3084074..53205e23e 100644 --- a/tests/acceptance/webRuntimeCli.test.ts +++ b/tests/acceptance/webRuntimeCli.test.ts @@ -1,5 +1,7 @@ import { execFile, spawn } from "node:child_process"; import { once } from "node:events"; +import { createServer } from "node:http"; +import type { Socket } from "node:net"; import { fileURLToPath } from "node:url"; import { promisify } from "node:util"; import { expect, it, onTestFinished } from "vitest"; @@ -104,3 +106,77 @@ it("translates SIGINT after real CLI arming into awaited instrumentation cleanup clearTimeout(timer); } }); + +it.each([ + ["observe-web-execution", "SIGINT", 130], + ["observe-web-execution", "SIGTERM", 143], + ["inspect-web-event-listeners", "SIGINT", 130], + ["inspect-web-event-listeners", "SIGTERM", 143], +] as const)( + "%s preserves %s status when cancellation interrupts CDP discovery", + async (command, signal, expectedExitCode) => { + const sockets = new Set(); + const server = createServer(() => undefined); + server.on("connection", (socket) => { + sockets.add(socket); + socket.once("close", () => sockets.delete(socket)); + }); + server.listen(0, "127.0.0.1"); + await once(server, "listening"); + const address = server.address(); + if (address === null || typeof address === "string") + throw new Error("Expected a TCP loopback listener"); + const endpoint = `http://127.0.0.1:${String(address.port)}`; + let contactedResolve!: () => void; + const contacted = new Promise((resolve) => { + contactedResolve = resolve; + }); + server.on("request", contactedResolve); + + const args = + command === "observe-web-execution" + ? [command, endpoint, "page-fixture", "--observation-ms", "30000", "--json"] + : [command, endpoint, "page-fixture", "body", "--json"]; + const child = spawn(process.execPath, [entrypoint, ...args], { + env: environment(), + stdio: ["ignore", "pipe", "pipe"], + }); + let stdout = ""; + child.stdout.setEncoding("utf8"); + child.stdout.on("data", (chunk: string) => { + stdout += chunk; + }); + const timeout = setTimeout(() => child.kill("SIGKILL"), 10_000); + try { + let contactTimeout: NodeJS.Timeout | undefined; + try { + await Promise.race([ + contacted, + new Promise((_resolve, reject) => { + contactTimeout = setTimeout( + () => reject(new Error("CLI did not contact the CDP fixture")), + 5_000, + ); + }), + ]); + } finally { + if (contactTimeout !== undefined) clearTimeout(contactTimeout); + } + const closed = once(child, "close"); + child.kill(signal); + const [exitCode, terminationSignal] = await closed; + expect(terminationSignal).toBeNull(); + expect(exitCode).toBe(expectedExitCode); + expect(JSON.parse(stdout)).toMatchObject({ + code: "cancelled", + category: "cancelled", + }); + } finally { + clearTimeout(timeout); + if (child.exitCode === null && child.signalCode === null) + child.kill("SIGKILL"); + for (const socket of sockets) socket.destroy(); + await new Promise((resolve) => server.close(() => resolve())); + } + }, +);