fix(native): retain failed command cleanup owners

Retain failed native command owners through both xcrun resolution and command capture. Retry all owned cleanup failures before new commands and after consumers join their launched work at close; preserve partial output and every resource diagnostic.

Refs #740.
This commit is contained in:
Ysopwnable
2026-10-11 13:39:42 +08:00
committed by GitHub
parent 8be7ad496c
commit d9b4ae182d
6 changed files with 724 additions and 41 deletions
@@ -1,11 +1,70 @@
import { createHash } from "node:crypto";
import { readFile } from "node:fs/promises";
import { EventEmitter } from "node:events";
import { PassThrough } from "node:stream";
import { afterEach, expect, it, vi } from "vitest";
import { XcrunCommandRunner } from "../../../../src/native/CommandRunner.js";
import {
XcrunCommandRunner,
type NativeCommandLauncher,
} from "../../../../src/native/CommandRunner.js";
import { ok } from "../../../../src/domain/result.js";
afterEach(() => vi.unstubAllEnvs());
it.skipIf(process.platform === "win32")(
"retains an xcrun resolver's failed owner and retries it before resolving again",
async () => {
let launches = 0;
const launcher: NativeCommandLauncher = async (options) => {
launches += 1;
const child = Object.assign(new EventEmitter(), {
stdout: new PassThrough(),
stderr: new PassThrough(),
exitCode: null as number | null,
signalCode: null as NodeJS.Signals | null,
kill: () => true,
});
setImmediate(() => {
child.stdout.end("partial xcrun output");
child.stderr.end("resolver failed");
child.exitCode = 1;
child.emit("exit", 1, null);
setImmediate(() => child.emit("close", 1, null));
});
return {
process: child,
ownership: {
runId: options.runId,
leaderPid: 43_000 + launches,
processGroupId: 43_000 + launches,
expectedParentPid: process.pid,
},
cleanup: async () => ({
cleaned: false,
reason: "resolver owner remains unverified",
}),
};
};
const runner = new XcrunCommandRunner({}, undefined, launcher);
const resolution = await runner.run("file", [], {});
expect(resolution.ok).toBe(false);
if (resolution.ok) return;
expect(resolution.error.cleanupOwner).toBeDefined();
expect(resolution.error.capture).toMatchObject({
stdout: "partial xcrun output",
stderr: "resolver failed",
});
expect(launches).toBe(1);
const retry = await runner.run("file", [], {});
expect(retry.ok).toBe(false);
if (!retry.ok) expect(retry.error.cleanupFailures).toHaveLength(1);
expect(launches).toBe(1);
},
);
it.skipIf(process.platform === "win32")(
"executes native commands with the selected environment snapshot instead of ambient values",
async () => {
@@ -37,6 +96,97 @@ it.skipIf(process.platform === "win32")(
},
);
it.skipIf(process.platform === "win32")(
"retains and retries every failed native command cleanup owner before launching another command",
async () => {
const cleanupCalls = new Map<string, number>();
let launches = 0;
const launcher: NativeCommandLauncher = async (options) => {
const name = options.arguments[0] ?? "unnamed";
launches += 1;
const child = Object.assign(new EventEmitter(), {
stdout: new PassThrough(),
stderr: new PassThrough(),
exitCode: null as number | null,
signalCode: null as NodeJS.Signals | null,
kill: () => true,
});
setImmediate(() => {
child.stdout.end(`partial-${name}`);
child.stderr.end(`diagnostic-${name}`);
child.exitCode = 0;
child.emit("exit", 0, null);
setImmediate(() => child.emit("close", 0, null));
});
return {
process: child,
ownership: {
runId: options.runId,
leaderPid: 42_000 + launches,
processGroupId: 42_000 + launches,
expectedParentPid: process.pid,
},
cleanup: async () => {
const calls = (cleanupCalls.get(name) ?? 0) + 1;
cleanupCalls.set(name, calls);
return calls < 3
? {
cleaned: false,
reason: `${name} cleanup attempt ${String(calls)}`,
}
: { cleaned: true, signaled: false };
},
};
};
const runner = new XcrunCommandRunner(
{},
async () => ok({ path: process.execPath, sha256: "a".repeat(64) }),
launcher,
);
const [first, second] = await Promise.all([
runner.run("file", ["first"], {}),
runner.run("file", ["second"], {}),
]);
expect(first.ok).toBe(false);
expect(second.ok).toBe(false);
if (first.ok || second.ok) return;
expect(first.error.capture).toMatchObject({
stdout: "partial-first",
stderr: "diagnostic-first",
});
expect(first.error.cleanupOwner).toBeDefined();
expect(second.error.cleanupOwner).toBeDefined();
expect(cleanupCalls).toEqual(
new Map([
["first", 1],
["second", 1],
]),
);
const blocked = await runner.run("file", ["must-not-launch"], {});
expect(blocked.ok).toBe(false);
if (blocked.ok) return;
expect(blocked.error.cleanupFailures).toHaveLength(2);
expect(blocked.error.cleanupResources).toEqual(
expect.arrayContaining([
first.error.cleanupOwner?.launch.ownership?.runId,
second.error.cleanupOwner?.launch.ownership?.runId,
]),
);
expect(launches).toBe(2);
const cleared = await runner.retryCleanup?.();
expect(cleared).toEqual([]);
expect(cleanupCalls).toEqual(
new Map([
["first", 3],
["second", 3],
]),
);
},
);
it.skipIf(process.platform === "win32")(
"does not accept signal termination as an allowed nonzero exit",
async () => {
@@ -1,6 +1,8 @@
import { createHash } from "node:crypto";
import { EventEmitter } from "node:events";
import { chmod, mkdir, readFile, rm, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { PassThrough } from "node:stream";
import { afterEach, beforeAll, describe, expect, it } from "vitest";
@@ -18,6 +20,7 @@ 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 { ProviderProcessSupervisor } from "../../../../src/process/ProviderProcess.js";
import {
NativeFixtureRunner as FixtureRunner,
@@ -26,6 +29,27 @@ import {
nativeMachoTargetForFile,
} from "../../../fixtures/nativeCommands.js";
const cleanupOwner = (runId: string): ProviderProcessSupervisor => {
const child = Object.assign(new EventEmitter(), {
stdout: new PassThrough(),
stderr: new PassThrough(),
exitCode: null as number | null,
signalCode: null as NodeJS.Signals | null,
kill: () => true,
});
return new ProviderProcessSupervisor({
process: child,
ownsProcessLifetime: true,
ownership: {
runId,
leaderPid: 46_000,
processGroupId: 46_000,
expectedParentPid: process.pid,
},
cleanup: async () => ({ cleaned: false, reason: "still running" }),
});
};
beforeAll(() => prepareProcessOwnershipInspection());
let directory: string | undefined;
@@ -315,6 +339,97 @@ describe("native command failure capture and limits", () => {
});
});
describe("native command cleanup lifecycle", () => {
it("waits for an active command before retrying its failed owner at close", async () => {
let finishCommand!: (
result: Awaited<ReturnType<NativeCommandRunner["run"]>>,
) => void;
let commandStarted!: () => void;
const started = new Promise<void>((resolve) => {
commandStarted = resolve;
});
const command = new Promise<
Awaited<ReturnType<NativeCommandRunner["run"]>>
>((resolve) => {
finishCommand = resolve;
});
let cleanupRetries = 0;
const runner: NativeCommandRunner = {
run: () => {
commandStarted();
return command;
},
retryCleanup: async () => {
cleanupRetries += 1;
return cleanupRetries === 1
? [
{
owner: cleanupOwner("run:first"),
reason: "first process group remains live",
resources: ["run:first", "process-group:1234"],
},
{
owner: cleanupOwner("run:second"),
reason: "second process group remains live",
resources: ["run:second", "process-group:5678"],
},
]
: [];
},
};
const client = new NativeMacOSProvider({}, runner, "darwin").createClient(
machoTarget("/private/fixture"),
);
const execution = client.execute("list_architectures", {});
await started;
const closing = client.close();
await Promise.resolve();
expect(cleanupRetries).toBe(0);
finishCommand(
err(
new NativeCommandFailure("lipo", "timeout", null, undefined, {
stdout: "partial architecture output",
stderr: "partial diagnostic",
stdoutBytes: 27,
stderrBytes: 19,
exitCode: null,
signal: null,
truncated: false,
}),
),
);
const [observed, closed] = await Promise.all([execution, closing]);
expect(observed.ok).toBe(false);
expect(closed.ok).toBe(false);
expect(cleanupRetries).toBe(1);
if (!closed.ok) {
expect(closed.error.cleanupResources).toEqual([
"run:first",
"process-group:1234",
"run:second",
"process-group:5678",
]);
expect(closed.error).toHaveProperty("diagnostics", {
reason: "Native command process cleanup remains incomplete",
failures: [
{
reason: "first process group remains live",
resources: ["run:first", "process-group:1234"],
},
{
reason: "second process group remains live",
resources: ["run:second", "process-group:5678"],
},
],
});
}
await expect(client.close()).resolves.toMatchObject({ ok: true });
expect(cleanupRetries).toBe(2);
});
});
describe("native command failure projection", () => {
it("projects native cancellation output and incomplete cleanup to callers", async () => {
const capture = {