fix(hopper): wait for late bridges during cleanup (#1866)

This commit is contained in:
morluto
2026-10-11 22:50:27 +08:00
committed by GitHub
parent 1f531fc257
commit cf7477eb46
8 changed files with 323 additions and 205 deletions
@@ -29,6 +29,7 @@ describe("Hopper launcher failure through the one-shot CLI", () => {
// Hopper target leases are acquired only for the macOS launcher route.
workspaceCliTest.skipIf(process.platform !== "darwin")(
"exits after a retained-cleanup failure and releases the target to a retry",
{ timeout: 90_000 },
async ({ cli, workspace }) => {
const launcherPath = await workspace.write(
"failure-helper",
@@ -58,7 +59,7 @@ describe("Hopper launcher failure through the one-shot CLI", () => {
HOPPER_LOADER_ARGS_JSON: JSON.stringify(loaderArgs),
REA_LOG_LEVEL: "silent",
},
timeoutMs: 30_000,
timeoutMs: 45_000,
});
const retained = RetainedRuntime.safeParse(result.json);
if (retained.success)
@@ -82,7 +82,7 @@ const clients: HopperClient[] = [];
afterEach(async () => {
await Promise.all(clients.splice(0).map((client) => client.close()));
});
}, 45_000);
describe("HopperClient cleanup", () => {
it("waits for an operation reply without a fixed request deadline", async () => {
@@ -1,5 +1,7 @@
import { spawn, type ChildProcess } from "node:child_process";
import { execFile, spawn, type ChildProcess } from "node:child_process";
import { access, rm } from "node:fs/promises";
import { setTimeout as delay } from "node:timers/promises";
import { promisify } from "node:util";
import { expect, it, onTestFinished, vi } from "vitest";
@@ -64,19 +66,85 @@ const setup = (
const launcher = new ExternalBridgeLauncher(identity, healthMode);
const client = new HopperClient({ launcher, startupTimeoutMs: 2_000 });
onTestFinished(async () => {
await client.close();
await Promise.all(
[...launcher.helpers, ...launcher.fixture.processes].map(stop),
);
await client.close();
await Promise.all(
launcher.sessions.map(({ directory }) =>
rm(directory, { recursive: true, force: true }),
),
);
});
}, 45_000);
return { client, launcher };
};
it.each(["missing", "not-listening"] as const)(
"waits for a matching bridge published during close after a %s socket failure",
async (socketState) => {
const fixture = new HopperFixtureLauncher();
let session: BridgeSession | undefined;
let helper: ChildProcess | undefined;
const client = new HopperClient({
startupTimeoutMs: 2_000,
launcher: {
launch(value) {
session = value;
helper = spawn(process.execPath, ["-e", "process.exit(0)"], {
stdio: "ignore",
});
return Promise.resolve(
ok({
process: helper,
ownsProcessLifetime: false as const,
providerLifetime: "external-application" as const,
shutdownMode: "bridge-request" as const,
}),
);
},
},
});
onTestFinished(async () => {
await Promise.all(
[...(helper === undefined ? [] : [helper]), ...fixture.processes].map(
stop,
),
);
if (session !== undefined)
await rm(session.directory, { recursive: true, force: true });
});
const started = await client.start();
if (started.ok) throw new Error("Expected readiness to time out");
expect(projectAnalysisError(started.error)).toMatchObject({
code: "cleanup_incomplete",
details: { diagnostics: { primary_error: { code: "provider_timeout" } } },
});
if (session === undefined) throw new Error("Fixture did not launch");
if (socketState === "not-listening")
await promisify(execFile)(process.execPath, [
"-e",
'require("node:net").createServer().listen(process.argv[1], () => process.exit(0))',
session.socketPath,
]);
const closing = client.close();
// Publish after close has encountered an unavailable endpoint, within its
// cleanup window rather than before the caller requests cleanup.
await delay(150);
await rm(session.socketPath, { force: true });
await fixture.launch(session);
const closed = await closing;
if (!closed.ok) throw closed.error;
expect(fixture.requests.map(({ method }) => method)).toEqual([
"health",
"shutdown_document",
]);
await vi.waitFor(() => expect(fixture.processes[0]?.exitCode).toBe(0));
await expect(access(session.directory)).rejects.toMatchObject({
code: "ENOENT",
});
},
);
it.each(["wrong-run", "wrong-token"] as const)(
"never requests document shutdown after the first handshake rejects %s",
async (identity) => {
@@ -55,7 +55,7 @@ const start = async (owned: boolean) => {
onTestFinished(async () => {
await client.close();
for (const child of launcher.fixture.processes) child.kill();
});
}, 45_000);
expect((await client.start()).ok).toBe(true);
return { client, launcher };
};
@@ -35,7 +35,7 @@ it("does not equate an owned native helper exit with an external GUI exit", asyn
const client = new HopperClient({ launcher, startupTimeoutMs: 10_000 });
onTestFinished(async () => {
await client.close();
});
}, 45_000);
const result = await client.start();
expect(result).toMatchObject({
ok: false,
@@ -87,7 +87,7 @@ it.each(["exit", "signal", "exit75"] as const)(
const client = new HopperClient({ launcher, startupTimeoutMs: 10_000 });
onTestFinished(async () => {
await client.close();
});
}, 45_000);
const result = await client.start();
expect(result).toMatchObject({
ok: false,
@@ -139,7 +139,7 @@ it.each([0, 1])(
const client = new HopperClient({ launcher, startupTimeoutMs: 10_000 });
onTestFinished(async () => {
await client.close();
});
}, 45_000);
const result = await client.start();
expect(result).toMatchObject({
ok: false,
@@ -177,7 +177,7 @@ it("preserves a successful helper's diagnostics when bridge readiness is never o
const client = new HopperClient({ launcher, startupTimeoutMs: 1500 });
onTestFinished(async () => {
await client.close();
});
}, 45_000);
const result = await client.start();
if (result.ok)
throw new Error("Expected missing bridge readiness to time out");
@@ -128,7 +128,7 @@ const clients: HopperClient[] = [];
afterEach(async () => {
await Promise.all(clients.splice(0).map((client) => client.close()));
});
}, 45_000);
describe("HopperClient startup deadline", () => {
it("applies one deadline to launcher, socket, and health startup phases", async () => {