Skip to content

fix(socket_mode): retire a builtin session that connects after close - #2105

Open
WilliamBergamin wants to merge 1 commit into
mainfrom
fix/socket-mode-close-during-reconnect
Open

WilliamBergamin wants to merge 1 commit into
mainfrom
fix/socket-mode-close-during-reconnect

Conversation

@WilliamBergamin

Copy link
Copy Markdown
Contributor

Summary

This pull request makes the built-in SocketModeClient.connect() retire the session it just opened when close() already ran, so closing the client during a reconnect no longer leaves a live session behind.

  • Cause: a reconnect already inside connect() (on the monitor, the runner's _on_close, or a disconnect message) published its new session after close() retired the old one, turned auto reconnect back on, and started the monitor. That leaked an open socket nobody reads, or left a runner for close() to wait on.
  • Fix: connect() checks closed after publishing and retires the session through _retire_current_session(), the same step close() now uses. close() sets closed before it retires, so whichever runs second cleans up. No lock in close(), so it can't deadlock with _on_close waiting on connect_operation_lock while close() joins the runner.
  • Follows fix(socket_mode): end the builtin receive loop when its socket closes #2104.

Testing

test_close_during_reconnect_leaves_no_live_session holds a reconnect in a slow issue_new_wss_url while close() runs, then asserts no live session and no monitor remain. It fails on main and passes here:

./scripts/run_tests.sh tests/slack_sdk/socket_mode/test_builtin.py

Category

  • slack_sdk.web.WebClient (sync/async) (Web API client)
  • slack_sdk.webhook.WebhookClient (sync/async) (Incoming Webhook, response_url sender)
  • slack_sdk.socket_mode (Socket Mode client)
  • slack_sdk.signature (Request Signature Verifier)
  • slack_sdk.oauth (OAuth Flow Utilities)
  • slack_sdk.models (UI component builders)
  • slack_sdk.scim (SCIM API client)
  • slack_sdk.audit_logs (Audit Logs API client)
  • slack_sdk.rtm_v2 (RTM client)
  • /docs (Documents)
  • /tutorial (PythOnBoardingBot tutorial)
  • tests/integration_tests (Automated tests for this library)

Requirements

  • I've read and understood the Contributing Guidelines and have done my best effort to follow them.
  • I've read and agree to the Code of Conduct.
  • I have signed the Salesforce CLA (required before your PR can be reviewed or merged).
  • I've run python3 -m venv .venv && source .venv/bin/activate && ./scripts/run_validation.sh after making the changes.

🤖 Generated with Claude Code

A reconnect already inside connect() when close() runs published a new
live session after close() retired the old one, re-enabled auto
reconnect, and started the monitor. connect() now retires that session
when the client is closed, through the same step close() uses.

Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
@WilliamBergamin
WilliamBergamin requested a review from a team as a code owner December 2, 2026 19:18
@WilliamBergamin WilliamBergamin added this to the 3.48.1 milestone Dec 2, 2026
@WilliamBergamin WilliamBergamin added bug M-T: A confirmed bug report. Issues are confirmed when the reproduction steps are documented semver:patch area:concurrency Issues and PRs related to concurrency socket-mode labels Dec 2, 2026
@WilliamBergamin WilliamBergamin self-assigned this Dec 2, 2026
@codecov

codecov Bot commented Dec 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.97%. Comparing base (6703d74) to head (eb5c158).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2105   +/-   ##
=======================================
  Coverage   84.96%   84.97%           
=======================================
  Files         118      118           
  Lines       14287    14292    +5     
=======================================
+ Hits        12139    12144    +5     
  Misses       2148     2148           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@mwbrooks mwbrooks left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice small fix, though there is still one ordering where the monitor starts after close(), which is the leak the new test checks for. Happy to approve once that is covered.

self.current_session = current_session
self.current_session_state = ConnectionState()
self.auto_reconnect_enabled = self.default_auto_reconnect_enabled
if self.closed:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (blocking): If close() runs after this check but before current_app_monitor.start() below, it sees the monitor as not alive and skips its shutdown, then this thread starts it on a closed client. Could we check self.closed again after starting the monitor, or do the check and the start under a lock close() also takes?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:concurrency Issues and PRs related to concurrency bug M-T: A confirmed bug report. Issues are confirmed when the reproduction steps are documented semver:patch socket-mode

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants