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
@@ -10,6 +10,11 @@ import type { ProviderIdentity } from "../AnalysisProvider.js";
/** Replaceable offline binary-layout provider; owns its parser and process protocol. */
export interface BinaryLayoutPort {
readonly identity: ProviderIdentity;
inspectAndClose(
input: InspectBinaryLayoutInput,
options?: ExecutionOptions,
): Promise<Result<BinaryLayout, AnalysisError>>;
close?(): Promise<void>;
inspect(
input: InspectBinaryLayoutInput,
options?: ExecutionOptions,
@@ -17,6 +17,9 @@ it.each([
"rejects malformed selection before starting the provider: %j",
async (input) => {
const service = new BinaryLayoutService({
inspectAndClose(input, options) {
return this.inspect(input, options);
},
identity: BINARY_LAYOUT_TEST_PROVIDER,
inspect: () => {
throw new Error("must not run");
@@ -32,6 +35,9 @@ it.each([
it("records the decoded layout once in its Evidence", async () => {
const layout = binaryLayoutFixture();
const service = new BinaryLayoutService({
inspectAndClose(input, options) {
return this.inspect(input, options);
},
identity: BINARY_LAYOUT_TEST_PROVIDER,
inspect: () => Promise.resolve(ok(layout)),
});
@@ -49,6 +55,9 @@ it("preserves a provider's actionable unsupported reason", async () => {
"ELF core belongs to recorded-crash analysis.",
);
const service = new BinaryLayoutService({
inspectAndClose(input, options) {
return this.inspect(input, options);
},
identity: BINARY_LAYOUT_TEST_PROVIDER,
inspect: () => Promise.resolve(err(failure)),
});
@@ -58,6 +67,9 @@ it("preserves a provider's actionable unsupported reason", async () => {
it("cancels before invocation and after an acquired observation", async () => {
const controller = new AbortController();
const service = new BinaryLayoutService({
inspectAndClose(input, options) {
return this.inspect(input, options);
},
identity: BINARY_LAYOUT_TEST_PROVIDER,
inspect: () => {
controller.abort();
@@ -77,6 +89,9 @@ it("cancels before invocation and after an acquired observation", async () => {
},
});
const untouched = new BinaryLayoutService({
inspectAndClose(input, options) {
return this.inspect(input, options);
},
identity: BINARY_LAYOUT_TEST_PROVIDER,
inspect: () => {
throw new Error("must not run");
@@ -112,6 +127,9 @@ it.each(["different-path", "invalid-digest", "outside-range"])(
permissions: { read: true, write: false, execute: true },
});
const service = new BinaryLayoutService({
inspectAndClose(input, options) {
return this.inspect(input, options);
},
identity: BINARY_LAYOUT_TEST_PROVIDER,
inspect: () => Promise.resolve(ok(fixture)),
});
@@ -21,10 +21,36 @@ const OPERATION = "inspect_binary_layout";
export class BinaryLayoutService {
constructor(readonly provider: BinaryLayoutPort) {}
close(): Promise<void> {
return this.provider.close?.() ?? Promise.resolve();
}
/** Bind complete observations to the caller-selected artifact before creating Evidence. */
async inspect(
rawInput: unknown,
options?: ExecutionOptions,
): Promise<Result<Evidence, AnalysisError>> {
return this.#inspect(rawInput, options, (input) =>
this.provider.inspect(input, options),
);
}
/** Complete the owned one-shot lifecycle before validating and publishing Evidence. */
inspectAndClose(
rawInput: unknown,
options?: ExecutionOptions,
): Promise<Result<Evidence, AnalysisError>> {
return this.#inspect(rawInput, options, (input) =>
this.provider.inspectAndClose(input, options),
);
}
async #inspect(
rawInput: unknown,
options: ExecutionOptions | undefined,
inspect: (
input: Parameters<BinaryLayoutPort["inspect"]>[0],
) => ReturnType<BinaryLayoutPort["inspect"]>,
): Promise<Result<Evidence, AnalysisError>> {
if (options?.signal?.aborted)
return err(new AnalysisCancelledError(OPERATION));
@@ -45,7 +71,7 @@ export class BinaryLayoutService {
},
]),
);
const inspected = await this.provider.inspect(input.data, options);
const inspected = await inspect(input.data);
if (!inspected.ok) return inspected;
const diagnostics = binaryLayoutSchema.shape.diagnostics.safeParse(
inspected.value.diagnostics,
@@ -12,6 +12,11 @@ import type { Result } from "../../domain/result.js";
/** Replaceable decoder of supplied recordings; historical PID is never live authority. */
export interface RecordedCrashPort {
readonly identity: ProviderIdentity;
inspectAndClose(
input: InspectRecordedCrashInput,
options?: ExecutionOptions,
): Promise<Result<RecordedCrash, AnalysisError>>;
close?(): Promise<void>;
inspect(
input: InspectRecordedCrashInput,
options?: ExecutionOptions,
@@ -13,6 +13,9 @@ it("labels an envelope containing debugger-derived maps as derived", async () =>
const fixture = recordedCrashFixture();
fixture.debugger = recordedCrashDebuggerFixture();
const service = new RecordedCrashService({
inspectAndClose(input, options) {
return this.inspect(input, options);
},
identity: RECORDED_CRASH_TEST_PROVIDER,
inspect: () => Promise.resolve(ok(fixture)),
});
@@ -36,6 +39,9 @@ it.each([
"rejects malformed selection before starting the provider: %j",
async (input) => {
const service = new RecordedCrashService({
inspectAndClose(input, options) {
return this.inspect(input, options);
},
identity: RECORDED_CRASH_TEST_PROVIDER,
inspect: () => {
throw new Error("must not run");
@@ -55,6 +61,9 @@ it("preserves a provider's actionable unsupported reason", async () => {
"ELF core belongs to recorded-crash analysis.",
);
const service = new RecordedCrashService({
inspectAndClose(input, options) {
return this.inspect(input, options);
},
identity: RECORDED_CRASH_TEST_PROVIDER,
inspect: () => Promise.resolve(err(failure)),
});
@@ -64,6 +73,9 @@ it("preserves a provider's actionable unsupported reason", async () => {
it("cancels before invocation and after an acquired observation", async () => {
const controller = new AbortController();
const service = new RecordedCrashService({
inspectAndClose(input, options) {
return this.inspect(input, options);
},
identity: RECORDED_CRASH_TEST_PROVIDER,
inspect: () => {
controller.abort();
@@ -83,6 +95,9 @@ it("cancels before invocation and after an acquired observation", async () => {
},
});
const untouched = new RecordedCrashService({
inspectAndClose(input, options) {
return this.inspect(input, options);
},
identity: RECORDED_CRASH_TEST_PROVIDER,
inspect: () => {
throw new Error("must not run");
@@ -117,6 +132,9 @@ it.each(["different-path", "invalid-digest", "outside-range"])(
flags: "0x5",
});
const service = new RecordedCrashService({
inspectAndClose(input, options) {
return this.inspect(input, options);
},
identity: RECORDED_CRASH_TEST_PROVIDER,
inspect: () => Promise.resolve(ok(fixture)),
});
@@ -21,10 +21,36 @@ const OPERATION = "inspect_recorded_crash";
export class RecordedCrashService {
constructor(readonly provider: RecordedCrashPort) {}
close(): Promise<void> {
return this.provider.close?.() ?? Promise.resolve();
}
/** Bind complete observations to the caller-selected artifact before creating Evidence. */
async inspect(
rawInput: unknown,
options?: ExecutionOptions,
): Promise<Result<Evidence, AnalysisError>> {
return this.#inspect(rawInput, options, (input) =>
this.provider.inspect(input, options),
);
}
/** Complete the owned one-shot lifecycle before validating and publishing Evidence. */
inspectAndClose(
rawInput: unknown,
options?: ExecutionOptions,
): Promise<Result<Evidence, AnalysisError>> {
return this.#inspect(rawInput, options, (input) =>
this.provider.inspectAndClose(input, options),
);
}
async #inspect(
rawInput: unknown,
options: ExecutionOptions | undefined,
inspect: (
input: Parameters<RecordedCrashPort["inspect"]>[0],
) => ReturnType<RecordedCrashPort["inspect"]>,
): Promise<Result<Evidence, AnalysisError>> {
if (options?.signal?.aborted)
return err(new AnalysisCancelledError(OPERATION));
@@ -45,7 +71,7 @@ export class RecordedCrashService {
},
]),
);
const inspected = await this.provider.inspect(input.data, options);
const inspected = await inspect(input.data);
if (!inspected.ok) return inspected;
const diagnostics = recordedCrashSchema.shape.diagnostics.safeParse(
inspected.value.diagnostics,