Skip to content

Commit 6687b1a

Browse files
committed
Detect disconnected websocket connections
With the current implementation, a disconnected websocket is not detected as *all* exceptions are silently swallowed, resulting in `rtm_read()` returning an empty list forever without raising any kind of exception. This makes it impossible to detect that the websocket has been disconnected and keeps client libraries reading forever while never receiving data anymore. With these changes, a `TimeoutError` will be thrown when the socket gets disconnected (and other exceptions should bubble up as well) so that users of this library know they must re-connect.
1 parent 39de600 commit 6687b1a

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

slackclient/_server.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from slackclient._channel import Channel
33
from slackclient._user import User
44
from slackclient._util import SearchList
5+
from ssl import SSLWantReadError
56

67
from websocket import create_connection
78
import json
@@ -106,8 +107,9 @@ def websocket_safe_read(self):
106107
while True:
107108
try:
108109
data += "{}\n".format(self.websocket.recv())
109-
except:
110-
return data.rstrip()
110+
except SSLWantReadError:
111+
return ''
112+
return data.rstrip()
111113

112114
def attach_user(self, name, id, real_name, tz):
113115
self.users.append(User(self, name, id, real_name, tz))

0 commit comments

Comments
 (0)