refactor(browser): bind a capture decoder's reported cleanup to its process lease (#10998)

This commit is contained in:
morluto authored and GitHub committed 2026-11-15 18:00:09 +08:00
1 parent 5775c8124f
commit acbbbaa346
3 files changed
+53 -2

No files matched your search

@@ -1,4 +1,5 @@
import { expect, it } from "vitest";
import { AnalysisCleanupLease } from "../../artifacts/AnalysisCleanupLease.js";
import { ArtifactReaderFailure } from "../../artifacts/ArtifactReader.js";
import {
captureDecoderHeapContinuingRecovery,
@@ -323,6 +324,38 @@ it("retains actual command status and both diagnostic streams when cleanup also
expect(JSON.stringify(redacted)).not.toContain("private-marker");
expect(redacted.code).toBe("cleanup_incomplete");
});
it("resolves the reported cleanup when the decoder's process lease confirms release", async () => {
const input = inspectWebNetworkCaptureInputSchema.parse({
capture_path: "/selected/input.har",
format: "har",
});
const cause = new OwnedCommandFailure(
"process",
"Observed command failure",
null,
"Observed cleanup failure",
undefined,
["pid:42"],
);
let stopped = false;
const processLease = new AnalysisCleanupLease(async () => {
if (!stopped) throw new Error("decoder still running");
}, ["pid:42"]);
const error = historicalCaptureFailure(
input,
cause,
"decoder",
undefined,
DEFAULT_CAPTURE_DECODER_HEAP,
processLease,
);
expect(error.cleanupIncomplete).toBe(true);
await expect(processLease.release()).rejects.toThrow("decoder still running");
expect(error.cleanupIncomplete).toBe(true);
stopped = true;
await processLease.release();
expect(error.cleanupIncomplete).toBe(false);
});
it.each(["io", "unavailable"] as const)(
"keeps %s read failures separate from malformed capture input",
(reason) => {
+12 -1
View File
@@ -2,6 +2,10 @@ import { javaScriptWorkerMemoryLimitBytes } from "../../process/JavaScriptWorker
import { captureBudgets } from "./CaptureBudgets.js";
import { analysisSettingLimit } from "../../domain/analysisSettingDefinitions.js";
import type { ExecutionOptions } from "../../application/AnalysisProvider.js";
import {
AnalysisCleanupLeaseGroup,
type AnalysisCleanupLease,
} from "../../artifacts/AnalysisCleanupLease.js";
import { ArtifactReaderFailure } from "../../artifacts/ArtifactReader.js";
import { stableArtifactInputRecovery } from "../../artifacts/StableArtifactFailure.js";
import { analysisErrorWithCleanupFailure } from "../../domain/analysisErrorCleanup.js";
@@ -89,13 +93,18 @@ export const protobufCaptureFailure = (
export const CAPTURE_PROCESS_MEMORY_LIMIT_REASON =
"The capture decoder reached its Windows Job Object memory limit";
/** Preserve the origin and typed cause of a historical capture failure. */
/**
* Preserve the origin and typed cause of a historical capture failure.
* `cleanupLease` is the decoder's lease on `rawCause`'s retained process
* owner; its confirmed release resolves the cleanup this failure reports.
*/
export const historicalCaptureFailure = (
input: InspectWebNetworkCaptureInput,
rawCause: unknown,
phase: "capture-read" | "descriptor-read" | "decoder",
options?: ExecutionOptions,
heap: CaptureDecoderHeap = DEFAULT_CAPTURE_DECODER_HEAP,
cleanupLease?: AnalysisCleanupLease,
): AnalysisError => {
const { cause, origin } = artifactFilePrimaryFacts(rawCause);
if (origin === "owned-or-classified" && cause instanceof AnalysisError)
@@ -130,6 +139,8 @@ export const historicalCaptureFailure = (
},
{ operation: OPERATION, cause },
);
if (cleanupLease !== undefined)
new AnalysisCleanupLeaseGroup(cleanup, [cleanupLease]);
return cause.cause instanceof OwnedCommandFailure
? analysisErrorWithCleanupFailure(
historicalCaptureFailure(input, cause.cause, phase, options, heap),
@@ -423,7 +423,14 @@ export class HistoricalCaptureDecoder {
// historical error already follows the post-launch completion owner.
const error = retainProcessInspectionCleanup(
processInspectionFailure(cause),
historicalCaptureFailure(input, cause, phase, options, reportedHeap),
historicalCaptureFailure(
input,
cause,
phase,
options,
reportedHeap,
processLease,
),
runtimeResources,
OPERATION,
);