fix(artifacts): serialize ASAR snapshot ownership
Serialize preparation and close for each reader so concurrent callers share one owned snapshot. Preserve per-call cancellation and expected digest checks, cleanup retry ownership, and reuse after close. Validation: 34 ASAR boundary tests, one fork-isolated snapshot race regression, test discovery, and check:fast passed.
This commit is contained in:
@@ -171,6 +171,54 @@ describe("ASAR inventory freshness", () => {
|
||||
);
|
||||
});
|
||||
|
||||
describe("ASAR snapshot operation ownership", () => {
|
||||
it("waits for preparation before close and cancels a queued caller", async () => {
|
||||
const fixture = await archiveFixture();
|
||||
let allowRemoval: (() => void) | undefined;
|
||||
let removalStarted: (() => void) | undefined;
|
||||
const removalGate = new Promise<void>((resolve) => {
|
||||
allowRemoval = resolve;
|
||||
});
|
||||
const removalStartedPromise = new Promise<void>((resolve) => {
|
||||
removalStarted = resolve;
|
||||
});
|
||||
const reader = new AsarArtifactReader(
|
||||
fixture.archive,
|
||||
undefined,
|
||||
async (path) => {
|
||||
removalStarted?.();
|
||||
await removalGate;
|
||||
await rm(path, { recursive: true, force: true });
|
||||
},
|
||||
);
|
||||
let prepared = false;
|
||||
const preparing = reader.prepareContainer().then(() => {
|
||||
prepared = true;
|
||||
});
|
||||
const closing = reader.close();
|
||||
const controller = new AbortController();
|
||||
try {
|
||||
const closeReachedRemoval = Promise.race([
|
||||
removalStartedPromise,
|
||||
closing.then(() => {
|
||||
throw new Error("close completed before snapshot removal began");
|
||||
}),
|
||||
]);
|
||||
await Promise.all([preparing, closeReachedRemoval]);
|
||||
expect(prepared).toBe(true);
|
||||
const queued = reader.prepareContainer(undefined, controller.signal);
|
||||
controller.abort();
|
||||
await expect(queued).rejects.toMatchObject({ reason: "cancelled" });
|
||||
allowRemoval?.();
|
||||
await Promise.all([preparing, closing]);
|
||||
await expect(reader.prepareContainer()).resolves.toBeUndefined();
|
||||
} finally {
|
||||
allowRemoval?.();
|
||||
await reader.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("ASAR captured-byte identity", () => {
|
||||
it("rejects same-size nested-container replacement between hashing and interpretation", async () => {
|
||||
const fixture = await archiveFixture();
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { mkdir, readFile, readdir, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { buffer } from "node:stream/consumers";
|
||||
|
||||
import { createPackage } from "@electron/asar";
|
||||
import { afterEach, expect, it, vi } from "vitest";
|
||||
|
||||
import { AsarArtifactReader } from "../../src/artifacts/AsarArtifactReader.js";
|
||||
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllEnvs();
|
||||
});
|
||||
|
||||
it("serializes concurrent ASAR snapshot preparation and cleans its single owned root", async () => {
|
||||
const root = await createTestTempDirectory("rea-asar-snapshot-owner-");
|
||||
vi.stubEnv("TMPDIR", root);
|
||||
vi.stubEnv("TMP", root);
|
||||
vi.stubEnv("TEMP", root);
|
||||
|
||||
const source = join(root, "source");
|
||||
const archive = join(root, "app.asar");
|
||||
await mkdir(source);
|
||||
await writeFile(join(source, "main.js"), "module.exports = 42;\n");
|
||||
await createPackage(source, archive);
|
||||
|
||||
const reader = new AsarArtifactReader(archive);
|
||||
const iterator = reader.entries()[Symbol.asyncIterator]();
|
||||
try {
|
||||
const expectedDigest = createHash("sha256")
|
||||
.update(await readFile(archive))
|
||||
.digest("hex");
|
||||
const openingContainer = reader.openContainer();
|
||||
const readingEntry = iterator.next();
|
||||
const preparingSnapshot = reader.prepareContainer(expectedDigest);
|
||||
const [snapshot, firstEntry] = await Promise.all([
|
||||
openingContainer,
|
||||
readingEntry,
|
||||
preparingSnapshot,
|
||||
]);
|
||||
if (firstEntry.done) throw new Error("Expected an ASAR member");
|
||||
expect(await buffer(snapshot)).toEqual(await readFile(archive));
|
||||
|
||||
const mismatch = reader.prepareContainer("0".repeat(64));
|
||||
const matching = reader.prepareContainer(expectedDigest);
|
||||
await expect(mismatch).rejects.toMatchObject({ reason: "integrity" });
|
||||
await expect(matching).resolves.toBeUndefined();
|
||||
|
||||
const snapshotRoots = async (): Promise<string[]> =>
|
||||
(await readdir(root)).filter((name) =>
|
||||
name.startsWith("rea-asar-snapshot-"),
|
||||
);
|
||||
expect(await snapshotRoots()).toHaveLength(1);
|
||||
await reader.close();
|
||||
expect(await snapshotRoots()).toEqual([]);
|
||||
} finally {
|
||||
try {
|
||||
await iterator.return?.();
|
||||
} finally {
|
||||
await reader.close();
|
||||
}
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user