Skip to content

refactor(socket_mode): stop reviving a terminated session state - #2106

Open
WilliamBergamin wants to merge 2 commits into
mainfrom
refactor/socket-mode-drop-terminated-reset
Open

WilliamBergamin wants to merge 2 commits into
mainfrom
refactor/socket-mode-drop-terminated-reset

Conversation

@WilliamBergamin

@WilliamBergamin WilliamBergamin commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This pull request removes the current_session_state.terminated = False reset from _run_current_session in the built-in Socket Mode client and RTM v2.

Testing

./scripts/run_tests.sh tests/slack_sdk/socket_mode tests/slack_sdk/rtm_v2

The new test_run_current_session_survives_a_reconnect_before_the_state_read in each client fails without the fix.

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

The runner reset current_session_state.terminated to False before each
run, which could only revive a state that connect() or close() had
already terminated. Drop it in the builtin Socket Mode client and RTM v2
so the flag keeps its meaning.

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 enhancement M-T: A feature request for new functionality semver:patch rtm-client socket-mode labels Dec 2, 2026
@WilliamBergamin WilliamBergamin self-assigned this Dec 2, 2026

@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.

@WilliamBergamin I think the reset still covers one reconnect race, so I'd like to settle that before this lands. Details inline, and the same applies to RTM v2.

Comment thread slack_sdk/socket_mode/builtin/client.py Outdated
try:
self.logger.info(f"Starting to receive messages from a new connection (session id: {session_id})")
self.current_session_state.terminated = False
self.current_session.run_until_completion(self.current_session_state)

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 connect() swaps the pair between the self.current_session and self.current_session_state reads on this line, the closed old session exits and its final state.terminated = True marks the new state, so every later tick skips the live session until the stale-ping check reconnects about 20s later. Without the reset nothing revives it, so could we read current_session_state into a local before current_session and pass both locals in?

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.

Fixed in commit 16fd300, and the regression test on both clients is a great touch. Thanks! 🙇

@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 85.00%. Comparing base (6703d74) to head (16fd300).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2106      +/-   ##
==========================================
+ Coverage   84.96%   85.00%   +0.04%     
==========================================
  Files         118      118              
  Lines       14287    14289       +2     
==========================================
+ Hits        12139    12147       +8     
+ Misses       2148     2142       -6     

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

Without the terminated reset, a connect() landing between the runner's
read of current_session and its read of current_session_state ran the
retired session's loop against the new state. That loop exits on its
closed socket and terminates the state it was given, so the live session
went idle until the stale-ping check reconnected.

The runner now reads the state, then the session, and checks and runs
only those locals. connect() publishes the session before the state, so
the pair is either consistent or an already terminated old state, which
returns at once. A test per client simulates a reconnect at the state
read.

Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>

@zimeg zimeg 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.

🐍 @WilliamBergamin LGTM! Reading the state before its session closes the reconnect race Michael found, in both clients, and the new tests catch the old order.

@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.

✅ Thanks @WilliamBergamin, reading the state before its session closes the race and the new tests pin it down nicely.

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

enhancement M-T: A feature request for new functionality rtm-client semver:patch socket-mode

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants