fix(artifacts): retain ASAR resources through cleanup failures
Give snapshot capture, header admission, packed members, and container streams one file-handle owner. Serialize adoption with reader shutdown, retain failed closes for retry, and remove snapshots only after descriptor cleanup succeeds. Cover unread members, pending opens, zero-byte members, and paired format/cleanup failures. Keep intentionally unrecoverable test fixtures hygienic and strengthen managed identity and process cleanup-authority expectations. Validation: full npm run check; 99 targeted tests and temporary-directory hygiene; exact discovery of 877 test files. No real vendor-provider verification was performed.
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user