refactor(errors): migrate direct cleanup-dependency compositions (#11861)

refactor(errors): migrate direct cleanup-dependency compositions onto analysisErrorWithCleanupDependencies
This commit is contained in:
RRFZ authored and GitHub committed 2026-11-17 18:45:46 -05:00
1 parent 26d6cf39e6
commit 7725aa0389
5 files changed
+103 -15

No files matched your search

@@ -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,
@@ -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));
}
+2 -6
View File
@@ -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(
+2 -2
View File
@@ -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,
@@ -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();
}
},
);