diff --git a/bridge/hopper_bridge.py b/bridge/hopper_bridge.py index 18548fac1..8a19e463a 100644 --- a/bridge/hopper_bridge.py +++ b/bridge/hopper_bridge.py @@ -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, diff --git a/src/hopper/BridgeLauncher.ts b/src/hopper/BridgeLauncher.ts index f40a48758..9fc10c6e3 100644 --- a/src/hopper/BridgeLauncher.ts +++ b/src/hopper/BridgeLauncher.ts @@ -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; } @@ -69,11 +71,13 @@ export class HopperApplicationLauncher implements BridgeLauncher { options: { readonly signal?: AbortSignal } = {}, ): Promise> { 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[], diff --git a/src/hopper/HopperClient.ts b/src/hopper/HopperClient.ts index 551e6fd39..0421dde74 100644 --- a/src/hopper/HopperClient.ts +++ b/src/hopper/HopperClient.ts @@ -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, diff --git a/tests/bridgeTruthfulness.test.ts b/tests/bridgeTruthfulness.test.ts index 3b16d0c27..e42f3ad2d 100644 --- a/tests/bridgeTruthfulness.test.ts +++ b/tests/bridgeTruthfulness.test.ts @@ -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(