Skip to content

Commit 03df353

Browse files
committed
fix: suppress discord reconnect exhaustion during shutdown
1 parent d90a08a commit 03df353

3 files changed

Lines changed: 64 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Docs: https://docs.openclaw.ai
5757
- Hooks/doctor: warn when `hooks.transformsDir` points outside the canonical hooks transform directory, so invalid workspace skill paths get a direct recovery hint before the Gateway crash-loops. Fixes #75853. Thanks @midobk.
5858
- Proxy/audio: convert standard `FormData` bodies before proxy-backed undici fetches, so audio transcription and multipart uploads no longer send `[object FormData]` when `HTTP_PROXY` or `HTTPS_PROXY` is configured. Fixes #48554. Thanks @dco5.
5959
- Discord: allow explicitly configured ack reactions in tool-only guild channels while keeping automatic lifecycle/status reactions suppressed. Fixes #74922. Thanks @samvilian and @BlueBirdBack.
60+
- Discord: treat abort-time Carbon reconnect-exhausted events as expected shutdown during stale-socket restarts, so health-monitor restarts no longer reject the monitor lifecycle. Carries forward #58216; supersedes #73949. Thanks @Perttulands.
6061
- Discord/native commands: return an explicit warning when slash command dispatch or direct plugin execution produces no visible reply instead of a success-style completion ack. Fixes #58986; supersedes #62057. Thanks @jb510.
6162
- Discord: keep typing indicators alive during long tool runs and auto-compaction while keepalive ticks continue, so active sessions do not appear stalled before the final reply. Thanks @Squirbie.
6263
- Discord: preserve multipart Content-Type headers for attachment uploads across REST fetch paths, so generated images and other media no longer fail delivery with `CONTENT_TYPE_INVALID`. Thanks @FunJim.

‎extensions/discord/src/monitor/provider.lifecycle.test.ts‎

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,13 @@ describe("runDiscordGatewayLifecycle", () => {
175175
threadStop: ReturnType<typeof vi.fn>;
176176
waitCalls: number;
177177
gatewaySupervisor: { detachLifecycle: ReturnType<typeof vi.fn> };
178+
detachCalls?: number;
178179
}) {
179180
expect(waitForDiscordGatewayStopMock).toHaveBeenCalledTimes(params.waitCalls);
180181
expect(unregisterGatewayMock).toHaveBeenCalledWith("default");
181182
expect(stopGatewayLoggingMock).toHaveBeenCalledTimes(1);
182183
expect(params.threadStop).toHaveBeenCalledTimes(1);
183-
expect(params.gatewaySupervisor.detachLifecycle).toHaveBeenCalledTimes(1);
184+
expect(params.gatewaySupervisor.detachLifecycle).toHaveBeenCalledTimes(params.detachCalls ?? 1);
184185
}
185186

186187
it("resolves gateway READY timeouts from config, env, then defaults", () => {
@@ -509,6 +510,60 @@ describe("runDiscordGatewayLifecycle", () => {
509510
});
510511
});
511512

513+
it("treats abort-time live reconnect exhaustion as expected shutdown", async () => {
514+
const abortController = new AbortController();
515+
let liveGatewayHandler: ((event: DiscordGatewayEvent) => void) | undefined;
516+
const { lifecycleParams, threadStop, runtimeLog, runtimeError, gatewaySupervisor } =
517+
createLifecycleHarness();
518+
lifecycleParams.abortSignal = abortController.signal;
519+
gatewaySupervisor.attachLifecycle.mockImplementation(
520+
(handler: (event: DiscordGatewayEvent) => void) => {
521+
liveGatewayHandler = handler;
522+
},
523+
);
524+
abortController.signal.addEventListener(
525+
"abort",
526+
() => {
527+
if (!liveGatewayHandler) {
528+
throw new Error("discord gateway lifecycle handler was not attached");
529+
}
530+
liveGatewayHandler(
531+
createGatewayEvent(
532+
"reconnect-exhausted",
533+
"Max reconnect attempts (50) reached after close code 1005",
534+
),
535+
);
536+
},
537+
{ once: true },
538+
);
539+
waitForDiscordGatewayStopMock.mockImplementationOnce(async (waitParams) => {
540+
const actual =
541+
await vi.importActual<typeof import("../monitor.gateway.js")>("../monitor.gateway.js");
542+
const waitPromise = actual.waitForDiscordGatewayStop(waitParams);
543+
abortController.abort(new Error("shutdown"));
544+
return await waitPromise;
545+
});
546+
547+
await expect(runDiscordGatewayLifecycle(lifecycleParams)).resolves.toBeUndefined();
548+
549+
expect(gatewaySupervisor.attachLifecycle).toHaveBeenCalledTimes(1);
550+
expect(runtimeLog).toHaveBeenCalledWith(
551+
expect.stringContaining("treating reconnect-exhausted during expected shutdown as clean"),
552+
);
553+
expect(runtimeLog).toHaveBeenCalledWith(
554+
expect.stringContaining("Max reconnect attempts (50) reached after close code 1005"),
555+
);
556+
expect(runtimeError).not.toHaveBeenCalledWith(
557+
expect.stringContaining("discord gateway reconnect-exhausted"),
558+
);
559+
expectLifecycleCleanup({
560+
threadStop,
561+
waitCalls: 1,
562+
gatewaySupervisor,
563+
detachCalls: 2,
564+
});
565+
});
566+
512567
it("surfaces fatal startup gateway errors while waiting for READY", async () => {
513568
vi.useFakeTimers();
514569
try {

‎extensions/discord/src/monitor/provider.lifecycle.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,13 @@ export async function runDiscordGatewayLifecycle(params: {
457457

458458
let sawDisallowedIntents = false;
459459
const handleGatewayEvent = (event: DiscordGatewayEvent): "continue" | "stop" => {
460+
if (params.abortSignal?.aborted && event.type === "reconnect-exhausted") {
461+
lifecycleStopping = true;
462+
params.runtime.log?.(
463+
`discord: treating reconnect-exhausted during expected shutdown as clean: ${event.message}`,
464+
);
465+
return "continue";
466+
}
460467
if (event.type === "disallowed-intents") {
461468
lifecycleStopping = true;
462469
sawDisallowedIntents = true;

0 commit comments

Comments
 (0)