fix(hopper): avoid blocking owned Linux shutdown

This commit is contained in:
morluto
2026-07-13 21:53:08 +00:00
parent e29652a2dd
commit d28336c6e5
4 changed files with 39 additions and 14 deletions
+8 -1
View File
@@ -634,10 +634,17 @@ def _dispatch(method, params):
return {"shutdown": True, "analysis_stopped": True, "document_closed": True}
if document.backgroundProcessActive():
document.requestBackgroundProcessStop()
if REA_OWNS_PROCESS_LIFETIME:
return {
"shutdown": True,
"analysis_stopped": not document.backgroundProcessActive(),
"document_closed": False,
"cleanup_required": True,
}
if document.backgroundProcessActive():
document.waitForBackgroundProcessToEnd()
analysis_stopped = not document.backgroundProcessActive()
document.closeDocument()
analysis_stopped = not document.backgroundProcessActive()
document_closed = _session_document() is None
return {
"shutdown": True,
+11 -5
View File
@@ -27,6 +27,8 @@ export interface BridgeSession {
export interface BridgeLaunch {
readonly process: ChildProcess;
readonly ownsProcessLifetime: boolean;
/** True when verified process cleanup completes bridge shutdown. */
readonly shutdownByCleanup?: boolean;
readonly cleanup?: () => Promise<ProcessCleanupResult>;
}
@@ -69,11 +71,13 @@ export class HopperApplicationLauncher implements BridgeLauncher {
options: { readonly signal?: AbortSignal } = {},
): Promise<Result<BridgeLaunch, HopperStartError | HopperCancelledError>> {
const bootstrapPath = `${session.directory}/bootstrap.py`;
const ownsProcessLifetime = usesLinuxDemo(this.options);
const source = [
`REA_SOCKET = ${JSON.stringify(session.socketPath)}`,
`REA_TOKEN = ${JSON.stringify(session.token)}`,
`REA_RUN_ID = ${JSON.stringify(session.runId)}`,
`REA_TARGET_PATH = ${JSON.stringify(this.options.targetPath)}`,
`REA_OWNS_PROCESS_LIFETIME = ${ownsProcessLifetime ? "True" : "False"}`,
`exec(compile(open(${JSON.stringify(this.options.bridgeScriptPath)}, 'rb').read(), ${JSON.stringify(this.options.bridgeScriptPath)}, 'exec'))`,
"",
].join("\n");
@@ -141,6 +145,7 @@ export class HopperApplicationLauncher implements BridgeLauncher {
return ok({
process: child,
ownsProcessLifetime: true,
shutdownByCleanup: ownsProcessLifetime,
cleanup: () =>
cleanupOwnedProcessGroup(
ownedProcessGroup(session, pid, ownershipCommand),
@@ -163,11 +168,7 @@ const linuxDemoLaunch = (
readonly ownershipCommand: string;
}
| undefined => {
if (
process.platform !== "linux" ||
basename(options.launcherPath) !== "Hopper" ||
options.demoHelperPath === undefined
)
if (!usesLinuxDemo(options) || options.demoHelperPath === undefined)
return undefined;
return {
command: "/usr/bin/python3",
@@ -185,6 +186,11 @@ const linuxDemoLaunch = (
};
};
const usesLinuxDemo = (options: HopperApplicationLauncherOptions): boolean =>
process.platform === "linux" &&
basename(options.launcherPath) === "Hopper" &&
options.demoHelperPath !== undefined;
const spawnLauncher = async (
command: string,
args: readonly string[],
+18 -8
View File
@@ -173,7 +173,13 @@ export class HopperClient {
{},
{ timeoutMs: SHUTDOWN_TIMEOUT_MS },
).catch(() => err(new HopperProcessError(null)));
if (!shutdown.ok || !isShutdownAcknowledgement(shutdown.value))
if (
!shutdown.ok ||
!isShutdownAcknowledgement(
shutdown.value,
this.#launch?.shutdownByCleanup === true,
)
)
this.#logger.warn(
{ status: shutdown.ok ? "invalid-acknowledgement" : "failed" },
"Hopper document shutdown was not confirmed",
@@ -412,13 +418,17 @@ 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 isShutdownAcknowledgement = (
value: JsonValue,
ownsProcessLifetime: boolean,
): boolean => {
if (typeof value !== "object" || value === null || Array.isArray(value))
return false;
if (value.shutdown !== true) return false;
if (value.analysis_stopped === true && value.document_closed === true)
return true;
return ownsProcessLifetime && value.cleanup_required === true;
};
const parseServerInfo = (
value: JsonValue,
+2
View File
@@ -104,6 +104,8 @@ describe("Hopper bridge truthfulness", () => {
expect(bridgeSource).toContain("document.getDatabaseFilePath()");
expect(bridgeSource).toContain("document.backgroundProcessActive()");
expect(bridgeSource).toContain("document.requestBackgroundProcessStop()");
expect(bridgeSource).toContain("if REA_OWNS_PROCESS_LIFETIME:");
expect(bridgeSource).toContain('"cleanup_required": True');
expect(bridgeSource).toContain("document.waitForBackgroundProcessToEnd()");
expect(bridgeSource).toContain("document.closeDocument()");
expect(bridgeSource).toContain(