Reverse engineer anything with agents, from app behavior down to native binaries.
TypeScript agent-skillsai-agentsbinary-analysisclaude-codeclicodexcordisdecompilerdeveloper-toolsdshdsh-pluginghidrahopperllmmcpmodel-context-protocolreareverse-engineeringstatic-analysistouhou
fix(artifacts): retain ASAR resources through cleanup failures
morlutoeb97da33881e
diff --git a/src/artifacts/AsarArtifactReader.ts b/src/artifacts/AsarArtifactReader.tsindex 3e0f69a5b..b7a235f4b 100644--- a/src/artifacts/AsarArtifactReader.ts+++ b/src/artifacts/AsarArtifactReader.ts@@ -1,10 +1,4 @@-import {- constants,- createReadStream,- createWriteStream,- type ReadStream,- type Stats,-} from "node:fs";+import { constants, createWriteStream, type Stats } from "node:fs";import {lstat,mkdtemp,@@ -46,51 +40,40 @@ export class AsarArtifactReader implements ArtifactReader {#archiveSize: number | undefined;#headerSize: number | undefined;readonly #entries = new Map<string, AsarEntryState>();- readonly #unpackedHandles = new Map<- UnpackedFileHandle,- {- readonly path: string;- readonly owner: OwnedFileHandle<UnpackedFileHandle>;- }- >();+ readonly #ownedHandles = new Map<SharedAsarFileHandle, OwnedAsarHandle>();#snapshot: { readonly path: string; readonly sha256: string } | undefined;#snapshotRoot: string | undefined;#snapshotOperation: Promise<void> = Promise.resolve();constructor(private readonly path: string,- private readonly openUnpackedFile: OpenAsarUnpackedFile = open,- private readonly removeSnapshot: (path: string) => Promise<void> = (path) =>- rm(path, { recursive: true, force: true }),+ private readonly options: AsarArtifactReaderOptions = {},) {}async *entries(signal?: AbortSignal): AsyncIterable<ArtifactEntry> {let paths: readonly AsarListedPath[];try {await this.prepareContainer(undefined, signal);- const snapshotPath = this.#snapshotPath();- this.#resetArchiveState();- this.#entries.clear();- const headerHandle = await open(- snapshotPath,- constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK,- );- try {- const admitted = await admitAsarHeader(headerHandle);+ paths = await this.#withSnapshotOperation(async () => {+ const snapshotPath = this.#snapshotPath();+ this.#resetArchiveState();+ this.#entries.clear();+ const admitted = await this.#admitHeader(snapshotPath);this.#archiveSize = admitted.archiveBytes;this.#headerSize = admitted.headerBytes;- } finally {- await headerHandle.close();- }- abortIfNeeded(signal);- paths = orderAsarListing(listPackage(snapshotPath, { isPack: false }));- const archiveMetadata = await lstat(snapshotPath);- if (!archiveMetadata.isFile() || archiveMetadata.isSymbolicLink())- throw new ArtifactReaderFailure(- "format",- "ASAR container is not a regular file",+ abortIfNeeded(signal);+ const ordered = orderAsarListing(+ listPackage(snapshotPath, { isPack: false }),);- this.#archiveSize = archiveMetadata.size;+ const archiveMetadata = await lstat(snapshotPath);+ if (!archiveMetadata.isFile() || archiveMetadata.isSymbolicLink())+ throw new ArtifactReaderFailure(+ "format",+ "ASAR container is not a regular file",+ );+ this.#archiveSize = archiveMetadata.size;+ return ordered;+ }, signal);} catch (cause: unknown) {throw asarFailure(this.path, "inventory", cause);}@@ -142,7 +125,47 @@ export class AsarArtifactReader implements ArtifactReader {}}+ async #admitHeader(snapshotPath: string) {+ const headerHandle = await this.#openOwned(+ snapshotPath,+ constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK,+ );+ let admission:+ | {+ readonly ok: true;+ readonly value: Awaited<ReturnType<typeof admitAsarHeader>>;+ }+ | { readonly ok: false; readonly cause: unknown };+ try {+ admission = { ok: true, value: await admitAsarHeader(headerHandle) };+ } catch (cause: unknown) {+ admission = { ok: false, cause };+ }+ try {+ await this.#closeOwnedHandle(headerHandle);+ } catch (cleanupCause: unknown) {+ if (!admission.ok)+ throw ArtifactReaderFailure.withCleanup(+ admission.cause,+ ArtifactReaderFailure.cleanupObservation(cleanupCause, snapshotPath),+ );+ throw cleanupCause;+ }+ if (!admission.ok) throw admission.cause;+ return admission.value;+ }+async open(entry: ArtifactEntry, signal?: AbortSignal): Promise<Readable> {+ return this.#withSnapshotOperation(+ () => this.#openEntry(entry, signal),+ signal,+ );+ }++ async #openEntry(+ entry: ArtifactEntry,+ signal?: AbortSignal,+ ): Promise<Readable> {abortIfNeeded(signal);const state = this.#entries.get(entry.adapterKey);if (state === undefined && entry.kind !== "file")@@ -160,10 +183,16 @@ export class AsarArtifactReader implements ArtifactReader {"integrity",`ASAR entry metadata changed since inventory: ${state.entry.path}`,);- const { metadata } = state;- const producedEntry = state.entry;- if (producedEntry.unpacked)- return this.#openUnpackedStream(producedEntry, signal);+ if (state.entry.unpacked)+ return this.#openUnpackedStream(state.entry, signal);+ return this.#openPackedStream(state, signal);+ }++ async #openPackedStream(+ state: AsarEntryState,+ signal?: AbortSignal,+ ): Promise<Readable> {+ const { metadata, entry: producedEntry } = state;const archiveSize = this.#archiveSize;const headerSize = this.#headerSize;const offset =@@ -187,10 +216,11 @@ export class AsarArtifactReader implements ArtifactReader {"format",`ASAR entry range is outside its container: ${producedEntry.path}`,);- let handle: FileHandle | undefined;+ const snapshotPath = this.#snapshotPath();+ let handle: SharedAsarFileHandle | undefined;try {- const openedHandle = await open(- this.#snapshotPath(),+ const openedHandle = await this.#openOwned(+ snapshotPath,constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK,);handle = openedHandle;@@ -202,25 +232,48 @@ export class AsarArtifactReader implements ArtifactReader {`ASAR container changed before read: ${producedEntry.path}`,);if (metadata.size === 0) {- await closeAsarHandle(() => openedHandle.close(), signal);handle = undefined;+ await closeAsarHandle(+ () => this.#closeOwnedHandle(openedHandle),+ signal,+ );return Readable.from([]);}- const source = openedHandle.createReadStream({+ const source = this.#ownedReadStream(openedHandle, {start,end: start + metadata.size - 1,- autoClose: true,+ signal,});- return readValidatedAsarEntry(+ handle = undefined;+ const output = readValidatedAsarEntry(source,metadata.size,producedEntry.path,this.path,signal,);+ this.#setOwnedOutput(openedHandle, output);+ return output;} catch (cause: unknown) {- await handle?.close().catch(() => undefined);- throw asarFailure(this.path, `read ${producedEntry.path}`, cause);+ const failure = asarFailure(+ this.path,+ `read ${producedEntry.path}`,+ cause,+ );+ if (handle !== undefined) {+ try {+ await this.#closeOwnedHandle(handle);+ } catch (cleanupCause: unknown) {+ throw ArtifactReaderFailure.withCleanup(+ failure,+ ArtifactReaderFailure.cleanupObservation(+ cleanupCause,+ snapshotPath,+ ),+ );+ }+ }+ throw failure;}}@@ -246,49 +299,37 @@ export class AsarArtifactReader implements ArtifactReader {}/** Read the same captured container that supplies headers and packed members. */- async openContainer(signal?: AbortSignal): Promise<ReadStream> {+ async openContainer(signal?: AbortSignal): Promise<Readable> {await this.prepareContainer(undefined, signal);- return createReadStream(- this.#snapshotPath(),- signal === undefined ? {} : { signal },- );+ return this.#withSnapshotOperation(async () => {+ abortIfNeeded(signal);+ const snapshotPath = this.#snapshotPath();+ const handle = await this.#openOwned(+ snapshotPath,+ constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK,+ );+ return this.#ownedReadStream(handle, { signal });+ }, signal);}async close(): Promise<void> {- await this.#withSnapshotOperation(async () => {- let cleanupFailure: ArtifactReaderFailure | undefined;- for (const [handle, { path }] of this.#unpackedHandles) {- try {- await this.#closeUnpackedHandle(handle);- } catch (cause: unknown) {- cleanupFailure = ArtifactReaderFailure.withCleanup(- cleanupFailure ?? cause,- ArtifactReaderFailure.cleanupObservation(cause, path),- );- }- }- try {- await this.#closeSnapshot();- } catch (cause: unknown) {- cleanupFailure = ArtifactReaderFailure.withCleanup(- cleanupFailure ?? cause,- ArtifactReaderFailure.cleanupObservation(cause, this.path),- );- }- if (cleanupFailure !== undefined) throw cleanupFailure;- });+ await this.#withSnapshotOperation(() => this.#closeSnapshot());}async #captureSnapshot(signal?: AbortSignal): Promise<void> {+ let inputHandle: SharedAsarFileHandle | undefined;try {this.#snapshotRoot = await mkdtemp(join(tmpdir(), "rea-asar-snapshot-"));const snapshotPath = join(this.#snapshotRoot, "container.asar");const hash = createHash("sha256");- const handle = await open(+ inputHandle = await this.#openOwned(this.path,constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK,);- const source = handle.createReadStream();+ const source = this.#ownedReadStream(inputHandle, {+ signal,+ releaseOnClose: false,+ });const digesting = new Transform({transform(chunk: Buffer, _encoding, callback) {hash.update(chunk);@@ -301,34 +342,50 @@ export class AsarArtifactReader implements ArtifactReader {createWriteStream(snapshotPath, { flags: "wx", mode: 0o600 }),signal === undefined ? {} : { signal },);+ const completedInput = inputHandle;+ inputHandle = undefined;+ await this.#closeOwnedHandle(completedInput);this.#snapshot = { path: snapshotPath, sha256: hash.digest("hex") };} catch (cause: unknown) {- const primary =+ let failure =signal?.aborted === true? new ArtifactReaderFailure("cancelled", "ASAR operation cancelled", {cause,}): asarFailure(this.path, "snapshot", cause);+ if (inputHandle !== undefined) {+ const failedInput = inputHandle;+ inputHandle = undefined;+ try {+ await this.#closeOwnedHandle(failedInput);+ } catch (cleanupCause: unknown) {+ failure = ArtifactReaderFailure.withCleanup(+ failure,+ ArtifactReaderFailure.cleanupObservation(cleanupCause, this.path),+ );+ }+ }try {- await this.#closeSnapshot();+ if (this.#ownedHandles.size === 0) await this.#closeSnapshot();} catch (cleanupCause: unknown) {throw ArtifactReaderFailure.withCleanup(- primary,+ failure,ArtifactReaderFailure.cleanupObservation(cleanupCause,this.#snapshotRoot ?? this.path,),);}- throw primary;+ throw failure;}}async #closeSnapshot(): Promise<void> {+ await this.#closeOwnedHandles();const root = this.#snapshotRoot;if (root !== undefined) {try {- await this.removeSnapshot(root);+ await (this.options.removeSnapshot ?? defaultRemoveSnapshot)(root);if (this.#snapshotRoot === root) this.#snapshotRoot = undefined;} catch (cause: unknown) {throw ArtifactReaderFailure.withCleanup(cause, {@@ -386,7 +443,7 @@ export class AsarArtifactReader implements ArtifactReader {signal?: AbortSignal,): Promise<Readable> {const unpackedRoot = `${this.path}.unpacked`;- let handle: UnpackedFileHandle | undefined;+ let handle: SharedAsarFileHandle | undefined;try {const rootMetadata = await lstat(unpackedRoot);if (!rootMetadata.isDirectory() || rootMetadata.isSymbolicLink())@@ -413,15 +470,12 @@ export class AsarArtifactReader implements ArtifactReader {"path",`ASAR unpacked entry is not a regular file: ${entry.path}`,);- handle = await this.openUnpackedFile(+ handle = await (this.options.openUnpackedFile ?? open)(canonical,constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK,);const openedHandle = handle;- this.#unpackedHandles.set(openedHandle, {- path: canonical,- owner: new OwnedFileHandle(openedHandle),- });+ this.#adoptHandle(openedHandle, canonical);const openedMetadata = await openedHandle.stat();if (!openedMetadata.isFile() ||@@ -441,30 +495,28 @@ export class AsarArtifactReader implements ArtifactReader {);}abortIfNeeded(signal);- const source = readFileHandleChunks(openedHandle, {- start: 0,- });- source.once("close", () => {- void this.#closeUnpackedHandle(openedHandle).catch(() => undefined);- });- return readValidatedAsarEntry(+ const source = this.#ownedReadStream(openedHandle, { start: 0 });+ handle = undefined;+ const output = readValidatedAsarEntry(source,undefined,entry.path,this.path,signal,);+ this.#setOwnedOutput(openedHandle, output);+ return output;} catch (cause: unknown) {let failure = asarUnpackedEntryFailure(this.path, entry, cause);- if (handle !== undefined && this.#unpackedHandles.has(handle)) {+ if (handle !== undefined && this.#ownedHandles.has(handle)) {try {- await this.#closeUnpackedHandle(handle);+ await this.#closeOwnedHandle(handle);} catch (cleanupCause: unknown) {failure = ArtifactReaderFailure.withCleanup(failure,ArtifactReaderFailure.cleanupObservation(cleanupCause,- this.#unpackedHandles.get(handle)?.path ?? entry.path,+ this.#ownedHandles.get(handle)?.path ?? entry.path,),);}@@ -473,11 +525,76 @@ export class AsarArtifactReader implements ArtifactReader {}}- async #closeUnpackedHandle(handle: UnpackedFileHandle): Promise<void> {- const owned = this.#unpackedHandles.get(handle);+ async #closeOwnedHandle(handle: SharedAsarFileHandle): Promise<void> {+ const owned = this.#ownedHandles.get(handle);if (owned === undefined) return;- await owned.owner.close();- this.#unpackedHandles.delete(handle);+ owned.output?.destroy();+ owned.source?.destroy();+ try {+ await owned.owner.close();+ } catch (cause: unknown) {+ throw ArtifactReaderFailure.withCleanup(+ cause,+ ArtifactReaderFailure.cleanupObservation(cause, owned.path),+ );+ }+ this.#ownedHandles.delete(handle);+ }++ async #closeOwnedHandles(): Promise<void> {+ let failure: ArtifactReaderFailure | undefined;+ for (const [handle, owned] of this.#ownedHandles) {+ try {+ await this.#closeOwnedHandle(handle);+ } catch (cause: unknown) {+ failure = ArtifactReaderFailure.withCleanup(+ failure ?? cause,+ ArtifactReaderFailure.cleanupObservation(cause, owned.path),+ );+ }+ }+ if (failure !== undefined) throw failure;+ }++ async #openOwned(path: string, flags: number): Promise<SharedAsarFileHandle> {+ const handle = await (this.options.openFile ?? open)(path, flags);+ this.#adoptHandle(handle, path);+ return handle;+ }++ #adoptHandle(handle: SharedAsarFileHandle, path: string): void {+ this.#ownedHandles.set(handle, {+ path,+ owner: new OwnedFileHandle(handle),+ });+ }++ #ownedReadStream(+ handle: SharedAsarFileHandle,+ options: {+ readonly start?: number;+ readonly end?: number;+ readonly signal?: AbortSignal | undefined;+ readonly releaseOnClose?: boolean;+ },+ ): Readable {+ const { releaseOnClose, ...readOptions } = options;+ const source = readFileHandleChunks(handle, readOptions);+ const owned = this.#ownedHandles.get(handle);+ if (owned === undefined)+ throw new ArtifactReaderFailure("integrity", "ASAR handle has no owner");+ owned.source = source;+ owned.output = source;+ if (releaseOnClose !== false)+ source.once("close", () => {+ void this.#closeOwnedHandle(handle).catch(() => undefined);+ });+ return source;+ }++ #setOwnedOutput(handle: SharedAsarFileHandle, output: Readable): void {+ const owned = this.#ownedHandles.get(handle);+ if (owned !== undefined) owned.output = output;}#resetArchiveState(): void {@@ -491,16 +608,27 @@ type AsarEntryState = {readonly metadata: AsarFileMetadata;readonly entry: ArtifactEntry;};-type UnpackedFileHandle = {- readonly fd: number;+type SharedAsarFileHandle = Pick<FileHandle, "fd" | "read" | "close"> & {stat(): Promise<Stats>;- close(): Promise<void>;- read: FileHandle["read"];};-type OpenAsarUnpackedFile = (+type OpenAsarFile = (path: string,flags: number,-) => Promise<UnpackedFileHandle>;+) => Promise<SharedAsarFileHandle>;+type OwnedAsarHandle = {+ readonly path: string;+ readonly owner: OwnedFileHandle<SharedAsarFileHandle>;+ source?: Readable;+ output?: Readable;+};+type AsarArtifactReaderOptions = {+ readonly openFile?: OpenAsarFile;+ readonly openUnpackedFile?: OpenAsarFile;+ readonly removeSnapshot?: (path: string) => Promise<void>;+};++const defaultRemoveSnapshot = (path: string): Promise<void> =>+ rm(path, { recursive: true, force: true });const asarUnpackedEntryFailure = (archivePath: string,diff --git a/src/artifacts/AsarHeader.ts b/src/artifacts/AsarHeader.tsindex 7c17f1cc7..216b05329 100644--- a/src/artifacts/AsarHeader.ts+++ b/src/artifacts/AsarHeader.ts@@ -1,3 +1,4 @@+import type { Stats } from "node:fs";import type { FileHandle } from "node:fs/promises";import { ArtifactReaderFailure } from "./ArtifactReader.js";@@ -7,7 +8,7 @@ export const MAX_ASAR_HEADER_BYTES = 16 * 1024 * 1024;/** Admit both Chromium pickle extents before an upstream decoder allocates them. */export const admitAsarHeader = async (- handle: FileHandle,+ handle: Pick<FileHandle, "read"> & { stat(): Promise<Stats> },): Promise<{readonly archiveBytes: number;readonly headerBytes: number;diff --git a/src/dotnet/ManagedNativeBoundaryInspector.test.ts b/src/dotnet/ManagedNativeBoundaryInspector.test.tsindex 3e33925ff..18ea8d988 100644--- a/src/dotnet/ManagedNativeBoundaryInspector.test.ts+++ b/src/dotnet/ManagedNativeBoundaryInspector.test.ts@@ -280,7 +280,11 @@ describe("managed native boundary inspection", () => {describe("independent managed metadata facets", () => {it("preserves identity, members, and imports when embedded resources are malformed", () => {- const bytes = buildManagedPeFixture({ pinvoke: {}, readyToRun: true });+ const bytes = buildManagedPeFixture({+ mvid: Buffer.from("3aebc60edc4a544b1f458b4ed40b33b1", "hex"),+ pinvoke: {},+ readyToRun: true,+ });bytes.writeUInt32LE(0x7fff_0000, 0x0200 + 24);const target = managedPeFixtureTarget(bytes);const artifact = inspectManagedArtifactBytes(bytes, target);@@ -288,7 +292,7 @@ describe("independent managed metadata facets", () => {const native = inspectManagedNativeBoundariesBytes(bytes, target);for (const result of [artifact, members, native]) {- expect(result.module?.mvid).toBeTruthy();+ expect(result.module?.mvid).toBe("0ec6eb3a-4adc-4b54-1f45-8b4ed40b33b1");expect(result.metadata.status).toBe("partial");expect(result.coverage).toMatchObject({state: "partial",diff --git a/tests/boundary/filesystem/asarInventoryFreshness.test.ts b/tests/boundary/filesystem/asarInventoryFreshness.test.tsindex 24d778bfc..4b83676ca 100644--- a/tests/boundary/filesystem/asarInventoryFreshness.test.ts+++ b/tests/boundary/filesystem/asarInventoryFreshness.test.ts@@ -182,15 +182,13 @@ describe("ASAR snapshot operation ownership", () => {const removalStartedPromise = new Promise<void>((resolve) => {removalStarted = resolve;});- const reader = new AsarArtifactReader(- fixture.archive,- undefined,- async (path) => {+ const reader = new AsarArtifactReader(fixture.archive, {+ removeSnapshot: async (path) => {removalStarted?.();await removalGate;await rm(path, { recursive: true, force: true });},- );+ });let prepared = false;const preparing = reader.prepareContainer().then(() => {prepared = true;@@ -302,15 +300,13 @@ describe("ASAR captured-byte identity", () => {it("retains a completed inventory and snapshot ownership when cleanup fails", async () => {const fixture = await archiveFixture();const attempts: string[] = [];- const reader = new AsarArtifactReader(- fixture.archive,- undefined,- async (path) => {+ const reader = new AsarArtifactReader(fixture.archive, {+ removeSnapshot: async (path) => {attempts.push(path);if (attempts.length === 1) throw new Error("snapshot removal denied");await rm(path, { recursive: true, force: true });},- );+ });const resourceScope = new ArtifactResourceScope();onTestFinished(() => resourceScope.close());try {diff --git a/tests/boundary/filesystem/asarReaderCleanup.test.ts b/tests/boundary/filesystem/asarReaderCleanup.test.tsnew file mode 100644index 000000000..073ee1c7d--- /dev/null+++ b/tests/boundary/filesystem/asarReaderCleanup.test.ts@@ -0,0 +1,266 @@+import { fstatSync } from "node:fs";+import {+ lstat,+ mkdir,+ open as openFile,+ type FileHandle,+ writeFile,+} from "node:fs/promises";+import { dirname, join } from "node:path";++import { createPackage } from "@electron/asar";+import { describe, expect, it } from "vitest";++import { AsarArtifactReader } from "../../../src/artifacts/AsarArtifactReader.js";+import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";++const CONTENT = "module.exports = 'owned';\n";++const createArchive = async (content = CONTENT) => {+ const root = await createTestTempDirectory("rea-asar-reader-cleanup-");+ const source = join(root, "source");+ const archive = join(root, "fixture.asar");+ await mkdir(source);+ await writeFile(join(source, "main.js"), content);+ await createPackage(source, archive);+ return { archive };+};++type OpenedHandle = { readonly path: string; readonly fd: number };+type ReaderHooks = {+ readonly beforeAdoption?: (path: string, handle: FileHandle) => Promise<void>;+ readonly close?: (handle: FileHandle) => Promise<void>;+};++const createTrackedReader = (archive: string, hooks: ReaderHooks = {}) => {+ const opened: OpenedHandle[] = [];+ const reader = new AsarArtifactReader(archive, {+ openFile: async (path, flags) => {+ const handle = await openFile(path, flags);+ opened.push({ path, fd: handle.fd });+ await hooks.beforeAdoption?.(path, handle);+ return {+ get fd() {+ return handle.fd;+ },+ stat: () => handle.stat(),+ read: handle.read.bind(handle),+ close: () => hooks.close?.(handle) ?? handle.close(),+ };+ },+ });+ return { reader, opened };+};++const packedMember = async (reader: AsarArtifactReader) => {+ for await (const entry of reader.entries())+ if (entry.path === "main.js") return entry;+ throw new Error("Expected packed ASAR member");+};++const latestHandle = (opened: readonly OpenedHandle[]): OpenedHandle => {+ const handle = opened.at(-1);+ if (handle === undefined)+ throw new Error("Expected a descriptor for the packed member");+ return handle;+};++const openPackedMember = async (+ reader: AsarArtifactReader,+ opened: readonly OpenedHandle[],+) => {+ const output = await reader.open(await packedMember(reader));+ const handle = latestHandle(opened);+ return { output, handle, root: dirname(handle.path) };+};++const expectDescriptorOpen = (fd: number): void => {+ expect(fstatSync(fd).isFile()).toBe(true);+};++const expectDescriptorClosed = (fd: number): void => {+ expect(() => fstatSync(fd)).toThrow();+};++const expectSnapshotRemoved = async (+ fd: number,+ root: string,+): Promise<void> => {+ expectDescriptorClosed(fd);+ await expect(lstat(root)).rejects.toMatchObject({ code: "ENOENT" });+};++describe("ASAR reader cleanup", () => {+ it("reaps an unread packed member and its snapshot when the reader closes", async () => {+ const { archive } = await createArchive();+ const { reader, opened } = createTrackedReader(archive);++ try {+ const {+ output,+ handle: memberHandle,+ root,+ } = await openPackedMember(reader, opened);+ expect(memberHandle.path).toContain("rea-asar-snapshot-");+ expectDescriptorOpen(memberHandle.fd);+ expect(output.destroyed).toBe(false);++ await reader.close();++ await expectSnapshotRemoved(memberHandle.fd, root);+ } finally {+ await reader.close();+ }+ });++ it("retains the snapshot and facts after a failed close, then retries cleanup", async () => {+ const { archive } = await createArchive();+ let failNextClose = false;+ const closeFailure = Object.assign(new Error("injected close failure"), {+ code: "EIO",+ errno: -5,+ syscall: "close",+ });+ const { reader, opened } = createTrackedReader(archive, {+ close: async (handle) => {+ if (failNextClose) {+ failNextClose = false;+ throw closeFailure;+ }+ await handle.close();+ },+ });++ try {+ const { handle: memberHandle, root } = await openPackedMember(+ reader,+ opened,+ );+ failNextClose = true;++ await expect(reader.close()).rejects.toMatchObject({+ cleanup: { resources: [memberHandle.path] },+ });+ expectDescriptorOpen(memberHandle.fd);+ expect((await lstat(root)).isDirectory()).toBe(true);+ expect(await packedMember(reader)).toMatchObject({ path: "main.js" });++ await reader.close();++ await expectSnapshotRemoved(memberHandle.fd, root);+ } finally {+ await reader.close();+ }+ });++ it("closes a member handle adopted while reader shutdown is queued", async () => {+ const { archive } = await createArchive();+ let pauseMemberOpen = false;+ let memberOpenReached!: () => void;+ let releaseMemberOpen!: () => void;+ const memberOpenStarted = new Promise<void>((resolve) => {+ memberOpenReached = resolve;+ });+ const memberOpenGate = new Promise<void>((resolve) => {+ releaseMemberOpen = resolve;+ });+ const { reader, opened } = createTrackedReader(archive, {+ beforeAdoption: async (path) => {+ if (pauseMemberOpen && path.includes("rea-asar-snapshot-")) {+ pauseMemberOpen = false;+ memberOpenReached();+ await memberOpenGate;+ }+ },+ });++ try {+ const entry = await packedMember(reader);+ pauseMemberOpen = true;+ const opening = reader.open(entry);+ await memberOpenStarted;+ const closing = reader.close();+ releaseMemberOpen();+ const output = await opening;+ const memberHandle = latestHandle(opened);+ const root = dirname(memberHandle.path);++ await closing;++ expect(output.destroyed).toBe(true);+ await expectSnapshotRemoved(memberHandle.fd, root);+ } finally {+ releaseMemberOpen();+ await reader.close();+ }+ });+});++describe("ASAR preparation cleanup", () => {+ it("retains a zero-byte member handle when its first close fails", async () => {+ const { archive } = await createArchive("");+ let failNextClose = false;+ const { reader, opened } = createTrackedReader(archive, {+ close: async (handle) => {+ if (failNextClose) {+ failNextClose = false;+ throw new Error("empty member close denied");+ }+ await handle.close();+ },+ });+ try {+ const entry = await packedMember(reader);+ failNextClose = true;+ await expect(reader.open(entry)).rejects.toMatchObject({+ cleanup: {+ reason: "empty member close denied",+ resources: [expect.any(String)],+ },+ });+ const handle = latestHandle(opened);+ expectDescriptorOpen(handle.fd);+ expect((await lstat(dirname(handle.path))).isDirectory()).toBe(true);+ await reader.close();+ await expectSnapshotRemoved(handle.fd, dirname(handle.path));+ } finally {+ await reader.close();+ }+ });++ it("preserves a malformed header failure when closing its handle also fails", async () => {+ const { archive } = await createArchive();+ await writeFile(archive, Buffer.alloc(16));+ let failHeaderClose = false;+ const closeFailure = new Error("header close denied");+ const { reader, opened } = createTrackedReader(archive, {+ beforeAdoption: async (path) => {+ if (path !== archive) failHeaderClose = true;+ },+ close: async (handle) => {+ if (failHeaderClose) {+ failHeaderClose = false;+ throw closeFailure;+ }+ await handle.close();+ },+ });+ try {+ await expect(packedMember(reader)).rejects.toMatchObject({+ reason: "format",+ message: "Truncated or invalid ASAR size pickle",+ cleanup: {+ reason: closeFailure.message,+ resources: [expect.any(String)],+ },+ });+ const handle = latestHandle(opened);+ expectDescriptorOpen(handle.fd);+ expect((await lstat(dirname(handle.path))).isDirectory()).toBe(true);+ await reader.close();+ await expectSnapshotRemoved(handle.fd, dirname(handle.path));+ } finally {+ await reader.close();+ }+ });+});diff --git a/tests/boundary/filesystem/asarStreamingAllocation.test.ts b/tests/boundary/filesystem/asarStreamingAllocation.test.tsindex 0b740d484..e8c46bd8c 100644--- a/tests/boundary/filesystem/asarStreamingAllocation.test.ts+++ b/tests/boundary/filesystem/asarStreamingAllocation.test.ts@@ -9,11 +9,11 @@ import {writeFile,truncate,} from "node:fs/promises";-import { join } from "node:path";+import { dirname, join } from "node:path";import { Readable } from "node:stream";import { buffer } from "node:stream/consumers";import { createPackageWithOptions } from "@electron/asar";-import { describe, expect, it } from "vitest";+import { describe, expect, it, onTestFinished } from "vitest";import { scanArtifactInventory } from "../../fixtures/artifactInventory.js";import { AsarArtifactReader } from "../../../src/artifacts/AsarArtifactReader.js";import {@@ -105,7 +105,13 @@ describe("ASAR entry streaming", () => {await writeFile(join(source, "small.js"), "module.exports = 1;\n");await createPackageWithOptions(source, archive, {});- const reader = new AsarArtifactReader(archive);+ let snapshotPath: string | undefined;+ const reader = new AsarArtifactReader(archive, {+ openFile: async (path, flags) => {+ if (path !== archive) snapshotPath = path;+ return openFile(path, flags);+ },+ });try {const entries = [];@@ -115,9 +121,11 @@ describe("ASAR entry streaming", () => {const entry = entries.find(({ path }) => path === "small.js");if (entry === undefined) throw new Error("Expected packed ASAR member");const snapshot = await reader.openContainer();- const identity = await lstat(snapshot.path);+ if (snapshotPath === undefined)+ throw new Error("Expected snapshot acquisition");+ const identity = await lstat(snapshotPath);await buffer(snapshot);- const probe = await openFile(snapshot.path, "r");+ const probe = await openFile(snapshotPath, "r");expect(await matchingDescriptorCount(identity)).toBeGreaterThan(0);await probe.close();expect(await matchingDescriptorCount(identity)).toBe(0);@@ -145,7 +153,13 @@ describe("ASAR entry streaming", () => {await writeFile(join(source, "small.js"), "module.exports = 1;\n");await createPackageWithOptions(source, archive, {});- const reader = new AsarArtifactReader(archive);+ let snapshotPath: string | undefined;+ const reader = new AsarArtifactReader(archive, {+ openFile: async (path, flags) => {+ if (path !== archive) snapshotPath = path;+ return openFile(path, flags);+ },+ });try {const entries = [];@@ -153,7 +167,9 @@ describe("ASAR entry streaming", () => {const entry = entries.find(({ path }) => path === "small.js");if (entry === undefined) throw new Error("Expected packed ASAR member");const snapshot = await reader.openContainer();- const identity = await lstat(snapshot.path);+ if (snapshotPath === undefined)+ throw new Error("Expected snapshot acquisition");+ const identity = await lstat(snapshotPath);await buffer(snapshot);const controller = new AbortController();const opening = reader.open(entry, controller.signal);@@ -340,23 +356,25 @@ describe("ASAR read failures", () => {syscall: "close",});let closeCount = 0;- const reader = new AsarArtifactReader(archive, async (path, flags) => {- const handle = await openFile(path, flags);- expect(await matchingDescriptorCount(identity)).toBeGreaterThan(0);- return {- get fd() {- return handle.fd;- },- async stat() {- throw ioError;- },- async close() {- closeCount += 1;- if (closeCount === 1) throw closeError;- await handle.close();- },- read: handle.read.bind(handle),- };+ const reader = new AsarArtifactReader(archive, {+ openUnpackedFile: async (path, flags) => {+ const handle = await openFile(path, flags);+ expect(await matchingDescriptorCount(identity)).toBeGreaterThan(0);+ return {+ get fd() {+ return handle.fd;+ },+ async stat() {+ throw ioError;+ },+ async close() {+ closeCount += 1;+ if (closeCount === 1) throw closeError;+ await handle.close();+ },+ read: handle.read.bind(handle),+ };+ },});try {@@ -396,24 +414,37 @@ it.skipIf(process.platform === "win32")(});const closeFailure = new Error("native close outcome unavailable");let closeCount = 0;- const reader = new AsarArtifactReader(archive, async (path, flags) => {- const handle = await openFile(path, flags);- return {- get fd() {- return handle.fd;- },- async stat() {- throw statFailure;- },- async close() {- closeCount += 1;- // Model Node's invalidated-descriptor rejection without leaking a- // native descriptor in the test process.- await handle.close();- throw closeFailure;- },- read: handle.read.bind(handle),- };+ let snapshotRoot: string | undefined;+ const reader = new AsarArtifactReader(archive, {+ openFile: async (path, flags) => {+ if (path !== archive && snapshotRoot === undefined) {+ const root = dirname(path);+ snapshotRoot = root;+ // This fixture deliberately reports an unrecoverable close outcome.+ // Remove its retained snapshot only after testing reader ownership.+ onTestFinished(() => rm(root, { recursive: true, force: true }));+ }+ return openFile(path, flags);+ },+ openUnpackedFile: async (path, flags) => {+ const handle = await openFile(path, flags);+ return {+ get fd() {+ return handle.fd;+ },+ async stat() {+ throw statFailure;+ },+ async close() {+ closeCount += 1;+ // Model Node's invalidated-descriptor rejection without leaking a+ // native descriptor in the test process.+ await handle.close();+ throw closeFailure;+ },+ read: handle.read.bind(handle),+ };+ },});const entry = await unpackedMember(reader);await expect(reader.open(entry)).rejects.toMatchObject({@@ -430,6 +461,8 @@ it.skipIf(process.platform === "win32")(});expect(closeCount).toBe(1);await waitForNoMatchingDescriptor(identity);+ if (snapshotRoot === undefined) throw new Error("Expected ASAR snapshot");+ expect((await lstat(snapshotRoot)).isDirectory()).toBe(true);},);@@ -439,24 +472,26 @@ it.skipIf(process.platform === "win32")(const { archive, unpackedPath, identity } = await createUnpackedFixture();const controller = new AbortController();let closeCount = 0;- const reader = new AsarArtifactReader(archive, async (path, flags) => {- const handle = await openFile(path, flags);- return {- get fd() {- return handle.fd;- },- async stat() {- const observed = await handle.stat();- controller.abort();- return observed;- },- async close() {- closeCount += 1;- if (closeCount === 1) throw new Error("first close failed");- await handle.close();- },- read: handle.read.bind(handle),- };+ const reader = new AsarArtifactReader(archive, {+ openUnpackedFile: async (path, flags) => {+ const handle = await openFile(path, flags);+ return {+ get fd() {+ return handle.fd;+ },+ async stat() {+ const observed = await handle.stat();+ controller.abort();+ return observed;+ },+ async close() {+ closeCount += 1;+ if (closeCount === 1) throw new Error("first close failed");+ await handle.close();+ },+ read: handle.read.bind(handle),+ };+ },});try {@@ -484,25 +519,27 @@ it.skipIf(process.platform === "win32")(const { archive, unpackedPath, identity } = await createUnpackedFixture();const closeError = new Error("first close failed");let closeCount = 0;- const reader = new AsarArtifactReader(archive, async (path, flags) => {- const handle = await openFile(path, flags);- return {- get fd() {- return handle.fd;- },- async stat() {- const observed = await handle.stat();- return Object.assign(Object.create(observed), {- ino: observed.ino + 1,- });- },- async close() {- closeCount += 1;- if (closeCount === 1) throw closeError;- await handle.close();- },- read: handle.read.bind(handle),- };+ const reader = new AsarArtifactReader(archive, {+ openUnpackedFile: async (path, flags) => {+ const handle = await openFile(path, flags);+ return {+ get fd() {+ return handle.fd;+ },+ async stat() {+ const observed = await handle.stat();+ return Object.assign(Object.create(observed), {+ ino: observed.ino + 1,+ });+ },+ async close() {+ closeCount += 1;+ if (closeCount === 1) throw closeError;+ await handle.close();+ },+ read: handle.read.bind(handle),+ };+ },});try {@@ -527,16 +564,15 @@ it.skipIf(process.platform === "win32")();it.skipIf(process.platform === "win32")(- "retains unpacked handles and snapshots across combined cleanup failures",+ "retains unpacked handles before retrying snapshot cleanup failures",async () => {const { archive, unpackedPath, identity } = await createUnpackedFixture();const closeError = new Error("unpacked close denied");const snapshotError = new Error("snapshot removal denied");let closeCount = 0;const snapshotPaths: string[] = [];- const reader = new AsarArtifactReader(- archive,- async (path, flags) => {+ const reader = new AsarArtifactReader(archive, {+ openUnpackedFile: async (path, flags) => {const handle = await openFile(path, flags);return {get fd() {@@ -557,12 +593,12 @@ it.skipIf(process.platform === "win32")(read: handle.read.bind(handle),};},- async (path) => {+ removeSnapshot: async (path) => {snapshotPaths.push(path);if (snapshotPaths.length === 1) throw snapshotError;await rm(path, { recursive: true, force: true });},- );+ });try {const entry = await unpackedMember(reader);@@ -579,19 +615,24 @@ it.skipIf(process.platform === "win32")();expect(cleanupFailure).toMatchObject({cleanup: {- resources: [unpackedPath, expect.any(String)],+ resources: [unpackedPath],reason: expect.stringContaining("unpacked close denied"),},});+ expect(closeCount).toBe(2);+ expect(snapshotPaths).toEqual([]);+ expect(await matchingDescriptorCount(identity)).toBeGreaterThan(0);++ await expect(reader.close()).rejects.toMatchObject({+ message: snapshotError.message,+ cleanup: { resources: [expect.any(String)] },+ });+ expect(closeCount).toBe(3);+ await waitForNoMatchingDescriptor(identity);const snapshotRoot = snapshotPaths[0];if (snapshotRoot === undefined)throw new Error("Expected owned ASAR snapshot root");expect(snapshotPaths).toEqual([snapshotRoot]);- expect(cleanupFailure).toMatchObject({- cleanup: { resources: [unpackedPath, snapshotRoot] },- });- expect(closeCount).toBe(2);- expect(await matchingDescriptorCount(identity)).toBeGreaterThan(0);expect((await lstat(snapshotRoot)).isDirectory()).toBe(true);await reader.close();diff --git a/tests/boundary/process/providerProcess.test.ts b/tests/boundary/process/providerProcess.test.tsindex b2813ce46..068f6becb 100644--- a/tests/boundary/process/providerProcess.test.ts+++ b/tests/boundary/process/providerProcess.test.ts@@ -285,12 +285,17 @@ describe("provider process output and cleanup primitives", () => {await stopProviderProcessFixture(child);}});+});+describe("provider process cleanup authority", () => {it("honors verified group cleanup instead of direct process signaling", async () => {const child = spawnProviderProcessFixture("stubborn");- await waitForProviderProcessReady(child);+ const signalFromCleanup = child.kill.bind(child);+ const directSignal = vi.spyOn(child, "kill");const cleanup = vi.fn(async () => {- child.kill("SIGKILL");+ // Keep the injected cleanup signal separate from the supervisor's+ // direct method, while retaining the child handle's lifetime checks.+ signalFromCleanup("SIGKILL");return { cleaned: true, signaled: true } as const;});const supervisor = new ProviderProcessSupervisor({@@ -299,12 +304,19 @@ describe("provider process output and cleanup primitives", () => {cleanup,});- await expect(supervisor.stop({ killGraceMs: 500 })).resolves.toEqual({- status: "verified-cleanup",- });- // Cleanup must be attempted. The exact attempt count is an internal retry- // ladder, so it is deliberately not pinned here.- expect(cleanup).toHaveBeenCalled();+ try {+ await waitForProviderProcessReady(child);+ await expect(supervisor.stop({ killGraceMs: 500 })).resolves.toEqual({+ status: "verified-cleanup",+ });+ // Cleanup must be attempted. The exact attempt count is an internal+ // retry ladder, so it is deliberately not pinned here.+ expect(cleanup).toHaveBeenCalled();+ expect(directSignal).not.toHaveBeenCalled();+ } finally {+ directSignal.mockRestore();+ await stopProviderProcessFixture(child);+ }});it("cleans an owned group even after its launcher leader has exited", async () => {