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,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 = {