Files
reverse-engineering-anything/scripts/lib/verifier-workspace.mjs
T
morluto 3ad2e214be fix(verifier): retain UF2 workspace until resource cleanup completes (#10096)
* fix(verifier): retain UF2 workspace until resource cleanup completes

* fix(verifiers): retain firmware workspace cleanup ownership
2026-11-11 18:53:02 +08:00

35 lines
1.1 KiB
JavaScript

import { rm } from "node:fs/promises";
import { withVerifierCleanup } from "./verifier-cleanup.mjs";
import { importVerifierSource } from "./verifier-source-loader.mjs";
/** Keep the workspace and its owned commands together until cleanup completes. */
export async function withVerifierWorkspace(operation, commands, workspace) {
const { AnalysisCleanupLease } = await importVerifierSource(
new URL("../../src/artifacts/AnalysisCleanupLease.ts", import.meta.url)
.href,
import.meta.url,
);
let lease;
const close = async () => {
const root = workspace();
if (root === undefined) return commands.close();
lease ??= new AnalysisCleanupLease(async () => {
await commands.close();
await rm(root, { recursive: true, force: true });
}, [root]);
await lease.release();
};
try {
return await withVerifierCleanup(operation, [close]);
} catch (cause) {
if (lease !== undefined && !lease.released)
throw Object.assign(
new Error(`Verifier workspace cleanup incomplete: ${workspace()}`, {
cause,
}),
{ cleanupLeases: [lease] },
);
throw cause;
}
}