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
+25 -5
View File
@@ -34,7 +34,10 @@ import type { ProgressReporter } from "../application/ProgressReporter.js";
import { silentLogger } from "../logger.js"; import { silentLogger } from "../logger.js";
import type { Logger } from "pino"; import type { Logger } from "pino";
import { PrivateRuntimeRoot } from "../process/PrivateRuntimeRoot.js"; import { PrivateRuntimeRoot } from "../process/PrivateRuntimeRoot.js";
import { ProviderStartupDeadline } from "../process/ProviderDeadline.js"; import {
ProviderStartupDeadline,
waitForAbortableDelay,
} from "../process/ProviderDeadline.js";
import { redactCapturedTransportCredential } from "../process/ProviderDiagnosticRedaction.js"; import { redactCapturedTransportCredential } from "../process/ProviderDiagnosticRedaction.js";
import { ProviderRunLineage } from "../process/ProviderRunLineage.js"; import { ProviderRunLineage } from "../process/ProviderRunLineage.js";
import { import {
@@ -423,7 +426,7 @@ export class HopperClient {
); );
if (!health.ok) { if (!health.ok) {
return this.#startupFailure( return this.#startupFailure(
deadline.signal.aborted deadline.signal.aborted || health.error instanceof HopperTimeoutError
? await this.#startupInterruption(deadline) ? await this.#startupInterruption(deadline)
: health.error, : health.error,
); );
@@ -542,11 +545,28 @@ export class HopperClient {
); );
this.#closing = true; this.#closing = true;
try { try {
const connected = await connectHopperSocketOnce( const socketPath = join(root.path, "bridge.sock");
join(root.path, "bridge.sock"), let connected = await connectHopperSocketOnce(
socketPath,
controller.signal, controller.signal,
); );
if (!connected.ok) return; while (!connected.ok) {
const cause = connected.error.cause;
if (
!(
cause instanceof Error &&
"code" in cause &&
(cause.code === "ENOENT" || cause.code === "ECONNREFUSED")
)
)
return;
if ((await waitForAbortableDelay(50, controller.signal)) === "aborted")
return;
connected = await connectHopperSocketOnce(
socketPath,
controller.signal,
);
}
const socket = connected.value; const socket = connected.value;
this.#socket = socket; this.#socket = socket;
this.#attachSocket(socket); this.#attachSocket(socket);
@@ -1,6 +1,7 @@
import { spawn, type ChildProcess } from "node:child_process"; import { spawn, type ChildProcess } from "node:child_process";
import { access, copyFile, readFile, rm, writeFile } from "node:fs/promises"; import { access, copyFile, readFile, rm, writeFile } from "node:fs/promises";
import { dirname, resolve } from "node:path"; import { dirname, resolve } from "node:path";
import { setTimeout as delay } from "node:timers/promises";
import { Client } from "@modelcontextprotocol/client"; import { Client } from "@modelcontextprotocol/client";
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio"; import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
@@ -14,9 +15,10 @@ import { acquireHopperTargetLease } from "../../../src/hopper/HopperTargetLease.
const statusSchema = z.object({ result: z.object({ open: z.boolean() }) }); const statusSchema = z.object({ result: z.object({ open: z.boolean() }) });
const test = workspaceCliTest.skipIf(process.platform !== "darwin"); const test = workspaceCliTest.skipIf(process.platform !== "darwin");
test("retains a denial-shaped failure when caller arguments can alter the helper's AppleScript", async ({ test(
workspace, "retains a denial-shaped failure when caller arguments can alter the helper's AppleScript",
}) => { { timeout: 90_000 },
async ({ workspace }) => {
for (const source of ["target", "loader"] as const) { for (const source of ["target", "loader"] as const) {
const target = workspace.path( const target = workspace.path(
source === "target" ? 'quoted"target' : "loader-target", source === "target" ? 'quoted"target' : "loader-target",
@@ -49,8 +51,21 @@ test("retains a denial-shaped failure when caller arguments can alter the helper
const mcp = new Client({ name: "hopper-literal-boundary", version: "1" }); const mcp = new Client({ name: "hopper-literal-boundary", version: "1" });
let runtime: string | undefined; let runtime: string | undefined;
onTestFinished(async () => { onTestFinished(async () => {
// SDK close can return immediately after SIGKILL. Wait for its actual
// child-close event before recovering this process's retained lease.
let serverExited: Promise<void> | undefined;
if (transport.pid !== null) {
const onclose = transport.onclose;
serverExited = new Promise<void>((resolveExit) => {
transport.onclose = () => {
onclose?.();
resolveExit();
};
});
}
await mcp.close(); await mcp.close();
await transport.close(); await transport.close();
await serverExited;
// The fixture created only this marker, never a native GUI document. // The fixture created only this marker, never a native GUI document.
if (runtime !== undefined) if (runtime !== undefined)
await rm(runtime, { recursive: true, force: true }); await rm(runtime, { recursive: true, force: true });
@@ -98,7 +113,8 @@ test("retains a denial-shaped failure when caller arguments can alter the helper
"cleanup_incomplete", "cleanup_incomplete",
); );
} }
}); },
);
test("recovers a denied Hopper launch through the same stdio MCP session and CLI", async ({ test("recovers a denied Hopper launch through the same stdio MCP session and CLI", async ({
workspace, workspace,
@@ -185,9 +201,10 @@ test("recovers a denied Hopper launch through the same stdio MCP session and CLI
).toBe(false); ).toBe(false);
}); });
test("preserves a timed-out handoff and closes only a late bridge with matching run identity", async ({ test(
workspace, "preserves a timed-out handoff and closes only a late bridge with matching run identity",
}) => { { timeout: 60_000 },
async ({ workspace }) => {
const target = workspace.path("target"); const target = workspace.path("target");
await copyFile("/usr/bin/true", target); await copyFile("/usr/bin/true", target);
const mode = await workspace.write("mode", "handoff"); const mode = await workspace.write("mode", "handoff");
@@ -231,7 +248,9 @@ test("preserves a timed-out handoff and closes only a late bridge with matching
await mcp.connect(transport); await mcp.connect(transport);
const call = (name: string, arguments_: Record<string, string> = {}) => const call = (name: string, arguments_: Record<string, string> = {}) =>
mcp.callTool({ name, arguments: arguments_ }); mcp.callTool({ name, arguments: arguments_ });
expect((await call("open_binary", { path: target })).isError).not.toBe(true); expect((await call("open_binary", { path: target })).isError).not.toBe(
true,
);
const initial = parseMcpToolError(await call("current_document")); const initial = parseMcpToolError(await call("current_document"));
expect(initial.error).toMatchObject({ expect(initial.error).toMatchObject({
code: "cleanup_incomplete", code: "cleanup_incomplete",
@@ -273,7 +292,12 @@ test("preserves a timed-out handoff and closes only a late bridge with matching
const lateBridge = async (identity: string, credential = token) => { const lateBridge = async (identity: string, credential = token) => {
const child = spawn( const child = spawn(
process.execPath, process.execPath,
[resolve("tests/fixtures/fakeHopper.mjs"), socket, credential, identity], [
resolve("tests/fixtures/fakeHopper.mjs"),
socket,
credential,
identity,
],
{ stdio: "ignore" }, { stdio: "ignore" },
); );
children.push(child); children.push(child);
@@ -293,12 +317,17 @@ test("preserves a timed-out handoff and closes only a late bridge with matching
await stop(wrong); await stop(wrong);
await rm(socket, { force: true }); await rm(socket, { force: true });
} }
const closing = call("close_binary");
await delay(150);
const owned = await lateBridge(runId); const owned = await lateBridge(runId);
expect((await call("close_binary")).isError).not.toBe(true); expect((await closing).isError).not.toBe(true);
await vi.waitFor(() => expect(owned.exitCode).toBe(0)); await vi.waitFor(() => expect(owned.exitCode).toBe(0));
await expect(access(runtime)).rejects.toMatchObject({ code: "ENOENT" }); await expect(access(runtime)).rejects.toMatchObject({ code: "ENOENT" });
await writeFile(mode, "bridge"); await writeFile(mode, "bridge");
expect((await call("open_binary", { path: target })).isError).not.toBe(true); expect((await call("open_binary", { path: target })).isError).not.toBe(
true,
);
expect((await call("current_document")).isError).not.toBe(true); expect((await call("current_document")).isError).not.toBe(true);
expect((await call("close_binary")).isError).not.toBe(true); expect((await call("close_binary")).isError).not.toBe(true);
}); },
);
@@ -29,6 +29,7 @@ describe("Hopper launcher failure through the one-shot CLI", () => {
// Hopper target leases are acquired only for the macOS launcher route. // Hopper target leases are acquired only for the macOS launcher route.
workspaceCliTest.skipIf(process.platform !== "darwin")( workspaceCliTest.skipIf(process.platform !== "darwin")(
"exits after a retained-cleanup failure and releases the target to a retry", "exits after a retained-cleanup failure and releases the target to a retry",
{ timeout: 90_000 },
async ({ cli, workspace }) => { async ({ cli, workspace }) => {
const launcherPath = await workspace.write( const launcherPath = await workspace.write(
"failure-helper", "failure-helper",
@@ -58,7 +59,7 @@ describe("Hopper launcher failure through the one-shot CLI", () => {
HOPPER_LOADER_ARGS_JSON: JSON.stringify(loaderArgs), HOPPER_LOADER_ARGS_JSON: JSON.stringify(loaderArgs),
REA_LOG_LEVEL: "silent", REA_LOG_LEVEL: "silent",
}, },
timeoutMs: 30_000, timeoutMs: 45_000,
}); });
const retained = RetainedRuntime.safeParse(result.json); const retained = RetainedRuntime.safeParse(result.json);
if (retained.success) if (retained.success)
@@ -82,7 +82,7 @@ const clients: HopperClient[] = [];
afterEach(async () => { afterEach(async () => {
await Promise.all(clients.splice(0).map((client) => client.close())); await Promise.all(clients.splice(0).map((client) => client.close()));
}); }, 45_000);
describe("HopperClient cleanup", () => { describe("HopperClient cleanup", () => {
it("waits for an operation reply without a fixed request deadline", async () => { 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 { 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"; import { expect, it, onTestFinished, vi } from "vitest";
@@ -64,19 +66,85 @@ const setup = (
const launcher = new ExternalBridgeLauncher(identity, healthMode); const launcher = new ExternalBridgeLauncher(identity, healthMode);
const client = new HopperClient({ launcher, startupTimeoutMs: 2_000 }); const client = new HopperClient({ launcher, startupTimeoutMs: 2_000 });
onTestFinished(async () => { onTestFinished(async () => {
await client.close();
await Promise.all( await Promise.all(
[...launcher.helpers, ...launcher.fixture.processes].map(stop), [...launcher.helpers, ...launcher.fixture.processes].map(stop),
); );
await client.close();
await Promise.all( await Promise.all(
launcher.sessions.map(({ directory }) => launcher.sessions.map(({ directory }) =>
rm(directory, { recursive: true, force: true }), rm(directory, { recursive: true, force: true }),
), ),
); );
}); }, 45_000);
return { client, launcher }; 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)( it.each(["wrong-run", "wrong-token"] as const)(
"never requests document shutdown after the first handshake rejects %s", "never requests document shutdown after the first handshake rejects %s",
async (identity) => { async (identity) => {
@@ -55,7 +55,7 @@ const start = async (owned: boolean) => {
onTestFinished(async () => { onTestFinished(async () => {
await client.close(); await client.close();
for (const child of launcher.fixture.processes) child.kill(); for (const child of launcher.fixture.processes) child.kill();
}); }, 45_000);
expect((await client.start()).ok).toBe(true); expect((await client.start()).ok).toBe(true);
return { client, launcher }; 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 }); const client = new HopperClient({ launcher, startupTimeoutMs: 10_000 });
onTestFinished(async () => { onTestFinished(async () => {
await client.close(); await client.close();
}); }, 45_000);
const result = await client.start(); const result = await client.start();
expect(result).toMatchObject({ expect(result).toMatchObject({
ok: false, ok: false,
@@ -87,7 +87,7 @@ it.each(["exit", "signal", "exit75"] as const)(
const client = new HopperClient({ launcher, startupTimeoutMs: 10_000 }); const client = new HopperClient({ launcher, startupTimeoutMs: 10_000 });
onTestFinished(async () => { onTestFinished(async () => {
await client.close(); await client.close();
}); }, 45_000);
const result = await client.start(); const result = await client.start();
expect(result).toMatchObject({ expect(result).toMatchObject({
ok: false, ok: false,
@@ -139,7 +139,7 @@ it.each([0, 1])(
const client = new HopperClient({ launcher, startupTimeoutMs: 10_000 }); const client = new HopperClient({ launcher, startupTimeoutMs: 10_000 });
onTestFinished(async () => { onTestFinished(async () => {
await client.close(); await client.close();
}); }, 45_000);
const result = await client.start(); const result = await client.start();
expect(result).toMatchObject({ expect(result).toMatchObject({
ok: false, 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 }); const client = new HopperClient({ launcher, startupTimeoutMs: 1500 });
onTestFinished(async () => { onTestFinished(async () => {
await client.close(); await client.close();
}); }, 45_000);
const result = await client.start(); const result = await client.start();
if (result.ok) if (result.ok)
throw new Error("Expected missing bridge readiness to time out"); throw new Error("Expected missing bridge readiness to time out");
@@ -128,7 +128,7 @@ const clients: HopperClient[] = [];
afterEach(async () => { afterEach(async () => {
await Promise.all(clients.splice(0).map((client) => client.close())); await Promise.all(clients.splice(0).map((client) => client.close()));
}); }, 45_000);
describe("HopperClient startup deadline", () => { describe("HopperClient startup deadline", () => {
it("applies one deadline to launcher, socket, and health startup phases", async () => { it("applies one deadline to launcher, socket, and health startup phases", async () => {