diff --git a/scripts/verify/baselines/cleanup-dependency-binding.json b/scripts/verify/baselines/cleanup-dependency-binding.json index 12a1386fae..0f585d6931 100644 --- a/scripts/verify/baselines/cleanup-dependency-binding.json +++ b/scripts/verify/baselines/cleanup-dependency-binding.json @@ -14,7 +14,6 @@ "src/browser/CdpTargetSessionOwner.ts": 2, "src/browser/PlaywrightBrowserScenarioProvider.ts": 2, "src/browser/WebSourceMapFetcher.ts": 2, - "src/browser/history/HistoricalCaptureDecoder.ts": 1, "src/cli/desktopCaptureCommands.ts": 1, "src/cutter/CutterBridgeClient.ts": 1, "src/desktop/ScreenJudgeProcess.ts": 1, @@ -27,7 +26,6 @@ "src/hopper/BridgeLauncher.ts": 3, "src/hopper/HopperAnalysisProfile.ts": 1, "src/hopper/HopperClient.ts": 1, - "src/hopper/HopperFileOffset.ts": 1, "src/hopper/HopperImageCopy.ts": 1, "src/ida/IdaSessionClient.ts": 1, "src/inspector/V8InspectorProvider.ts": 1, @@ -40,7 +38,7 @@ "src/native/LldbCallTracer.ts": 3, "src/native/NativeCommandAnalysisError.ts": 1, "src/native/NativeMacOSProvider.ts": 2, - "src/pcap/PcapProvider.ts": 6, + "src/pcap/PcapProvider.ts": 5, "src/pcap/PcapSnapshot.ts": 1, "src/process/ArtifactAnalysisResources.ts": 1, "src/process/OwnedDebuggerSessionRegistry.ts": 1, diff --git a/src/browser/history/HistoricalCaptureDecoder.ts b/src/browser/history/HistoricalCaptureDecoder.ts index c957eccd7e..f9ddc77e3b 100644 --- a/src/browser/history/HistoricalCaptureDecoder.ts +++ b/src/browser/history/HistoricalCaptureDecoder.ts @@ -50,7 +50,7 @@ import { } from "../../domain/analysisErrorCore.js"; import { ProviderAdapterError } from "../../domain/providerAdapterError.js"; import { ProviderCleanupError } from "../../domain/providerCleanupError.js"; -import { analysisErrorWithCleanupFailure } from "../../domain/analysisErrorCleanup.js"; +import { analysisErrorWithCleanupDependencies } from "../../domain/analysisErrorCleanup.js"; import { err, ok, type Result } from "../../domain/result.js"; import { WEB_NETWORK_CAPTURE_LIMITS, @@ -540,7 +540,7 @@ export class HistoricalCaptureDecoder { ); const failure = result.ok ? cleanupError - : analysisErrorWithCleanupFailure( + : analysisErrorWithCleanupDependencies( result.error, cleanupError, OPERATION, @@ -548,8 +548,6 @@ export class HistoricalCaptureDecoder { if (completedCapture !== undefined) failure.retainPartialObservation(completedCapture); new AnalysisCleanupLeaseGroup(cleanupError, [cleanupFailure.lease]); - if (!result.ok) - failure.bindCleanupDependencies([result.error, cleanupError]); new AnalysisCleanupLeaseGroup(failure, [cleanupFailure.lease]); return err(redactExplicitFailure(failure, input.sensitive_values)); } diff --git a/src/hopper/HopperFileOffset.ts b/src/hopper/HopperFileOffset.ts index 98eca96cb6..afa8e0216f 100644 --- a/src/hopper/HopperFileOffset.ts +++ b/src/hopper/HopperFileOffset.ts @@ -3,7 +3,7 @@ import { constants } from "node:fs"; import { open, type FileHandle } from "node:fs/promises"; import type { AnalysisError } from "../domain/analysisErrorBase.js"; -import { analysisErrorWithCleanupFailure } from "../domain/analysisErrorCleanup.js"; +import { analysisErrorWithCleanupDependencies } from "../domain/analysisErrorCleanup.js"; import { AnalysisAccessDeniedError, AnalysisArtifactChangedError, @@ -91,11 +91,7 @@ export async function mapHopperFileOffset( unavailable, publish, withCleanup: (primary, marker) => - analysisErrorWithCleanupFailure( - primary, - publish(marker), - OPERATION, - ).bindCleanupDependencies([marker]), + analysisErrorWithCleanupDependencies(primary, publish(marker), OPERATION), }; try { const captured = await snapshotFromPath( diff --git a/src/pcap/PcapProvider.ts b/src/pcap/PcapProvider.ts index 6e7b77cbf6..11e164673a 100644 --- a/src/pcap/PcapProvider.ts +++ b/src/pcap/PcapProvider.ts @@ -932,11 +932,11 @@ export class PcapProvider { new RetainedSnapshotFailure(failure, failure), ); } else { - const returned = analysisErrorWithCleanupFailure( + const returned = analysisErrorWithCleanupDependencies( result.error, cleanup, request.operation, - ).bindCleanupDependencies([cleanup]); + ); result = err(returned); this.snapshotFailures.set( snapshot, diff --git a/tests/boundary/pcap/pcapProviderCrossPlatform.test.ts b/tests/boundary/pcap/pcapProviderCrossPlatform.test.ts index 0cc8312931..3b26483f42 100644 --- a/tests/boundary/pcap/pcapProviderCrossPlatform.test.ts +++ b/tests/boundary/pcap/pcapProviderCrossPlatform.test.ts @@ -30,6 +30,11 @@ import { TSHARK_COMMAND_ENV, } from "../../../src/pcap/PcapConfiguration.js"; import { PcapSnapshot } from "../../../src/pcap/PcapSnapshot.js"; +import { + AnalysisCleanupLease, + ArtifactReaderFailure, + readStableArtifact, +} from "../../../src/resources/index.js"; import * as regularFile from "../../../src/filesystem/RegularFile.js"; import { OwnedFileHandle } from "../../../src/filesystem/OwnedFileHandle.js"; import type { PcapCommandLauncher } from "../../../src/pcap/PcapCommand.js"; @@ -602,3 +607,94 @@ it.skipIf(isWindows).each([ expect(await provider.inspectAvailability()).toMatchObject(expected); }, ); + +it.skipIf(!hasNativeBound)( + "keeps cleanupIncomplete true when snapshot closes before a failed primary cleanup releases", + async () => { + const directory = await createTestTempDirectory( + "rea-pcap-primary-cleanup-", + ); + const capturePath = join(directory, "sample.pcap"); + await writeFile(capturePath, Buffer.from("pcap-fixture-bytes")); + const resolvedCapinfos = await realpath(capinfosStub); + let capinfosReads = 0; + let blockPrimaryRelease = true; + let primaryLeaseRetained = false; + + const provider = new PcapProvider({ + environment: { + REA_TSHARK_COMMAND: tsharkStub, + REA_CAPINFOS_COMMAND: capinfosStub, + }, + artifactReader: async (path, maximumBytes, signal, kind, scope) => { + if ((await realpath(path)) === resolvedCapinfos) { + capinfosReads += 1; + if (capinfosReads === 5) { + const lease = new AnalysisCleanupLease(async () => { + if (blockPrimaryRelease) + throw new Error("controlled tool descriptor close failure"); + }, [path]); + scope?.retain({ + kind: "lease", + resource: path, + lease, + }); + primaryLeaseRetained = true; + throw new ArtifactReaderFailure( + "io", + "Controlled descriptor close failure after reading capinfos", + { + cleanup: { + reason: "Controlled descriptor close failure", + resources: [path], + }, + cleanupLeases: [lease], + cause: new Error("controlled primary EIO"), + }, + ); + } + } + return readStableArtifact(path, maximumBytes, signal, kind, scope); + }, + }); + + const originalClose = PcapSnapshot.prototype.close; + let failSnapshotClose = true; + const closeSpy = vi + .spyOn(PcapSnapshot.prototype, "close") + .mockImplementation(async function (this: PcapSnapshot) { + if (failSnapshotClose) { + failSnapshotClose = false; + throw new Error("controlled snapshot close failure"); + } + return originalClose.call(this); + }); + + try { + const result = await provider.execute({ + operation: "inspect_pcap_capture", + input: { path: capturePath }, + }); + expect(primaryLeaseRetained).toBe(true); + expect(failSnapshotClose).toBe(false); + expect(result.ok).toBe(false); + if (result.ok) return; + expect(result.error.cleanupIncomplete).toBe(true); + + // Releasing only the snapshot leaves the primary cleanup failure open. + await expect(provider.close()).rejects.toThrow(); + expect(result.error.cleanupResources).toEqual([resolvedCapinfos]); + expect(result.error.cleanupIncomplete).toBe(true); + + // Once the primary read lease also releases, cleanup becomes complete. + blockPrimaryRelease = false; + await expect(provider.close()).resolves.toBeUndefined(); + expect(result.error.cleanupIncomplete).toBe(false); + } finally { + blockPrimaryRelease = false; + failSnapshotClose = false; + closeSpy.mockRestore(); + await provider.close(); + } + }, +);