fix(slack): close previous handler in connect() to prevent zombie Socket Mode connections#18982
Closed
nftpoetrist wants to merge 1 commit into
Closed
Conversation
…ket Mode connections SlackAdapter.connect() overwrote self._handler, self._app, and self._socket_mode_task without closing the prior AsyncSocketModeHandler first. If connect() was called a second time on the same adapter (e.g. during a gateway restart or in-process reconnect attempt), the old Socket Mode websocket stayed alive. Both the old and new connections received every Slack event and dispatched it twice — producing double responses with different wording, the same bug that affected DiscordAdapter (NousResearch#18187, fixed in NousResearch#18758). Fix: add a close-before-reassign guard at the start of the connection setup path, mirroring the guard DiscordAdapter.connect() already has. When self._handler is None (fresh adapter, first connect()) the block is a harmless no-op. Scoped to the handler/app fields only — no behavior change for any path that does not call connect() twice. Fixes NousResearch#18980
Collaborator
|
Merged via PR #19181. Your commit was cherry-picked onto current main with authorship preserved. Clean parity fix with the Discord equivalent — thanks @nftpoetrist! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
SlackAdapter.connect()overwroteself._handler,self._app, andself._socket_mode_taskwithout closing the priorAsyncSocketModeHandlerfirst. Ifconnect()was called a second time on the same adapter (e.g. during a gateway restart or in-process reconnect attempt), the old Socket Mode websocket stayed alive. Both the old and new connections received every Slack event and dispatched it twice — producing double responses with different wording.This is the same bug that affected
DiscordAdapter(#18187, fixed in #18758 merged today). Parity fix: adds the same close-before-reassign guard thatDiscordAdapter.connect()already has.One-line guard in
connect()—if self._handler is not None: await self._handler.close_async()followed byfinally: self._handler = None; self._app = None. When_handlerisNone(fresh adapter, firstconnect()) the block is a harmless no-op. Scoped to the handler/app fields only — no behavior change for any path that does not callconnect()twice.Related Issue
Fixes #18980
Type of Change
Changes Made
gateway/platforms/slack.py: add close-before-reassign guard inconnect()beforeself._app = AsyncApp(...)(+10 lines)tests/gateway/test_slack.py: addtest_reconnect_closes_previous_handler_to_prevent_zombie_sockettoTestSlackConnectCleanup(+54 lines)How to Test
python3.11 -m pytest tests/gateway/test_slack.py::TestSlackConnectCleanup -v --override-ini="addopts="Checklist
Code
Documentation & Housekeeping