Repository navigation
refactor(socket_mode): stop reviving a terminated session state - #2106
WilliamBergamin wants to merge 2 commits into
Conversation
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>
mwbrooks
left a comment
There was a problem hiding this comment.
@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.
| 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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Fixed in commit 16fd300, and the regression test on both clients is a great touch. Thanks! 🙇
Codecov Report✅ All modified and coverable lines are covered by tests. 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. |
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
left a comment
There was a problem hiding this comment.
🐍 @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
left a comment
There was a problem hiding this comment.
✅ Thanks @WilliamBergamin, reading the state before its session closes the race and the new tests pin it down nicely.
Summary
This pull request removes the
current_session_state.terminated = Falsereset from_run_current_sessionin the built-in Socket Mode client and RTM v2.connect()orclose()had already terminated; since fix(socket_mode): end the builtin receive loop when its socket closes #2104 the receive loop exits once its socket closes.Testing
The new
test_run_current_session_survives_a_reconnect_before_the_state_readin each client fails without the fix.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.🤖 Generated with Claude Code