diff --git a/docs/reverse-engineering-tools.md b/docs/reverse-engineering-tools.md index 2d49029b0d..5ec4278977 100644 --- a/docs/reverse-engineering-tools.md +++ b/docs/reverse-engineering-tools.md @@ -182,8 +182,9 @@ that needs a new file step is refused with a retryable capacity error access working for REA's other tools; process capture's executable lookup obeys the same budget. A retry on a path that already has stalled work waits and takes no thread. Closing the service does not wait for that work, which a mount that -never answers would hold forever; the process itself cannot exit until it -returns. +never answers would hold forever. An MCP server's process cannot end on its own +until the work returns. The CLI writes its result and then exits explicitly; a +system call the kernel treats as uninterruptible can still hold the process. Captured stdout and stderr are retained up to a 16 MiB combined limit; `output_truncated` marks output beyond that limit. One-shot commands use the `command_timeout_ms` budget described above. That 16 MiB cap has no runtime override. A typed failure's `captured_output` marks a stream that held invalid UTF-8 with `stdout_invalid_utf8` or `stderr_invalid_utf8`. diff --git a/scripts/rea.mjs b/scripts/rea.mjs index 57e2b02485..5fb4754fee 100755 --- a/scripts/rea.mjs +++ b/scripts/rea.mjs @@ -129,6 +129,7 @@ if (!(await compiledRuntimeExists(runtimeFiles))) { const configurationError = cliConfigurationError(process.env); let dispatchFailure; let closeCliResources; + let cliHoldsStalledFileWork; // Incur can exit from the callback below before this function returns. // That callback owns the status, including a closed-pipe exit of 141. let exiting = false; @@ -151,6 +152,7 @@ if (!(await compiledRuntimeExists(runtimeFiles))) { } else { const cliRuntime = await import("../dist/cli-runtime/cli.js"); closeCliResources = cliRuntime.closeCliResources; + cliHoldsStalledFileWork = cliRuntime.cliHoldsStalledFileWork; const { createCli } = cliRuntime; const { createCliResultOutput } = await import("../dist/cli-runtime/cliResultOutput.js"); @@ -245,6 +247,13 @@ if (!(await compiledRuntimeExists(runtimeFiles))) { !(stdoutClosed && dispatchFailure?.code === "EPIPE") ) throw dispatchFailure; + // A stalled file step keeps the event loop alive until its mount answers, + // which may be never. The command has delivered its result, so exit. + if (!exiting && cliHoldsStalledFileWork?.()) { + if (!stdoutClosed) + await new Promise((resolve) => process.stdout.write("", resolve)); + process.exit(); + } } /** diff --git a/src/cli.ts b/src/cli.ts index 84b7b13522..2ec07853d7 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -86,6 +86,10 @@ import { } from "./cli/guidanceCommands.js"; import { closeCliJavaScriptResources } from "./cli/javascriptApplicationAnalysis.js"; import { registerGuidedWorkflowCommands } from "./cli/guidedWorkflowCommands.js"; +import { HOST_FILE_WORK } from "./process/FileWork.js"; + +/** Whether a stalled file step would keep the process alive after its command. */ +export const cliHoldsStalledFileWork = (): boolean => HOST_FILE_WORK.held > 0; /** Close every process-owned resource used by CLI commands. */ export const closeCliResources = async (): Promise => { diff --git a/tests/boundary/cli/reverseEngineeringStalledFileExit.test.ts b/tests/boundary/cli/reverseEngineeringStalledFileExit.test.ts new file mode 100644 index 0000000000..67e9d8dcbc --- /dev/null +++ b/tests/boundary/cli/reverseEngineeringStalledFileExit.test.ts @@ -0,0 +1,63 @@ +import { execFile } from "node:child_process"; +import { writeFile } from "node:fs/promises"; +import { join } from "node:path"; +import { pathToFileURL } from "node:url"; +import { promisify } from "node:util"; + +import { expect, it } from "vitest"; + +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; + +const execute = promisify(execFile); + +/** + * Stalls one path's `stat` in the child. Its interval keeps the event loop + * alive, as a real system call that a stalled mount holds does. + */ +const STALLED_STAT = ` +import fs from "node:fs"; +import { syncBuiltinESMExports } from "node:module"; + +const stat = fs.promises.stat; +fs.promises.stat = (path, ...rest) => { + if (path !== process.env.REA_TEST_STALLED_PATH) return stat(path, ...rest); + setInterval(() => {}, 1_000); + return new Promise(() => {}); +}; +syncBuiltinESMExports(); +`; + +it("exits after reporting a timeout while a stalled file step is still held", async () => { + const directory = await createTestTempDirectory("rea-stalled-exit-"); + const target = join(directory, "fixture.bin"); + const preload = join(directory, "stalled-stat.mjs"); + await writeFile(target, "fixture"); + await writeFile(preload, STALLED_STAT); + const failure: unknown = await execute( + process.execPath, + [ + "--import", + pathToFileURL(preload).href, + "scripts/rea.mjs", + "inspect-with-objdump", + target, + "file_headers", + "--command-timeout-ms", + "100", + "--json", + ], + { + cwd: process.cwd(), + env: { ...process.env, REA_TEST_STALLED_PATH: target }, + timeout: 30_000, + maxBuffer: 16 * 1_024 * 1_024, + }, + ).then( + () => new Error("Expected the command to fail"), + (cause: unknown) => cause, + ); + // Without the exit, the held step keeps the child alive until the timeout + // above kills it. + expect(failure).toMatchObject({ code: 1, killed: false }); + expect(String((failure as { stdout: unknown }).stdout)).toContain("100 ms"); +});