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:
Yijie Lu
2026-10-13 01:37:47 +08:00
committed by GitHub
co-authored by morluto
parent ad08510a11
commit 11fe5a13f8
26 changed files with 1716 additions and 135 deletions
@@ -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));