fix(native): share command supervision and retain failure output
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { EventEmitter } from "node:events";
|
||||
import { PassThrough } from "node:stream";
|
||||
import { setTimeout as delay } from "node:timers/promises";
|
||||
import { beforeAll, expect, it } from "vitest";
|
||||
import { beforeAll, expect, it, vi } from "vitest";
|
||||
import {
|
||||
OwnedCommandFailure,
|
||||
runOwnedCommand,
|
||||
@@ -93,6 +95,60 @@ it("collects a real owned command and verifies its exit before returning diagnos
|
||||
});
|
||||
});
|
||||
|
||||
it("fails an accepted nonzero command when an output stream errors and cleans it up", async () => {
|
||||
const stdout = new PassThrough();
|
||||
const stderr = new PassThrough();
|
||||
class FakeProcess extends EventEmitter {
|
||||
readonly stdout = stdout;
|
||||
readonly stderr = stderr;
|
||||
exitCode: number | null = null;
|
||||
signalCode: NodeJS.Signals | null = null;
|
||||
readonly pid = 41234;
|
||||
kill(): boolean {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
const child = new FakeProcess();
|
||||
const cleanup = vi.fn(async () => ({
|
||||
cleaned: true as const,
|
||||
signaled: false,
|
||||
}));
|
||||
const request = command("unused fake launcher command");
|
||||
|
||||
const failure = runOwnedCommand(
|
||||
request,
|
||||
{ timeoutMs: 2_000, diagnosticBytes: 1024, acceptNonZeroExit: true },
|
||||
{
|
||||
launcher: async () => {
|
||||
setImmediate(() => {
|
||||
stdout.write("partial output");
|
||||
stdout.destroy(new Error("fixture read failure"));
|
||||
stderr.end();
|
||||
child.exitCode = 7;
|
||||
child.emit("exit", 7, null);
|
||||
child.emit("close", 7, null);
|
||||
});
|
||||
return {
|
||||
process: child,
|
||||
ownership: {
|
||||
runId: request.runId,
|
||||
leaderPid: child.pid,
|
||||
processGroupId: child.pid,
|
||||
},
|
||||
cleanup,
|
||||
};
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
await expect(failure).rejects.toMatchObject({
|
||||
reason: "process",
|
||||
message: "stdout stream failed: fixture read failure",
|
||||
snapshot: { stdout: { text: "partial output" }, exitCode: 7 },
|
||||
});
|
||||
expect(cleanup).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it("cancels a real acquired process and independently releases it", async () => {
|
||||
const controller = new AbortController();
|
||||
let pid: number | undefined;
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import { mkdir, rm, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import { afterEach, beforeAll, describe, expect, it } from "vitest";
|
||||
|
||||
import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js";
|
||||
|
||||
import { NativeMacOSProvider } from "../../../../src/native/NativeMacOSProvider.js";
|
||||
import {
|
||||
NATIVE_COMMAND_OUTPUT_BUDGET_BYTES,
|
||||
NativeCommandFailure,
|
||||
XcrunCommandRunner,
|
||||
type NativeCommandRunner,
|
||||
@@ -14,6 +15,8 @@ import {
|
||||
import { err, ok } from "../../../../src/domain/result.js";
|
||||
import { parseLipoArchitectures } from "../../../../src/native/parsers/lipo.js";
|
||||
import { parseCodeSignature } from "../../../../src/native/parsers/codesign.js";
|
||||
import { prepareProcessOwnershipInspection } from "../../../../src/process/ProcessOwnershipObservation.js";
|
||||
import { projectAnalysisError } from "../../../../src/domain/analysisErrorProjection.js";
|
||||
|
||||
import {
|
||||
NativeFixtureRunner as FixtureRunner,
|
||||
@@ -22,6 +25,8 @@ import {
|
||||
nativeMachoTargetForFile,
|
||||
} from "../../../fixtures/nativeCommands.js";
|
||||
|
||||
beforeAll(() => prepareProcessOwnershipInspection());
|
||||
|
||||
let directory: string | undefined;
|
||||
afterEach(async () => {
|
||||
if (directory !== undefined)
|
||||
@@ -29,7 +34,7 @@ afterEach(async () => {
|
||||
directory = undefined;
|
||||
});
|
||||
|
||||
describe("native macOS provider discovery and inspection", () => {
|
||||
describe("native macOS provider discovery", () => {
|
||||
it.each(["ordinary.fixture", " spaced.fixture ", " leading", "trailing "])(
|
||||
"preserves the exact signing identifier %j reported by codesign",
|
||||
(identifier) => {
|
||||
@@ -87,6 +92,48 @@ describe("native macOS provider discovery and inspection", () => {
|
||||
expect(resolutions).toBe(2);
|
||||
});
|
||||
|
||||
it.each(["timeout", "output-limit"] as const)(
|
||||
"preserves %s resolution failures with their captured output and cleanup status",
|
||||
async (reason) => {
|
||||
const failure = new NativeCommandFailure(
|
||||
"file",
|
||||
reason,
|
||||
9,
|
||||
undefined,
|
||||
{
|
||||
stdout: "partial xcrun output",
|
||||
stderr: "partial xcrun diagnostic",
|
||||
stdoutBytes: 20,
|
||||
stderrBytes: 25,
|
||||
exitCode: 9,
|
||||
signal: null,
|
||||
truncated: reason === "output-limit",
|
||||
},
|
||||
"ownership cleanup was not confirmed",
|
||||
["process-group:1234"],
|
||||
);
|
||||
const runner = new XcrunCommandRunner(() =>
|
||||
Promise.resolve(err(failure)),
|
||||
);
|
||||
|
||||
const result = await runner.run("file", [], {});
|
||||
|
||||
expect(result).toEqual(err(failure));
|
||||
if (result.ok) throw new Error("Resolution failure unexpectedly passed");
|
||||
expect(result.error).toMatchObject({
|
||||
reason,
|
||||
capture: {
|
||||
stdout: "partial xcrun output",
|
||||
truncated: reason === "output-limit",
|
||||
},
|
||||
cleanupFailure: "ownership cleanup was not confirmed",
|
||||
cleanupResources: ["process-group:1234"],
|
||||
});
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe("native command output collection", () => {
|
||||
it("retains the complete native command output", async () => {
|
||||
const runner = new XcrunCommandRunner(() =>
|
||||
Promise.resolve(ok({ path: process.execPath, sha256: "a".repeat(64) })),
|
||||
@@ -101,23 +148,222 @@ describe("native macOS provider discovery and inspection", () => {
|
||||
expect(captured.ok && captured.value.stdout).toBe(output);
|
||||
});
|
||||
|
||||
it("cancels and reaps a running native command without an operation timeout", async () => {
|
||||
it("retains both output streams and exit status when a command fails", async () => {
|
||||
const runner = new XcrunCommandRunner(() =>
|
||||
Promise.resolve(ok({ path: process.execPath, sha256: "a".repeat(64) })),
|
||||
);
|
||||
const result = await runner.run(
|
||||
"file",
|
||||
[
|
||||
"-e",
|
||||
'process.stdout.write("partial stdout"); process.stderr.write("partial stderr"); process.exitCode = 7',
|
||||
],
|
||||
{},
|
||||
);
|
||||
|
||||
expect(result.ok).toBe(false);
|
||||
if (result.ok) return;
|
||||
expect(result.error).toMatchObject({
|
||||
reason: "nonzero-exit",
|
||||
exitCode: 7,
|
||||
capture: {
|
||||
stdout: "partial stdout",
|
||||
stderr: "partial stderr",
|
||||
stdoutBytes: 14,
|
||||
stderrBytes: 14,
|
||||
exitCode: 7,
|
||||
signal: null,
|
||||
truncated: false,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("retains accepted nonzero output as a complete command capture", async () => {
|
||||
const runner = new XcrunCommandRunner(() =>
|
||||
Promise.resolve(ok({ path: process.execPath, sha256: "a".repeat(64) })),
|
||||
);
|
||||
const result = await runner.run(
|
||||
"file",
|
||||
[
|
||||
"-e",
|
||||
'process.stdout.write("unsigned observation"); process.exitCode = 1',
|
||||
],
|
||||
{ acceptNonZero: true },
|
||||
);
|
||||
|
||||
expect(result).toMatchObject({
|
||||
ok: true,
|
||||
value: {
|
||||
stdout: "unsigned observation",
|
||||
stdoutBytes: 20,
|
||||
exitCode: 1,
|
||||
signal: null,
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("native command failure capture and limits", () => {
|
||||
it("projects captured diagnostics from a failed native provider command", async () => {
|
||||
const runner = new XcrunCommandRunner(() =>
|
||||
Promise.resolve(ok({ path: process.execPath, sha256: "a".repeat(64) })),
|
||||
);
|
||||
const result = await new NativeMacOSProvider(runner, "darwin")
|
||||
.createClient(machoTarget("/private/fixture"))
|
||||
.execute("demangle_swift", { symbols: ["fixture-symbol"] });
|
||||
|
||||
expect(result.ok).toBe(false);
|
||||
if (result.ok) return;
|
||||
expect(result.error._tag).toBe("ProviderAdapterError");
|
||||
expect(result.error.capturedOutput?.stderr).toContain("Cannot find module");
|
||||
expect(result.error.capturedOutput?.stderr_bytes).toBeGreaterThan(0);
|
||||
expect(result.error.capturedOutput?.exit_code).not.toBe(0);
|
||||
expect(projectAnalysisError(result.error).details).toMatchObject({
|
||||
captured_output: {
|
||||
stderr: expect.stringContaining("Cannot find module"),
|
||||
stderr_bytes: expect.any(Number),
|
||||
exit_code: expect.any(Number),
|
||||
},
|
||||
diagnostics: {
|
||||
reason: "nonzero-exit",
|
||||
stderr_bytes: expect.any(Number),
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("retains output written before cancellation and reaps the stalled command", async () => {
|
||||
const runner = new XcrunCommandRunner(() =>
|
||||
Promise.resolve(ok({ path: process.execPath, sha256: "a".repeat(64) })),
|
||||
);
|
||||
const controller = new AbortController();
|
||||
const pending = runner.run("file", ["-e", "setTimeout(() => {}, 10000)"], {
|
||||
signal: controller.signal,
|
||||
});
|
||||
setTimeout(() => controller.abort(), 30);
|
||||
const pending = runner.run(
|
||||
"file",
|
||||
[
|
||||
"-e",
|
||||
'process.stdout.write("started"); process.stderr.write("warning"); setInterval(() => {}, 1000)',
|
||||
],
|
||||
{ signal: controller.signal },
|
||||
);
|
||||
setTimeout(() => controller.abort(), 100);
|
||||
|
||||
const result = await pending;
|
||||
|
||||
expect(result).toMatchObject({
|
||||
ok: false,
|
||||
error: { reason: "cancelled" },
|
||||
expect(result.ok).toBe(false);
|
||||
if (result.ok) return;
|
||||
expect(result.error).toMatchObject({
|
||||
reason: "cancelled",
|
||||
capture: {
|
||||
stdout: "started",
|
||||
stderr: "warning",
|
||||
stdoutBytes: 7,
|
||||
stderrBytes: 7,
|
||||
truncated: false,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("rejects output beyond the complete native-command budget", async () => {
|
||||
const runner = new XcrunCommandRunner(() =>
|
||||
Promise.resolve(ok({ path: process.execPath, sha256: "a".repeat(64) })),
|
||||
);
|
||||
const megabytesOverBudget =
|
||||
Math.ceil(NATIVE_COMMAND_OUTPUT_BUDGET_BYTES / (1024 * 1024)) + 1;
|
||||
const result = await runner.run(
|
||||
"file",
|
||||
[
|
||||
"-e",
|
||||
`async function main() { const chunk = "x".repeat(1024 * 1024); for (let i = 0; i < ${String(megabytesOverBudget)}; i += 1) await new Promise((resolve) => process.stdout.write(chunk, resolve)); } void main()`,
|
||||
],
|
||||
{},
|
||||
);
|
||||
|
||||
expect(result.ok).toBe(false);
|
||||
if (result.ok) return;
|
||||
expect(result.error.reason).toBe("output-limit");
|
||||
expect(result.error.capture?.truncated).toBe(true);
|
||||
expect(
|
||||
(result.error.capture?.stdoutBytes ?? 0) +
|
||||
(result.error.capture?.stderrBytes ?? 0),
|
||||
).toBeLessThanOrEqual(NATIVE_COMMAND_OUTPUT_BUDGET_BYTES);
|
||||
});
|
||||
});
|
||||
|
||||
describe("native command failure projection", () => {
|
||||
it("projects native cancellation output and incomplete cleanup to callers", async () => {
|
||||
const capture = {
|
||||
stdout: "partial output",
|
||||
stderr: "partial diagnostic",
|
||||
stdoutBytes: 14,
|
||||
stderrBytes: 19,
|
||||
exitCode: null,
|
||||
signal: "SIGKILL",
|
||||
truncated: false,
|
||||
};
|
||||
const runner: NativeCommandRunner = {
|
||||
async run(tool) {
|
||||
return err(
|
||||
new NativeCommandFailure(
|
||||
tool,
|
||||
"cancelled",
|
||||
null,
|
||||
undefined,
|
||||
capture,
|
||||
"process group cleanup was not confirmed",
|
||||
["process-group:1234"],
|
||||
),
|
||||
);
|
||||
},
|
||||
};
|
||||
const result = await new NativeMacOSProvider(runner, "darwin")
|
||||
.createClient(machoTarget("/private/fixture"))
|
||||
.execute("list_architectures", {});
|
||||
|
||||
expect(result.ok).toBe(false);
|
||||
if (result.ok) return;
|
||||
expect(result.error.capturedOutput).toMatchObject({
|
||||
stdout: "partial output",
|
||||
stderr: "partial diagnostic",
|
||||
stdout_bytes: 14,
|
||||
stderr_bytes: 19,
|
||||
signal: "SIGKILL",
|
||||
truncated: false,
|
||||
});
|
||||
expect(result.error.cleanup).toEqual({
|
||||
reason: "process group cleanup was not confirmed",
|
||||
resources: ["process-group:1234"],
|
||||
});
|
||||
expect(projectAnalysisError(result.error).details).toMatchObject({
|
||||
captured_output: {
|
||||
stdout_bytes: 14,
|
||||
stderr_bytes: 19,
|
||||
signal: "SIGKILL",
|
||||
},
|
||||
cleanup: "incomplete",
|
||||
resources: ["process-group:1234"],
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
["timeout", "AnalysisTimeoutError", "provider_timeout"],
|
||||
["output-limit", "AnalysisResourceConstraintError", "resource_constraint"],
|
||||
] as const)(
|
||||
"projects native command %s as a typed resource failure",
|
||||
async (reason, tag, code) => {
|
||||
const runner: NativeCommandRunner = {
|
||||
async run(tool) {
|
||||
return err(new NativeCommandFailure(tool, reason));
|
||||
},
|
||||
};
|
||||
const result = await new NativeMacOSProvider(runner, "darwin")
|
||||
.createClient(machoTarget("/private/fixture"))
|
||||
.execute("list_architectures", {});
|
||||
|
||||
expect(result.ok).toBe(false);
|
||||
if (result.ok) return;
|
||||
expect(result.error._tag).toBe(tag);
|
||||
expect(projectAnalysisError(result.error).code).toBe(code);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe("native macOS provider inspection", () => {
|
||||
|
||||
Reference in New Issue
Block a user