diff --git a/slack_sdk/socket_mode/builtin/client.py b/slack_sdk/socket_mode/builtin/client.py index 6701cf6f0..84da546cf 100644 --- a/slack_sdk/socket_mode/builtin/client.py +++ b/slack_sdk/socket_mode/builtin/client.py @@ -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: + self._retire_current_session() + return if not self.current_app_monitor_started: self.current_app_monitor_started = True @@ -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(): @@ -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)})") diff --git a/tests/slack_sdk/socket_mode/test_builtin.py b/tests/slack_sdk/socket_mode/test_builtin.py index 61b1193e5..cb620bd3d 100644 --- a/tests/slack_sdk/socket_mode/test_builtin.py +++ b/tests/slack_sdk/socket_mode/test_builtin.py @@ -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 @@ -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",