Skip to content

Commit 01cba5b

Browse files
hello-ashleyintechclaudeClaude
committed
fix(socket_mode): seed last_ping_pong_time on first ping in builtin client
The stale-connection check in `check_state()` was gated on `last_ping_pong_time is not None`, which is only set when a pong frame arrives. If the server never answered the first ping, the value stayed `None` forever and the client never disconnected. Fix by seeding `last_ping_pong_time` with the current time on the first `check_state()` ping, matching the existing behavior in the aiohttp client. Fixes #1997 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
1 parent ab4e498 commit 01cba5b

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

‎slack_sdk/socket_mode/builtin/connection.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,11 @@ def check_state(self) -> None:
249249
try:
250250
if self.sock is not None:
251251
try:
252-
self.ping(f"{self.session_id}:{time.time()}")
252+
ping_time = time.time()
253+
self.ping(f"{self.session_id}:{ping_time}")
254+
# Seed on first ping so the stale check fires even if no pong ever arrives.
255+
if self.last_ping_pong_time is None:
256+
self.last_ping_pong_time = ping_time
253257
except ssl.SSLZeroReturnError as e:
254258
self.logger.info(
255259
"Unable to send a ping message. Closing the connection..."

‎tests/slack_sdk/socket_mode/test_builtin.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,25 @@ def run():
126126

127127
self.assertIsNotNone(conn)
128128

129+
def test_check_state_seeds_last_ping_pong_time_on_first_ping(self):
130+
conn = Connection(url="wss://example.com", logger=self.logger)
131+
conn.sock = patch("socket.socket").start() # non-None socket
132+
self.assertIsNone(conn.last_ping_pong_time)
133+
with patch.object(conn, "ping"):
134+
conn.check_state()
135+
self.assertIsNotNone(conn.last_ping_pong_time)
136+
patch.stopall()
137+
138+
def test_check_state_does_not_overwrite_existing_last_ping_pong_time(self):
139+
conn = Connection(url="wss://example.com", logger=self.logger)
140+
conn.sock = patch("socket.socket").start()
141+
existing_time = time.time() - 1
142+
conn.last_ping_pong_time = existing_time
143+
with patch.object(conn, "ping"):
144+
conn.check_state()
145+
self.assertEqual(conn.last_ping_pong_time, existing_time)
146+
patch.stopall()
147+
129148
# ----------------------------------
130149
# FrameHeader
131150

0 commit comments

Comments
 (0)