fix(native): retain failed pwntools process and runtime cleanup (#1708)
* fix(native): retain failed pwntools process and runtime cleanup Keep failed worker/debugger cleanup owners and their private files until verified process shutdown and successful directory removal. Retry cleanup before another inspection and expose shutdown through the shared services, CLI and MCP server. Serialize acquisition without delaying cancellation of queued callers, and preserve primary diagnostics when CLI cleanup fails. The original decoder reproduction left a live owned Node child with its runtime deleted and no retained cleanup owner; the regression now verifies retention, admission blocking, retries and eventual process/file cleanup. Validation: 10 lifecycle/public CLI/MCP tests, 20 service tests, test discovery 814/814, build:cached and full check including architecture guards. Actual POSIX process/filesystem boundaries used injected Node workers on macOS; real Linux pwntools/GDB provider parsing was not exercised. * test(native): verify retained recorded-crash cleanup lifecycle * fix(native): finalize diagnostic cleanup before publishing results * fix(native): retire decoder stages before reading runtime evidence --------- Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
@@ -11,6 +11,7 @@ import { PWNTOOLS_PROVIDER_IDENTITY } from "../../../src/native/pwntools/Pwntool
|
||||
import { spawnOwnedProviderProcess } from "../../../src/process/ProviderProcess.js";
|
||||
import { waitForProviderProcessReady } from "../../fixtures/providerProcess.js";
|
||||
import { PrivateRuntimeRoot } from "../../../src/process/PrivateRuntimeRoot.js";
|
||||
import { cleanupOwnedProcessGroup } from "../../../src/process/ProcessOwnership.js";
|
||||
import { binaryLayoutFixture } from "../../fixtures/binaryDiagnostics/layout.js";
|
||||
import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js";
|
||||
|
||||
@@ -94,6 +95,7 @@ it.runIf(!unsupportedHost)(
|
||||
async () => {
|
||||
const { path } = await fixture();
|
||||
let ownedPath = "";
|
||||
let cleanupAllowed = false;
|
||||
const provider = new PwntoolsLayoutProvider(
|
||||
{ REA_PWNTOOLS_PYTHON: process.execPath },
|
||||
async (spawn) => {
|
||||
@@ -106,22 +108,37 @@ it.runIf(!unsupportedHost)(
|
||||
return {
|
||||
...launched,
|
||||
cleanup: async () => {
|
||||
await launched.cleanup?.();
|
||||
if (cleanupAllowed)
|
||||
return cleanupOwnedProcessGroup(launched.ownership);
|
||||
throw new Error("injected post-cleanup reporting failure");
|
||||
},
|
||||
};
|
||||
},
|
||||
);
|
||||
onTestFinished(async () => {
|
||||
cleanupAllowed = true;
|
||||
await provider.close();
|
||||
});
|
||||
const result = await provider.inspect({ path });
|
||||
if (result.ok) throw new Error("Expected compounded lifecycle failure");
|
||||
expect(result.error).toMatchObject({
|
||||
cleanupIncomplete: true,
|
||||
diagnostics: {
|
||||
reason: "injected post-cleanup reporting failure",
|
||||
previous_error: { failure_kind: "output-limit" },
|
||||
captured_output: { truncated: true, stderr: "" },
|
||||
primary_error: {
|
||||
code: "unreadable_output",
|
||||
details: { captured_output: { truncated: true, stderr: "" } },
|
||||
},
|
||||
cleanup_error: {
|
||||
code: "cleanup_incomplete",
|
||||
details: {
|
||||
diagnostics: { reason: "injected post-cleanup reporting failure" },
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
await expect(access(ownedPath)).resolves.toBeUndefined();
|
||||
cleanupAllowed = true;
|
||||
await provider.close();
|
||||
await expect(access(ownedPath)).rejects.toMatchObject({ code: "ENOENT" });
|
||||
},
|
||||
);
|
||||
@@ -418,8 +435,11 @@ it.runIf(!unsupportedHost)(
|
||||
cleanupIncomplete: true,
|
||||
cleanupResources: [root],
|
||||
diagnostics: {
|
||||
reason: "root cleanup failed",
|
||||
previous_error: { category: "invalid_input" },
|
||||
primary_error: { category: "invalid_input" },
|
||||
cleanup_error: {
|
||||
code: "cleanup_incomplete",
|
||||
details: { diagnostics: { reason: "root cleanup failed" } },
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import type { AnalysisError } from "../../../src/domain/analysisErrorBase.js";
|
||||
import type { RecordedCrash } from "../../../src/domain/native/recordedCrash.js";
|
||||
import { access, mkdir, readFile, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
@@ -9,7 +10,10 @@ import {
|
||||
} from "../../support/workspace/workspaceFixture.js";
|
||||
import { PwntoolsRecordedCrashProvider } from "../../../src/native/pwntools/PwntoolsRecordedCrashProvider.js";
|
||||
import { PWNTOOLS_PROVIDER_IDENTITY } from "../../../src/native/pwntools/PwntoolsRelease.js";
|
||||
import { spawnOwnedProviderProcess } from "../../../src/process/ProviderProcess.js";
|
||||
import {
|
||||
spawnOwnedProviderProcess,
|
||||
type SpawnedOwnedProviderProcess,
|
||||
} from "../../../src/process/ProviderProcess.js";
|
||||
import { cleanupOwnedProcessGroup } from "../../../src/process/ProcessOwnership.js";
|
||||
import { waitForProviderProcessReady } from "../../fixtures/providerProcess.js";
|
||||
import {
|
||||
@@ -27,6 +31,19 @@ const requestSchema = z.object({
|
||||
});
|
||||
const host = process.platform === "linux" && process.arch === "x64";
|
||||
|
||||
const withCleanupGate = (
|
||||
launched: SpawnedOwnedProviderProcess,
|
||||
cleanupAllowed: () => boolean,
|
||||
): SpawnedOwnedProviderProcess => ({
|
||||
...launched,
|
||||
cleanup: async () => {
|
||||
const result = await cleanupOwnedProcessGroup(launched.ownership);
|
||||
if (!cleanupAllowed())
|
||||
throw new Error("source-owned post-cleanup reporting failure");
|
||||
return result;
|
||||
},
|
||||
});
|
||||
|
||||
it
|
||||
.runIf(host)
|
||||
.each([
|
||||
@@ -132,6 +149,7 @@ it
|
||||
"invalid-context",
|
||||
"cancelled",
|
||||
"cleanup-failure",
|
||||
"decoder-cleanup-failure",
|
||||
] as const)(
|
||||
"owns optional context from the same snapshot: %s",
|
||||
async (scenario) => {
|
||||
@@ -139,6 +157,8 @@ it
|
||||
const controller = new AbortController();
|
||||
let owned = "";
|
||||
let snapshotPath = "";
|
||||
let cleanupAllowed = false;
|
||||
let launches = 0;
|
||||
const venv = join(root, "venv");
|
||||
const plugin = join(root, "gdbinit.py");
|
||||
await mkdir(venv);
|
||||
@@ -155,6 +175,7 @@ it
|
||||
}),
|
||||
},
|
||||
async (spawn) => {
|
||||
launches++;
|
||||
const request = requestSchema.parse(
|
||||
JSON.parse(await readFile(spawn.arguments.at(-1) ?? "", "utf8")),
|
||||
);
|
||||
@@ -177,11 +198,14 @@ it
|
||||
value: raw,
|
||||
}),
|
||||
);
|
||||
return spawnOwnedProviderProcess({
|
||||
const launched = await spawnOwnedProviderProcess({
|
||||
...spawn,
|
||||
command: process.execPath,
|
||||
arguments: ["-e", "process.stdout.write('basic stage diagnostic')"],
|
||||
});
|
||||
return scenario === "decoder-cleanup-failure"
|
||||
? withCleanupGate(launched, () => cleanupAllowed)
|
||||
: launched;
|
||||
}
|
||||
expect(request.snapshot_path).toBe(snapshotPath);
|
||||
const context = recordedCrashDebuggerFixture();
|
||||
@@ -207,17 +231,15 @@ it
|
||||
await waitForProviderProcessReady(launched.process);
|
||||
setImmediate(() => controller.abort());
|
||||
}
|
||||
if (scenario === "cleanup-failure")
|
||||
return {
|
||||
...launched,
|
||||
cleanup: async () => {
|
||||
await cleanupOwnedProcessGroup(launched.ownership);
|
||||
throw new Error("source-owned post-cleanup reporting failure");
|
||||
},
|
||||
};
|
||||
return launched;
|
||||
return scenario === "cleanup-failure"
|
||||
? withCleanupGate(launched, () => cleanupAllowed)
|
||||
: launched;
|
||||
},
|
||||
);
|
||||
onTestFinished(async () => {
|
||||
cleanupAllowed = true;
|
||||
await provider.close();
|
||||
});
|
||||
const result = await provider.inspect(
|
||||
{
|
||||
path,
|
||||
@@ -234,46 +256,90 @@ it
|
||||
});
|
||||
} else {
|
||||
if (result.ok) throw new Error("Expected optional debugger failure");
|
||||
const projected = projectAnalysisError(result.error);
|
||||
if (scenario === "unconfigured")
|
||||
expect(projected).toMatchObject({
|
||||
details: { captured_output: { stdout: "basic stage diagnostic" } },
|
||||
});
|
||||
else if (
|
||||
scenario === "invalid-context" ||
|
||||
scenario === "cancelled" ||
|
||||
scenario === "cleanup-failure"
|
||||
) {
|
||||
const serialized = JSON.stringify(projected);
|
||||
expect(serialized).toContain("basic stage diagnostic");
|
||||
expect(serialized).toContain(
|
||||
scenario === "cancelled" ? "ready" : "debugger stage diagnostic",
|
||||
);
|
||||
expect(result.error._tag).toBe(
|
||||
scenario === "invalid-context"
|
||||
? "AnalysisOutputError"
|
||||
: scenario === "cancelled"
|
||||
? "AnalysisCancelledError"
|
||||
: "ProviderAdapterError",
|
||||
);
|
||||
if (scenario === "cleanup-failure")
|
||||
expect(result.error).toBeInstanceOf(ProviderCleanupError);
|
||||
} else
|
||||
expect(projected).toMatchObject({
|
||||
category: "execution_failure",
|
||||
details: {
|
||||
diagnostics: {
|
||||
preceding_output: { stdout: "basic stage diagnostic" },
|
||||
stderr: "debugger failed",
|
||||
},
|
||||
},
|
||||
});
|
||||
expectOptionalContextFailure(scenario, result.error);
|
||||
}
|
||||
expect(await readFile(path)).toEqual(bytes);
|
||||
if (
|
||||
scenario === "cleanup-failure" ||
|
||||
scenario === "decoder-cleanup-failure"
|
||||
) {
|
||||
await expect(access(owned)).resolves.toBeUndefined();
|
||||
expect(await readFile(snapshotPath)).toEqual(bytes);
|
||||
const refused = await provider.inspect({
|
||||
path,
|
||||
include_debugger_context: true,
|
||||
});
|
||||
if (refused.ok)
|
||||
throw new Error("Unreleased ownership must block a launch");
|
||||
expect(refused.error.cleanupResources).toContain(owned);
|
||||
expect(launches).toBe(scenario === "decoder-cleanup-failure" ? 1 : 2);
|
||||
await expect(provider.close()).rejects.toMatchObject({
|
||||
cleanupIncomplete: true,
|
||||
});
|
||||
await expect(access(owned)).resolves.toBeUndefined();
|
||||
cleanupAllowed = true;
|
||||
await provider.close();
|
||||
}
|
||||
await expect(access(owned)).rejects.toMatchObject({ code: "ENOENT" });
|
||||
},
|
||||
);
|
||||
|
||||
const expectOptionalContextFailure = (
|
||||
scenario: string,
|
||||
error: AnalysisError,
|
||||
) => {
|
||||
const projected = projectAnalysisError(error);
|
||||
if (scenario === "unconfigured")
|
||||
expect(projected).toMatchObject({
|
||||
details: { captured_output: { stdout: "basic stage diagnostic" } },
|
||||
});
|
||||
else if (scenario === "decoder-cleanup-failure")
|
||||
expect(projected).toMatchObject({
|
||||
code: "cleanup_incomplete",
|
||||
details: {
|
||||
diagnostics: { captured_output: { stdout: "basic stage diagnostic" } },
|
||||
},
|
||||
});
|
||||
else if (
|
||||
scenario === "invalid-context" ||
|
||||
scenario === "cancelled" ||
|
||||
scenario === "cleanup-failure"
|
||||
) {
|
||||
const serialized = JSON.stringify(projected);
|
||||
expect(serialized).toContain("basic stage diagnostic");
|
||||
expect(serialized).toContain(
|
||||
scenario === "cancelled" ? "ready" : "debugger stage diagnostic",
|
||||
);
|
||||
expect(error._tag).toBe(
|
||||
scenario === "invalid-context"
|
||||
? "AnalysisOutputError"
|
||||
: scenario === "cancelled"
|
||||
? "AnalysisCancelledError"
|
||||
: "ProviderAdapterError",
|
||||
);
|
||||
if (scenario === "cleanup-failure" || scenario === "invalid-context") {
|
||||
if (scenario === "cleanup-failure")
|
||||
expect(error).toBeInstanceOf(ProviderCleanupError);
|
||||
expect(projected).toMatchObject({
|
||||
details: {
|
||||
partial_observation: {
|
||||
debugger: { status: "unavailable", requested: true },
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
} else
|
||||
expect(projected).toMatchObject({
|
||||
category: "execution_failure",
|
||||
details: {
|
||||
diagnostics: {
|
||||
preceding_output: { stdout: "basic stage diagnostic" },
|
||||
stderr: "debugger failed",
|
||||
},
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const fixture = async (includeSignal = false, opaqueOwner = false) => {
|
||||
const workspace = await createTestWorkspace("rea-recorded-core-seam-");
|
||||
onTestFinished(() => removeTestWorkspace(workspace.root));
|
||||
|
||||
@@ -137,6 +137,9 @@ it("returns the summary of retained binary layout Evidence", async () => {
|
||||
const client = await connect(
|
||||
true,
|
||||
new BinaryLayoutService({
|
||||
inspectAndClose(input, options) {
|
||||
return this.inspect(input, options);
|
||||
},
|
||||
identity: BINARY_LAYOUT_TEST_PROVIDER,
|
||||
inspect: () => Promise.resolve(ok(layout)),
|
||||
}),
|
||||
|
||||
@@ -179,6 +179,9 @@ it("discovers an oversized native dossier by exact metadata and reads its retain
|
||||
it("advertises exact schemas and projects one layout section from retained Evidence", async () => {
|
||||
const layout = analysisViewLayoutFixture();
|
||||
const service = new BinaryLayoutService({
|
||||
inspectAndClose(input, options) {
|
||||
return this.inspect(input, options);
|
||||
},
|
||||
identity: BINARY_LAYOUT_TEST_PROVIDER,
|
||||
inspect: () => Promise.resolve(ok(layout)),
|
||||
});
|
||||
|
||||
@@ -14,9 +14,16 @@ import {
|
||||
|
||||
it("advertises exact valid schemas and records inline layout without a binary target", async () => {
|
||||
const value = binaryLayoutFixture();
|
||||
let providerClosed = false;
|
||||
const service = new BinaryLayoutService({
|
||||
inspectAndClose(input, options) {
|
||||
return this.inspect(input, options);
|
||||
},
|
||||
identity: BINARY_LAYOUT_TEST_PROVIDER,
|
||||
inspect: () => Promise.resolve(ok(value)),
|
||||
close: async () => {
|
||||
providerClosed = true;
|
||||
},
|
||||
});
|
||||
const session = createTestBinarySession(() => {
|
||||
throw new Error("deep provider must not start");
|
||||
@@ -29,6 +36,7 @@ it("advertises exact valid schemas and records inline layout without a binary ta
|
||||
onTestFinished(async () => {
|
||||
await client.close();
|
||||
await server.close();
|
||||
expect(providerClosed).toBe(true);
|
||||
await session.close();
|
||||
});
|
||||
const [clientTransport, serverTransport] =
|
||||
|
||||
@@ -14,9 +14,16 @@ import {
|
||||
|
||||
it("advertises exact valid schemas and records inline recorded-core evidence without a binary target", async () => {
|
||||
const value = recordedCrashFixture();
|
||||
let providerClosed = false;
|
||||
const service = new RecordedCrashService({
|
||||
inspectAndClose(input, options) {
|
||||
return this.inspect(input, options);
|
||||
},
|
||||
identity: RECORDED_CRASH_TEST_PROVIDER,
|
||||
inspect: () => Promise.resolve(ok(value)),
|
||||
close: async () => {
|
||||
providerClosed = true;
|
||||
},
|
||||
});
|
||||
const session = createTestBinarySession(() => {
|
||||
throw new Error("deep provider must not start");
|
||||
@@ -29,6 +36,7 @@ it("advertises exact valid schemas and records inline recorded-core evidence wit
|
||||
onTestFinished(async () => {
|
||||
await client.close();
|
||||
await server.close();
|
||||
expect(providerClosed).toBe(true);
|
||||
await session.close();
|
||||
});
|
||||
const [clientTransport, serverTransport] =
|
||||
|
||||
@@ -43,6 +43,9 @@ it.each(["pre-aborted", "aborted-during-probe"] as const)(
|
||||
diagnostics,
|
||||
options: { signal: controller.signal },
|
||||
probePath,
|
||||
runCommand: () => {
|
||||
throw new Error("No process should exist before cancellation");
|
||||
},
|
||||
}),
|
||||
).rejects.toBeInstanceOf(AnalysisCancelledError);
|
||||
expect(probes).toBe(scenario === "pre-aborted" ? 0 : 1);
|
||||
|
||||
Reference in New Issue
Block a user