mirror of
https://github.com/morluto/rea.git
synced 2026-11-29 01:46:39 +00:00
fix(cli): exit after a command while a stalled file step is held
A pending system call keeps Node's event loop alive, so a CLI command that timed out on a stalled mount wrote its result and then stayed running until the mount answered, which may be never. The launcher now asks the CLI runtime whether the process holds stalled file work and, once the result is delivered and stdout has drained, exits explicitly. Runs that hold no stalled work end as before.
This commit is contained in:
1 parent
e8eab4bfdf
commit
4a4bb74bc4
4 files changed
+79
-2
No files matched your search
@@ -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`.
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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<void> => {
|
||||
|
||||
@@ -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");
|
||||
});
|
||||
Reference in new issue
Block a user