From bc1edeba339dbfcb7932f2f485a5388b3181efd2 Mon Sep 17 00:00:00 2001 From: Gustavo Madeira Santana Date: Sat, 14 Feb 2026 17:14:53 -0500 Subject: [PATCH] fix(cli): harden message gateway_stop teardown --- src/cli/program/message/helpers.test.ts | 14 ++++++++++++++ src/cli/program/message/helpers.ts | 6 +++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/cli/program/message/helpers.test.ts b/src/cli/program/message/helpers.test.ts index 82f16dc3805..e566605d0d1 100644 --- a/src/cli/program/message/helpers.test.ts +++ b/src/cli/program/message/helpers.test.ts @@ -107,6 +107,20 @@ describe("runMessageAction", () => { expect(exitMock).toHaveBeenCalledWith(1); }); + it("logs gateway_stop failure and still exits with success code", async () => { + hasHooksMock.mockReturnValueOnce(true); + runGatewayStopMock.mockRejectedValueOnce(new Error("hook failed")); + const fakeCommand = { help: vi.fn() } as never; + const { runMessageAction } = createMessageCliHelpers(fakeCommand, "discord"); + + await expect( + runMessageAction("send", { channel: "discord", target: "123", message: "hi" }), + ).rejects.toThrow("exit"); + + expect(errorMock).toHaveBeenCalledWith("gateway_stop hook failed: Error: hook failed"); + expect(exitMock).toHaveBeenCalledWith(0); + }); + it("does not call exit(0) when the action throws", async () => { messageCommandMock.mockRejectedValueOnce(new Error("boom")); const fakeCommand = { help: vi.fn() } as never; diff --git a/src/cli/program/message/helpers.ts b/src/cli/program/message/helpers.ts index 4ecf265aa49..83c281d912e 100644 --- a/src/cli/program/message/helpers.ts +++ b/src/cli/program/message/helpers.ts @@ -28,7 +28,11 @@ async function runPluginStopHooks(): Promise { if (!hookRunner?.hasHooks("gateway_stop")) { return; } - await hookRunner.runGatewayStop({ reason: "cli message action complete" }, {}); + try { + await hookRunner.runGatewayStop({ reason: "cli message action complete" }, {}); + } catch (err) { + defaultRuntime.error(danger(`gateway_stop hook failed: ${String(err)}`)); + } } export function createMessageCliHelpers(