diff --git a/src/browser/history/CaptureFailures.test.ts b/src/browser/history/CaptureFailures.test.ts index adcdeaafe1..67ce738e54 100644 --- a/src/browser/history/CaptureFailures.test.ts +++ b/src/browser/history/CaptureFailures.test.ts @@ -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) => { diff --git a/src/browser/history/CaptureFailures.ts b/src/browser/history/CaptureFailures.ts index 23c7761914..1cb3b02ec7 100644 --- a/src/browser/history/CaptureFailures.ts +++ b/src/browser/history/CaptureFailures.ts @@ -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), diff --git a/src/browser/history/HistoricalCaptureDecoder.ts b/src/browser/history/HistoricalCaptureDecoder.ts index 7f541ed027..b04dc47260 100644 --- a/src/browser/history/HistoricalCaptureDecoder.ts +++ b/src/browser/history/HistoricalCaptureDecoder.ts @@ -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, );