fix(hopper): cancel analysis and close documents reliably

This commit is contained in:
morluto
2026-07-14 04:18:26 +08:00
parent fb0da031cf
commit 179870e825
5 changed files with 70 additions and 5 deletions
+19 -3
View File
@@ -19,6 +19,7 @@ import { silentLogger, type Logger } from "../logger.js";
import { parseResponseLine, responseResult } from "./protocol.js";
const MAX_LINE_BYTES = 10 * 1024 * 1024;
const SHUTDOWN_TIMEOUT_MS = 30_000;
const SESSION_ROOT = process.platform === "darwin" ? "/tmp" : tmpdir();
/** Dependencies, deadlines, and redacted diagnostics for one bridge client. */
@@ -164,9 +165,16 @@ export class HopperClient {
try {
const socket = this.#socket;
if (socket !== undefined && !socket.destroyed) {
await this.#request("shutdown", {}, { timeoutMs: 500 }).catch(() =>
err(new HopperProcessError(null)),
);
const shutdown = await this.#request(
"shutdown",
{},
{ timeoutMs: SHUTDOWN_TIMEOUT_MS },
).catch(() => err(new HopperProcessError(null)));
if (!shutdown.ok || !isShutdownAcknowledgement(shutdown.value))
this.#logger.warn(
{ status: shutdown.ok ? "invalid-acknowledgement" : "failed" },
"Hopper document shutdown was not confirmed",
);
}
this.#failAll(new HopperProcessError(null));
socket?.destroy();
@@ -395,6 +403,14 @@ const connectOnce = async (
});
});
const isShutdownAcknowledgement = (value: JsonValue): boolean =>
typeof value === "object" &&
value !== null &&
!Array.isArray(value) &&
value.shutdown === true &&
value.analysis_stopped === true &&
value.document_closed === true;
const parseServerInfo = (
value: JsonValue,
expectedRunId: string,