From 292d2fb42fe304e4d6e6184f39e1f60e5aa771f8 Mon Sep 17 00:00:00 2001 From: luyao618 <364939526@qq.com> Date: Fri, 1 May 2026 11:40:23 +0800 Subject: [PATCH] fix(discord): close old client before reconnect to prevent zombie websockets (#18187) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When DiscordAdapter.connect() is called during reconnect, it creates a new commands.Bot client without closing the previous one. The old client's websocket remains connected to Discord's gateway, causing both to fire on_message for every incoming event — resulting in double responses. Fix: before creating a new Bot instance, check if a previous client exists and close it. This ensures only one websocket connection is active at any time. Closes #18187 --- gateway/platforms/discord.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/gateway/platforms/discord.py b/gateway/platforms/discord.py index 1dd608d6f3..369a607a90 100644 --- a/gateway/platforms/discord.py +++ b/gateway/platforms/discord.py @@ -613,6 +613,21 @@ class DiscordAdapter(BasePlatformAdapter): # so LLM output or echoed user content can't ping the whole # server; override per DISCORD_ALLOW_MENTION_* env vars or the # discord.allow_mentions.* block in config.yaml. + + # Close any existing client to prevent zombie websocket connections + # on reconnect (see #18187). Without this, the old client remains + # connected to Discord gateway and both fire on_message, causing + # double responses. + if self._client is not None: + try: + if not self._client.is_closed(): + await self._client.close() + except Exception: + logger.debug("[%s] Failed to close previous Discord client", self.name) + finally: + self._client = None + self._ready_event.clear() + self._client = commands.Bot( command_prefix="!", # Not really used, we handle raw messages intents=intents,