python-slack-sdk icon indicating copy to clipboard operation
python-slack-sdk copied to clipboard

SocketModeClient does not reconnect after a DNS outage

Open ktindall-godaddy opened this issue 2 years ago • 0 comments

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:

  1. Have a SocketModeClient with 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()
  1. While that client is running, disconnect it from the Internet. This forces the active websockets to lose connection and simulates a DNS outage.
  2. 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
  1. 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

ktindall-godaddy avatar Feb 16 '24 15:02 ktindall-godaddy