Python-slack-sdk: Connection is already closed

Created on 29 Sep 2016  路  21Comments  路  Source: slackapi/python-slack-sdk

  • [X] I've read and understood the Contributing guidelines and have done my best effort to follow them.
  • [X] I've read and agree to the Code of Conduct.
  • [X] I've searched for any related issues and avoided creating a duplicate issue.

    Description

Slack teams with a significant number of users may never connect to Slack
My slack team has 9,500+ users and upon bot application startup will experience a "connection is already closed"
In commit 875839b - slackclient/_server.py.rtm_connect(self, reconnect=False):
If line 74 self.connect_slack_websocket(self.ws_url) is moved before if not reconnect: on line 72 it will allow the websocket to establish connection so then parse_slack_login_data can run for a lengthy duration without causing the issue.

Reproducible in:

  • [?] This is reproducible in the sample project. -- Possibly if your team's user count is sufficiently large. "large" would mean that parse_slack_login_data(self, login_data) takes over 30 seconds; which is the validity period for the websocket url. Alternatively you can import time and sleep 45(or so) seconds in the parse_slack_login_data function to simulate the delay I experience.
    I resorted to console outputs to capture our real-world times while troubleshooting:
Start-parse in rtm_connect: 16:19:37
    Start-parse channel-channels:   16:19:37
    End-parse channel-channels: 16:19:39
    Elapsed-parse channel-channels: 2.35307884216
    Start-parse channel-groups: 16:19:39
    End-parse channel-groups:   16:19:39
    Elapsed-parse channel-groups:   0.00262713432312
    Start-parse channel-ims:     16:19:39
    End-parse channel-ims:  16:19:39
    Elapsed-parse channel-ims:  0.00488305091858
    Start-parse users:  16:19:39
    End-parse users:    16:20:25
    Elapsed-parse users:    45.6203141212
End-parse in rtm_connect:   16:20:25
Elapsed-parse rtm_connect:  47.9812588692

python-slackclient version: slackclient-1.0.2(?)
Python Version: 2.7.10
OS Version: CentOS Linux release 7.1.1503 (Core)

Steps to reproduce:



    1. 2.

  1. Expected result:

valid connection to Slack over RTM

Actual result:

```Traceback (most recent call last):
File ./rtmbot.py, line 26, in
bot.start()
File /python-rtmbot/python-rtmbot/rtmbot/core.py, line 78, in start
self._start()
File /python-rtmbot/python-rtmbot/rtmbot/core.py, line 65, in _start
for reply in self.slack_client.rtm_read():
File /python-rtmbot/lib/python2.7/site-packages/slackclient/_client.py, line 120, in rtm_read
json_data = self.server.websocket_safe_read()
File /python-rtmbot/lib/python2.7/site-packages/slackclient/_server.py, line 184, in websocket_safe_read
data += {0}n.format(self.websocket.recv())
File /python-rtmbot/lib/python2.7/site-packages/websocket/_core.py, line 298, in recv
opcode, data = self.recv_data()
File /python-rtmbot/lib/python2.7/site-packages/websocket/_core.py, line 315, in recv_data
opcode, frame = self.recv_data_frame(control_frame)
File /python-rtmbot/lib/python2.7/site-packages/websocket/_core.py, line 328, in recv_data_frame
frame = self.recv_frame()
File /python-rtmbot/lib/python2.7/site-packages/websocket/_core.py, line 360, in recv_frame
return self.frame_buffer.recv_frame()
File /python-rtmbot/lib/python2.7/site-packages/websocket/_abnf.py, line 312, in recv_frame
self.recv_header()
File /python-rtmbot/lib/python2.7/site-packages/websocket/_abnf.py, line 261, in recv_header
header = self.recv_strict(2)
File /python-rtmbot/lib/python2.7/site-packages/websocket/_abnf.py, line 346, in recv_strict
bytes = self.recv(min(16384, shortage))
File /python-rtmbot/lib/python2.7/site-packages/websocket/_core.py, line 429, in _recv
return recv(self.sock, bufsize)
File /python-rtmbot/lib/python2.7/site-packages/websocket/_socket.py, line 89, in recv
raise WebSocketConnectionClosedException(Connection is already closed.)
websocket._exceptions.WebSocketConnectionClosedException: Connection is already closed.


Attachments:

e.g. Logs, screenshots, screencast, sample project, funny gif, etc.

bug

Most helpful comment

I faced the same issue on Ubuntu 16.04, Python 3.6. slackclient 1.1.0 : The bot had been running for a few hours until the error occurred.
websocket._exceptions.WebSocketConnectionClosedException: Connection is already closed.

All 21 comments

Did you ever get any follow-up or a resolution to this issue? I'm seeing the same problem.

thanks for this report; helped get my own bot working. Starring - would love to see this make it into the codebase.

This has been deployed and is available in v1.0.3 馃帀

Unfortunately I still encounter this error with version 1.0.5 running in python 3.5.2 under Ubuntu 16.04.2 LTS. The bot had been running for a few days until the error occurred.

```Traceback (most recent call last):
File "/home/mikulcak/essensplan_slack_bot/slack_bot.py", line 143, in
command, channel, user = parse_slack_output(slack_client.rtm_read())
File "/usr/local/lib/python3.5/dist-packages/slackclient/_client.py", line 123, in rtm_read
json_data = self.server.websocket_safe_read()
File "/usr/local/lib/python3.5/dist-packages/slackclient/_server.py", line 141, in websocket_safe_read
data += "{0}n".format(self.websocket.recv())
File "/usr/local/lib/python3.5/dist-packages/websocket/_core.py", line 293, in recv
opcode, data = self.recv_data()
File "/usr/local/lib/python3.5/dist-packages/websocket/_core.py", line 310, in recv_data
opcode, frame = self.recv_data_frame(control_frame)
File "/usr/local/lib/python3.5/dist-packages/websocket/_core.py", line 323, in recv_data_frame
frame = self.recv_frame()
File "/usr/local/lib/python3.5/dist-packages/websocket/_core.py", line 357, in recv_frame
return self.frame_buffer.recv_frame()
File "/usr/local/lib/python3.5/dist-packages/websocket/_abnf.py", line 336, in recv_frame
self.recv_header()
File "/usr/local/lib/python3.5/dist-packages/websocket/_abnf.py", line 286, in recv_header
header = self.recv_strict(2)
File "/usr/local/lib/python3.5/dist-packages/websocket/_abnf.py", line 371, in recv_strict
bytes_ = self.recv(min(16384, shortage))
File "/usr/local/lib/python3.5/dist-packages/websocket/_core.py", line 427, in _recv
return recv(self.sock, bufsize)
File "/usr/local/lib/python3.5/dist-packages/websocket/_socket.py", line 93, in recv
"Connection is already closed.")
websocket._exceptions.WebSocketConnectionClosedException: Connection is already closed.

Hi guys.
This is what I found (my bot connects really seldom) during my debugging.
When RTM (real time websocket) starts it sends rtm.start request first. In the response there is websocket url along with other information such as users (all users) etc...
In my case this response has cca 40 megabytes (yeah megabytes).
As per documentation:
The Websocket URLs provided by rtm.start are single-use and are only valid for 30 seconds, so make sure to connect quickly. If you connect successfully the first event received will be a hello:
The message is consumed and parsed usually not in this interval (30 seconds) so I got usually this response:
{u'type': u'error', u'error': {u'msg': u'Socket URL has expired', u'code': 1}}

Then my bot tries to reconnect and this scenario is usually repeating over and over.

i'm having trouble finding the change mentioned here that went out in 1.0.3. perhaps it got lost in a merge somewhere.

i agree with OP that reordering the methods makes sense and should resolve a large portion of the timeout issues. lets land this quick win!

for completeness, the timeout issue is also related to #190

I faced the same issue on Ubuntu 16.04, Python 3.6. slackclient 1.1.0 : The bot had been running for a few hours until the error occurred.
websocket._exceptions.WebSocketConnectionClosedException: Connection is already closed.

Fixed in 2.1.0

I am facing the same issue on python3.5 , ubuntu 16.04 and slackclient 1.3.0

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/site-packages/slackclient/server.py", line 278, in websocket_safe_read
    data += "{0}\n".format(self.websocket.recv())
  File "/usr/local/lib/python3.5/site-packages/websocket/_core.py", line 302, in recv
    opcode, data = self.recv_data()
  File "/usr/local/lib/python3.5/site-packages/websocket/_core.py", line 319, in recv_data
    opcode, frame = self.recv_data_frame(control_frame)
  File "/usr/local/lib/python3.5/site-packages/websocket/_core.py", line 332, in recv_data_frame
    frame = self.recv_frame()
  File "/usr/local/lib/python3.5/site-packages/websocket/_core.py", line 366, in recv_frame
    return self.frame_buffer.recv_frame()
  File "/usr/local/lib/python3.5/site-packages/websocket/_abnf.py", line 361, in recv_frame
    self.recv_header()
  File "/usr/local/lib/python3.5/site-packages/websocket/_abnf.py", line 309, in recv_header
    header = self.recv_strict(2)
  File "/usr/local/lib/python3.5/site-packages/websocket/_abnf.py", line 396, in recv_strict
    bytes_ = self.recv(min(16384, shortage))
  File "/usr/local/lib/python3.5/site-packages/websocket/_core.py", line 441, in _recv
    return recv(self.sock, bufsize)
  File "/usr/local/lib/python3.5/site-packages/websocket/_socket.py", line 94, in recv
    "Connection is already closed.")
websocket._exceptions.WebSocketConnectionClosedException: Connection is already closed.

@Roach , pip install slackclient==2.1.0

Collecting slackclient==2.1.0
  Could not find a version that satisfies the requirement slackclient==2.1.0 (from versions: 0.13, 0.14, 0.15, 0.16, 0.17, 0.18.0, 1.0.0, 1.0.1, 1.0.2, 1.0.3, 1.0.4, 1.0.5, 1.0.6, 1.0.7, 1.0.8, 1.0.9, 1.1.0, 1.1.1, 1.1.2, 1.1.3, 1.2.0, 1.2.1, 1.3.0)
No matching distribution found for slackclient==2.1.0

@cmaliwal - that may have been a typo by @Roach , can you try pip install slackclient==1.2.0? I imagine it should be fixed with later versions however, is this the latest version you're using?

(Pypi has v1.2.0 published a day after Roach's comment above so I reckon twas just a typo - https://pypi.org/project/slackclient/1.2.0/ )

@splinterific , I am already using slackclient 1.3.0 . ( latest version ) .

I'm reopening this issue, but removing the bug label so that we can reinvestigate the issue in the context of v1.3.0, and so it doesn't drop off our radar.

Thanks @aoberoi .

Hello - experiencing this since today, after our bot was running smoothly for a year. Suddenly today it tanked and restarting doesn't help. The code for rtm.connect() and rtm.read() hasn't changed:

slack_client = SlackClient(os.environ.get('SLACK_BOT_TOKEN'))
starterbot_id = None

RTM_READ_DELAY = 1

if slack_client.rtm_connect(auto_reconnect=True, with_team_state=False):
        starterbot_id = slack_client.api_call("auth.test")["user_id"]
        while True:
            command, channel = parse_bot_commands(slack_client.rtm_read())
            if command:
                handle_command(command, channel)
            time.sleep(RTM_READ_DELAY)
    else:
        print("Connection failed. Exception traceback printed above.")

The error messages are always:
slackclient.server.SlackConnectionError: RTM connection failed, reached max reconnects.
and
websocket._exceptions.WebSocketConnectionClosedException: Connection is already closed.

The crash always happen in the while loop at rtm_read()

The bot is successfully installed in the workspace, the tokens check out, and calls to slack_client.api_call always succeed - it's "just" the rtm_connect/rtm_read that gives us headaches.

We were running on slackclient v1.2.1 and now upgraded to v1.3.0 but the error stays

Note: downgrading websocket-client to 0.54.0 fixes it (I finally saw https://github.com/slackapi/python-slackclient/pull/387)

@cmaliwal Can you confirm when you're connecting with RTM if you're using the auto reconnect config? i.e. SlackClient('token').rtm_connect(auto_reconnect=True)

I've been having the same issue and a couple days ago I added the auto_reconnect=True parameter, and the service hasn't disconnected since.

Before it looked like such: SlackClient('token').rtm_connect()

Installing websocket-client==0.55.0 resolves the issue

I encountered this issue...
I try to uninstall websocket-client-0.56.0, and install 0.55.0 to see if it works:

pip3.7 uninstall websocket-client

pip3.7 install websocket-client==0.55.0

Hi folks,

It seems like we've talked about several issues within this issue. They all happen to have the WebSocketConnectionClosedException, so I think that's how we all ended up here. Let's try to separate them:

  1. @JohnB520's original issue pertains to the order of operations when calling client.rtm_connect() to cause the websocket URL to no longer be valid by the time the websocket connection attempt was made. This was most apparent when the team that the app was connecting to was very large (lots of members, lots of channels). This issue was fixed in #193. Also, using with_team_state=False can generally speed up the time it takes to connect. It seems @exodusftw, @holozip, @phonkee were having this issue. If it is not fixed for any of you, let us know. If so, we will reopen this issue.

  2. An error with both client.rtm_connect() and client.rtm_read() which was the result of a regression in websocket-client==0.56.0. This is a duplicate of #385. We addressed this issue in slackclient==1.3.1. @rowolff was having this issue. If it is not fixed for you, please comment on #385, and we will reopen that issue.

  3. An error when calling client.rtm_read(), which seems to indicate that the connection timed out since the last time the socket was read. This would occur when processing the messages you read take a long time, and therefore the while True: loop slows down to the point where the connection idles and disconnects. This problem could be addressed with using the auto_reconnect=True option on client.rtm_connect(). @CaymonSullivan and @ideaman924 seem to have been having this issue. If it is not fixed for any of you, please open a new issue and capture as much detail as you can in the bug report.

@mikulcak: Since your traceback shows rtm_read(), and your comment occurred before the regression, I think you were having problem 3. Are you still having this problem?

@alexattia: You haven't provided much information, but your comment is also before the regression. I think you had either problem 1 or 3. Are you still having this problem?

@cmaliwal: We haven't heard from you since the slackclient==1.3.1 release. This leads me to believe you had problem 2. Are you still having this problem?

@peteramalraj: I'm not sure what's going on in your app because websocket-client==0.55.0 was the faulty version with the regression.

@mosjin: I'm not sure what your issue is. Can you please check all 3 issues above to see if any of the remedies fix your issue? If not, please open a new issue and capture as much detail as you can in the bug report.


With that said, I'm closing this issue. This should only be used to discuss problem 1. If you're experiencing that, please comment and I will reopen.

Was this page helpful?
0 / 5 - 0 ratings