python-slack-sdk
python-slack-sdk copied to clipboard
SocketModeClient does not reconnect after a DNS outage
Reproducible in:
slack_sdk==3.26.2
Python 3.9.18
ProductName: macOS
ProductVersion: 13.6.4
BuildVersion: 22G513
Darwin Kernel Version 22.6.0: Sun Dec 17 22:13:25 PST 2023; root:xnu-8796.141.3.703.2~2/RELEASE_ARM64_T6020
Steps to reproduce:
- Have a
SocketModeClientwith these settings:
import os
from slack_sdk.socket_mode.websocket_client import SocketModeClient
from slack_sdk.web import WebClient
web_client = WebClient(token=os.environ["SLACK_WEB_TOKEN"])
socket_client = SocketModeClient(
app_token=os.environ["SLACK_APP_TOKEN"],
web_client=web_client,
concurrency=100,
auto_reconnect_enabled=True,
trace_enabled=True,
)
socket_client.connect()
- While that client is running, disconnect it from the Internet. This forces the active websockets to lose connection and simulates a DNS outage.
- Wait until these stacktraces appear in an infinite loop:
2024-02-15 14:32:20,957 INFO:slack_sdk.socket_mode.websocket_client:Starting to receive messages from a new connection
2024-02-15 14:32:20,957 - slack_sdk.socket_mode.websocket_client - INFO - Starting to receive messages from a new connection
2024-02-15 14:32:20,958 ERROR:slack_sdk.socket_mode.websocket_client:Failed to start or stop the current session: socket is already opened
Traceback (most recent call last):
File "/path/venv/lib/python3.9/site-packages/slack_sdk/socket_mode/websocket_client/__init__.py", line 237, in _run_current_session
self.current_session.run_forever(
File "/path/venv/lib/python3.9/site-packages/websocket/_app.py", line 422, in run_forever
raise WebSocketException("socket is already opened")
websocket._exceptions.WebSocketException: socket is already opened
- Reconnect the Internet and observe that the client never reconnects and continues providing the stacktraces above.
Expected result:
I expected the client to reconnect.
Actual result:
The client never reconnects.
Possible fix:
This function could set self.current_session = None in the event of an Exception. https://github.com/slackapi/python-slack-sdk/blob/60b02d6ea56fe3c4cd9d5d51370f99ca5f9795f3/slack_sdk/socket_mode/websocket_client/init.py#L233-L246