Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions slack_sdk/socket_mode/builtin/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ def connect(self) -> None:
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?

self._retire_current_session()
return

if not self.current_app_monitor_started:
self.current_app_monitor_started = True
Expand Down Expand Up @@ -223,9 +226,7 @@ def send_message(self, message: str) -> None:

def close(self) -> None:
self.closed = True
self.auto_reconnect_enabled = False
self.current_session_state.terminated = True
self.disconnect()
self._retire_current_session()
if self.current_session_runner.is_alive():
self.current_session_runner.shutdown()
if self.current_app_monitor.is_alive():
Expand All @@ -234,6 +235,11 @@ def close(self) -> None:
self.message_processor.shutdown()
self.message_workers.shutdown()

def _retire_current_session(self) -> None:
self.auto_reconnect_enabled = False
self.current_session_state.terminated = True
self.disconnect()

def _on_message(self, message: str):
if self.logger.isEnabledFor(logging.DEBUG):
self.logger.debug(f"on_message invoked: (message: {debug_redacted_message_string(message)})")
Expand Down
33 changes: 32 additions & 1 deletion tests/slack_sdk/socket_mode/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import time
import unittest
from unittest.mock import MagicMock, patch, sentinel
from threading import Thread
from threading import Event, Thread

from slack_sdk import WebClient
from slack_sdk.socket_mode import SocketModeClient
Expand Down Expand Up @@ -87,6 +87,37 @@ def test_connect_to_new_endpoint(self):
client.connect_to_new_endpoint()
self.assertFalse(client.is_connected())

def test_close_during_reconnect_leaves_no_live_session(self):
client = SocketModeClient(app_token="xapp-A111-222-xyz", web_client=self.web_client)
reconnecting, release = Event(), Event()

def slow_issue_new_wss_url():
reconnecting.set()
release.wait()
return "wss://example.com/"

def fake_connect(connection):
connection.sock = MagicMock()
connection.sock.recv.side_effect = socket.timeout

reconnect = Thread(target=client.connect_to_new_endpoint, daemon=True)
try:
with patch.object(client, "issue_new_wss_url", side_effect=slow_issue_new_wss_url):
with patch.object(Connection, "connect", fake_connect):
reconnect.start()
self.assertTrue(reconnecting.wait(timeout=2))
client.close()
release.set()
reconnect.join(timeout=2)

self.assertFalse(reconnect.is_alive())
self.assertFalse(client.is_connected())
self.assertFalse(client.current_app_monitor.is_alive())
finally:
release.set()
client.disconnect()
client.current_app_monitor.shutdown()

def test_enqueue_message(self):
client = SocketModeClient(
app_token="xapp-A111-222-xyz",
Expand Down
Loading