fix: stop aiohttp Socket Mode connect() retrying forever after close - #1956
Merged
WilliamBergamin merged 2 commits intoSep 3, 2026
Merged
Conversation
…1913) SocketModeClient.connect() used `while True` and its retry handler never checked self.closed, so once close() closed the aiohttp ClientSession the loop spun forever on `RuntimeError: Session is closed` (observed as 270k+ errors over 46 days in production). Make connect() respect self.closed like its sibling loops (monitor_current_session, receive_messages) already do: guard the loop with `while not self.closed` and return from the retry handler once shutdown has begun. Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1956 +/- ##
==========================================
+ Coverage 84.06% 84.08% +0.01%
==========================================
Files 118 118
Lines 13505 13509 +4
==========================================
+ Hits 11353 11359 +6
+ Misses 2152 2150 -2 ☔ View full report in Codecov by Harness. |
WilliamBergamin
marked this pull request as ready for review
September 2, 2026 17:31
Regression coverage for #1913: assert the aiohttp Socket Mode connect() retry handler returns without logging or retrying when an in-flight attempt raises after close(). The existing test only exercised the `while not self.closed` loop condition, not the except-block guard. Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
5 tasks
zimeg
approved these changes
Sep 2, 2026
zimeg
left a comment
Member
There was a problem hiding this comment.
@WilliamBergamin LGTM! I have one comment on logging but it's a ramble.
Comment on lines
+409
to
+410
| if self.logger.level <= logging.DEBUG: | ||
| self.logger.debug(f"Stopped connecting because the client is closed (error: {e})") |
Member
There was a problem hiding this comment.
🪬 note: I'm surprised self.logger.debug doesn't check this condition itself but I think this log is quite useful.
Contributor
Author
There was a problem hiding this comment.
This logic follows the existing pattern in the file, but I'm not sure why we have this condition, I need to take a deeper dive into it
WilliamBergamin
deleted the
fix/aiohttp-socket-mode-connect-stops-when-closed
branch
September 3, 2026 14:03
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.
Summary
The aiohttp Socket Mode
SocketModeClient.connect()looped withwhile True:, and its retryhandler never checked
self.closed. If a connect/reconnect attempt was in flight when the clientwas closed, every retry's
ws_connect()raisedRuntimeError: Session is closed, so the loop kept retrying at theping_intervalcadence forever.The fix aligns
connect()with thewhile not self.closed:idiom already used by its sibling loops (monitor_current_session,receive_messages, and the baseprocess_messages), and returns from the retry handler onceself.closedis set. A background task orphaned by shutdown now exits within one iteration instead of spinning forever; normal startup/reconnect is unchanged.Closes #1913.
Testing
To watch it fail against the pre-fix code, restore the old
connect()(keeping the new test), thenput the fix back:
(Equivalent manual repro: change
while not self.closed:back towhile True:and delete theif self.closed: returnguard inconnect()'sexceptblock.)Category
/docs(Documents)/tutorial(PythOnBoardingBot tutorial)tests/integration_tests(Automated tests for this library)Requirements
python3 -m venv .venv && source .venv/bin/activate && ./scripts/run_validation.shafter making the changes.