diff --git a/src/cli/daemon-cli/lifecycle.test.ts b/src/cli/daemon-cli/lifecycle.test.ts index 53ded2b5790..20ee6a69a76 100644 --- a/src/cli/daemon-cli/lifecycle.test.ts +++ b/src/cli/daemon-cli/lifecycle.test.ts @@ -326,6 +326,14 @@ describe("runDaemonRestart health checks", () => { expect(signalVerifiedGatewayPidSync).toHaveBeenCalledWith(4300, "SIGTERM"); }); + it("skips gateway port resolution on stop when the service manager handles the stop", async () => { + await runDaemonStop({ json: true }); + + expect(service.readCommand).not.toHaveBeenCalled(); + expect(loadConfig).not.toHaveBeenCalled(); + expect(resolveGatewayPort).not.toHaveBeenCalled(); + }); + it("signals a single unmanaged gateway process on restart", async () => { findVerifiedGatewayListenerPidsOnPortSync.mockReturnValue([4200]); mockUnmanagedRestart({ runPostRestartCheck: true }); diff --git a/src/cli/daemon-cli/lifecycle.ts b/src/cli/daemon-cli/lifecycle.ts index 540d38c2a55..306c1d73ce1 100644 --- a/src/cli/daemon-cli/lifecycle.ts +++ b/src/cli/daemon-cli/lifecycle.ts @@ -157,14 +157,17 @@ export async function runDaemonStart(opts: DaemonLifecycleOptions = {}) { export async function runDaemonStop(opts: DaemonLifecycleOptions = {}) { const service = resolveGatewayService(); - const gatewayPort = await resolveGatewayLifecyclePort(service).catch(() => - resolveGatewayPortFallback(), - ); + let gatewayPortPromise: Promise | undefined; return await runServiceStop({ serviceNoun: "Gateway", service, opts, - onNotLoaded: async () => stopGatewayWithoutServiceManager(gatewayPort), + onNotLoaded: async () => { + gatewayPortPromise ??= resolveGatewayLifecyclePort(service).catch(() => + resolveGatewayPortFallback(), + ); + return await stopGatewayWithoutServiceManager(await gatewayPortPromise); + }, }); }