fix(io): consolidate managed snapshots and retain cleanup ownership

Reuse managed snapshot admission and hashing in sequential path comparisons, retaining caller paths and completed inspection Evidence on later failure. Preserve exact failed-close owners alongside typed read and cancellation failures, and retry them through caller-owned resource scopes. Require canonical Hopper lease identities, remove signed offset coercion, and repair stale setup cleanup expectations that failed CI.
This commit is contained in:
morluto
2026-10-12 14:46:37 +08:00
parent e9970855ba
commit 98d8470d36
16 changed files with 899 additions and 225 deletions
+84 -9
View File
@@ -1,8 +1,17 @@
import { execFile } from "node:child_process";
import { once } from "node:events";
import { lstat, mkdir, mkdtemp, readdir, rm, rmdir } from "node:fs/promises";
import {
lstat,
mkdir,
mkdtemp,
readdir,
rm,
rmdir,
symlink,
writeFile,
} from "node:fs/promises";
import { createServer, type Socket } from "node:net";
import { join } from "node:path";
import { dirname, join } from "node:path";
import { promisify } from "node:util";
import { afterEach, describe, expect, it } from "vitest";
@@ -25,8 +34,9 @@ afterEach(async () => {
describe("Hopper target leases", () => {
it("reports the owning REA session for a duplicate target and profile", async () => {
const directory = await temporaryDirectory();
const targetPath = await temporaryTarget();
const first = await acquireHopperTargetLease({
...leaseInput,
...leaseInput(targetPath),
directory,
runId: "session-first",
});
@@ -34,7 +44,7 @@ describe("Hopper target leases", () => {
if (!first.acquired) return;
const duplicate = await acquireHopperTargetLease({
...leaseInput,
...leaseInput(targetPath),
directory,
runId: "session-second",
});
@@ -45,7 +55,7 @@ describe("Hopper target leases", () => {
await first.lease.release();
const reopened = await acquireHopperTargetLease({
...leaseInput,
...leaseInput(targetPath),
directory,
runId: "session-third",
});
@@ -55,13 +65,14 @@ describe("Hopper target leases", () => {
it("keeps different Hopper profiles independent", async () => {
const directory = await temporaryDirectory();
const targetPath = await temporaryTarget();
const first = await acquireHopperTargetLease({
...leaseInput,
...leaseInput(targetPath),
directory,
runId: "session-one",
});
const second = await acquireHopperTargetLease({
...leaseInput,
...leaseInput(targetPath),
directory,
loaderArgs: ["--aarch64"],
runId: "session-two",
@@ -71,6 +82,63 @@ describe("Hopper target leases", () => {
if (first.acquired) await first.lease.release();
if (second.acquired) await second.lease.release();
});
it.skipIf(process.platform === "win32")(
"shares one lease for symlink aliases of the same target",
async () => {
const directory = await temporaryDirectory();
const targetPath = await temporaryTarget();
const alias = join(dirname(targetPath), "sample-alias");
await symlink(targetPath, alias);
const first = await acquireHopperTargetLease({
...leaseInput(targetPath),
directory,
runId: "canonical-owner",
});
expect(first.acquired).toBe(true);
if (!first.acquired) return;
try {
expect(
await acquireHopperTargetLease({
...leaseInput(alias),
directory,
runId: "alias-request",
}),
).toMatchObject({
acquired: false,
owner: { runId: "canonical-owner" },
});
} finally {
await first.lease.release();
}
},
);
it.each(["missing", "not-directory"] as const)(
"does not create a lease endpoint when target canonicalization fails for %s",
async (failure) => {
const directory = await temporaryDirectory();
const parent = await temporaryDirectory();
const targetPath =
failure === "missing"
? join(parent, "missing-target")
: join(await temporaryTarget(parent), "child");
await expect(
acquireHopperTargetLease({
...leaseInput(targetPath),
directory,
runId: "unresolvable-target",
}),
).rejects.toMatchObject({
_tag: "HopperStartError",
userMessage: expect.stringContaining("canonical path"),
cause: expect.objectContaining({
code: failure === "missing" ? "ENOENT" : "ENOTDIR",
}),
});
expect(await readdir(directory)).toEqual([]);
},
);
});
describe("Linux Hopper application leases", () => {
@@ -212,10 +280,17 @@ const releasedApplicationSocket = async (
return join(directory, name);
};
const leaseInput = {
targetPath: "/tmp/sample-binary",
const leaseInput = (targetPath: string) => ({
targetPath,
targetKind: "executable" as const,
loaderArgs: [],
});
const temporaryTarget = async (parent?: string): Promise<string> => {
const directory = parent ?? (await temporaryDirectory());
const targetPath = join(directory, "sample-binary");
await writeFile(targetPath, "admitted target bytes");
return targetPath;
};
const temporaryDirectory = async (): Promise<string> => {
+12 -11
View File
@@ -6,7 +6,7 @@ import {
type Server,
type Socket,
} from "node:net";
import { join, resolve } from "node:path";
import { join } from "node:path";
import { HopperStartError } from "../domain/hopperErrors.js";
import { safeParseJson } from "../domain/safeJson.js";
@@ -37,16 +37,17 @@ export const acquireHopperTargetLease = async (input: {
readonly runId: string;
readonly directory?: string;
}): Promise<HopperTargetLeaseAcquisition> => {
// The canonical path only derives the lease key; it never authorizes access.
// Common expected `realpath` failures include a missing target or path
// component (ENOENT, ENOTDIR), an unreadable component (EACCES), or a
// symlink loop (ELOOP). This catch also falls back for every other error,
// which may hide a real filesystem problem. The lexical `resolve` fallback
// still yields a deterministic key, but aliases may take separate leases
// whenever `realpath` cannot resolve them.
const targetPath = await realpath(input.targetPath).catch(() =>
resolve(input.targetPath),
);
// Canonicalization is required for lease identity. A lexical fallback could
// let two aliases reserve independent sockets for the same Hopper document.
let targetPath: string;
try {
targetPath = await realpath(input.targetPath);
} catch (cause: unknown) {
throw new HopperStartError({
cause,
userMessage: `REA could not resolve Hopper target ${JSON.stringify(input.targetPath)} to a canonical path before reserving the document. Confirm that the target exists and its path is accessible.`,
});
}
return acquireLease({
...input,
identity: {