Random ConnectionError using python-socketio on the client side

85 views Asked by At

I am using python-socketio to connect to a third party server using the socketIO protocol. The server is out of my control, therefore I do not have access to server side logs, etc... . The error below happens randomly (say half of the times), so my current workaround is to retry until the connection succeeds.

I have found other posts where the same exception is thrown, but in those cases it was a systematic error and the corresponding solutions do not apply to my case.

Any help to completely avoid the exception?

Code

# sio_mwe.py

from socketio import Client

sio_url = 'https://r.thewebsite.net?user=myuser&name=myname&credentials=mysecret'

sio = Client(logger=True, engineio_logger=True, reconnection_attempts=5)
sio.on('*', lambda x: print("Inside handler:", x))

sio.connect(sio_url, socketio_path='r')
print("-- CONNECTED --")

sio.emit('signal', data=f'mydata')
print("-- EMITTED --")

Logs

Below you can find the logs generated when the above MWE has been repeatedly executed.

$ python sio_mwe.py 
Attempting polling connection to https://r.thewebsite.net/r/?user=myuser&name=myname&credentials=mysecret&transport=polling&EIO=4
Polling connection accepted with {'sid': 'ghfrdZaKRjVS2vcBACqE', 'upgrades': ['websocket'], 'pingInterval': 25000, 'pingTimeout': 20000, 'maxPayload': 1000000}
Engine.IO connection established
Sending packet MESSAGE data 0{}
Attempting WebSocket upgrade to wss://r.thewebsite.net/r/?user=myuser&name=myname&credentials=mysecret&transport=websocket&EIO=4
WebSocket upgrade was successful
Received packet MESSAGE data 0{"sid":"7TWzrBV3ehhR212PACqG"}
Namespace / is connected
-- CONNECTED --
Emitting event "signal" [/]
Sending packet MESSAGE data 2["signal","mydata"]
-- EMITTED --
$ python sio_mwe.py 
Attempting polling connection to https://r.thewebsite.net/r/?user=myuser&name=myname&credentials=mysecret&transport=polling&EIO=4
Polling connection accepted with {'sid': 'yiL2tFH4gy4FjExvACqI', 'upgrades': ['websocket'], 'pingInterval': 25000, 'pingTimeout': 20000, 'maxPayload': 1000000}
Engine.IO connection established
Sending packet MESSAGE data 0{}
Attempting WebSocket upgrade to wss://r.thewebsite.net/r/?user=myuser&name=myname&credentials=mysecret&transport=websocket&EIO=4
WebSocket upgrade was successful
Received packet MESSAGE data 0{"sid":"UpcoKLNq0X_ifz7oACqJ"}
Namespace / is connected
-- CONNECTED --
Emitting event "signal" [/]
Sending packet MESSAGE data 2["signal","mydata"]
-- EMITTED --
$ python sio_mwe.py 
Attempting polling connection to https://r.thewebsite.net/r/?user=myuser&name=myname&credentials=mysecret&transport=polling&EIO=4
Polling connection accepted with {'sid': 'qmWjIe407V-92_7_ACqK', 'upgrades': ['websocket'], 'pingInterval': 25000, 'pingTimeout': 20000, 'maxPayload': 1000000}
Engine.IO connection established
Sending packet MESSAGE data 0{}
Attempting WebSocket upgrade to wss://r.thewebsite.net/r/?user=myuser&name=myname&credentials=mysecret&transport=websocket&EIO=4
WebSocket upgrade was successful
WebSocket connection was closed, aborting
Waiting for write loop task to end
Exiting write loop task
Engine.IO connection dropped
Connection failed, new attempt in 1.45 seconds
Exiting read loop task
Traceback (most recent call last):
  File "**OMISSIS**/sio_mwe.py", line 39, in <module>
    sio.connect(sio_url, socketio_path='r')
  File "**OMISSIS**/python3.10/site-packages/socketio/client.py", line 163, in connect
    raise exceptions.ConnectionError(
socketio.exceptions.ConnectionError: One or more namespaces failed to connect
$ python sio_mwe.py 
Attempting polling connection to https://r.thewebsite.net/r/?user=myuser&name=myname&credentials=mysecret&transport=polling&EIO=4
Polling connection accepted with {'sid': 'bsU8bGZDK3C-dLEYACqS', 'upgrades': ['websocket'], 'pingInterval': 25000, 'pingTimeout': 20000, 'maxPayload': 1000000}
Engine.IO connection established
Sending packet MESSAGE data 0{}
Attempting WebSocket upgrade to wss://r.thewebsite.net/r/?user=myuser&name=myname&credentials=mysecret&transport=websocket&EIO=4
WebSocket upgrade was successful
WebSocket connection was closed, aborting
Waiting for write loop task to end
Exiting write loop task
Engine.IO connection dropped
Connection failed, new attempt in 1.33 seconds
Exiting read loop task
Traceback (most recent call last):
  File "**OMISSIS**/sio_mwe.py", line 39, in <module>
    sio.connect(sio_url, socketio_path='r')
  File "**OMISSIS**/python3.10/site-packages/socketio/client.py", line 163, in connect
    raise exceptions.ConnectionError(
socketio.exceptions.ConnectionError: One or more namespaces failed to connect
$ python sio_mwe.py 
Attempting polling connection to https://r.thewebsite.net/r/?user=myuser&name=myname&credentials=mysecret&transport=polling&EIO=4
Polling connection accepted with {'sid': '6TqMXBDJspZBe92YACqY', 'upgrades': ['websocket'], 'pingInterval': 25000, 'pingTimeout': 20000, 'maxPayload': 1000000}
Engine.IO connection established
Sending packet MESSAGE data 0{}
Attempting WebSocket upgrade to wss://r.thewebsite.net/r/?user=myuser&name=myname&credentials=mysecret&transport=websocket&EIO=4
WebSocket upgrade was successful
Received packet MESSAGE data 0{"sid":"P10loKi4B23X79xqACqZ"}
Namespace / is connected
-- CONNECTED --
Emitting event "signal" [/]
Sending packet MESSAGE data 2["signal","mydata"]
-- EMITTED --
$ python sio_mwe.py 
Attempting polling connection to https://r.thewebsite.net/r/?user=myuser&name=myname&credentials=mysecret&transport=polling&EIO=4
Polling connection accepted with {'sid': 'AmSCvIsjdRpnsdHwACqc', 'upgrades': ['websocket'], 'pingInterval': 25000, 'pingTimeout': 20000, 'maxPayload': 1000000}
Engine.IO connection established
Sending packet MESSAGE data 0{}
Attempting WebSocket upgrade to wss://r.thewebsite.net/r/?user=myuser&name=myname&credentials=mysecret&transport=websocket&EIO=4
WebSocket upgrade was successful
Received packet MESSAGE data 0{"sid":"veuDxr2k5dLLfX20ACqd"}
Namespace / is connected
-- CONNECTED --
Emitting event "signal" [/]
Sending packet MESSAGE data 2["signal","mydata"]
-- EMITTED --
$ python sio_mwe.py 
Attempting polling connection to https://r.thewebsite.net/r/?user=myuser&name=myname&credentials=mysecret&transport=polling&EIO=4
Polling connection accepted with {'sid': '2tiYT9ZTqRiumIv4ACqe', 'upgrades': ['websocket'], 'pingInterval': 25000, 'pingTimeout': 20000, 'maxPayload': 1000000}
Engine.IO connection established
Sending packet MESSAGE data 0{}
Attempting WebSocket upgrade to wss://r.thewebsite.net/r/?user=myuser&name=myname&credentials=mysecret&transport=websocket&EIO=4
WebSocket upgrade was successful
WebSocket connection was closed, aborting
Waiting for write loop task to end
Exiting write loop task
Engine.IO connection dropped
Connection failed, new attempt in 0.85 seconds
Exiting read loop task
Attempting polling connection to https://r.thewebsite.net/r/?user=myuser&name=myname&credentials=mysecret&transport=polling&EIO=4
Polling connection accepted with {'sid': 'PAOF0QWb1rfW0f9-ACqg', 'upgrades': ['websocket'], 'pingInterval': 25000, 'pingTimeout': 20000, 'maxPayload': 1000000}
Engine.IO connection established
Sending packet MESSAGE data 0{}
Attempting WebSocket upgrade to wss://r.thewebsite.net/r/?user=myuser&name=myname&credentials=mysecret&transport=websocket&EIO=4
Sending packet CLOSE data None
Engine.IO connection dropped
Traceback (most recent call last):
  File "**OMISSIS**/sio_mwe.py", line 39, in <module>
    sio.connect(sio_url, socketio_path='r')
  File "**OMISSIS**/python3.10/site-packages/socketio/client.py", line 163, in connect
    raise exceptions.ConnectionError(
socketio.exceptions.ConnectionError: One or more namespaces failed to connect
$ python sio_mwe.py 
Attempting polling connection to https://r.thewebsite.net/r/?user=myuser&name=myname&credentials=mysecret&transport=polling&EIO=4
Polling connection accepted with {'sid': 'H-81bc5akux_oi7MACqq', 'upgrades': ['websocket'], 'pingInterval': 25000, 'pingTimeout': 20000, 'maxPayload': 1000000}
Engine.IO connection established
Sending packet MESSAGE data 0{}
Attempting WebSocket upgrade to wss://r.thewebsite.net/r/?user=myuser&name=myname&credentials=mysecret&transport=websocket&EIO=4
WebSocket upgrade was successful
WebSocket connection was closed, aborting
Waiting for write loop task to end
Exiting write loop task
Engine.IO connection dropped
Connection failed, new attempt in 1.44 seconds
Exiting read loop task
Traceback (most recent call last):
  File "**OMISSIS**/sio_mwe.py", line 39, in <module>
    sio.connect(sio_url, socketio_path='r')
  File "**OMISSIS**/python3.10/site-packages/socketio/client.py", line 163, in connect
    raise exceptions.ConnectionError(
socketio.exceptions.ConnectionError: One or more namespaces failed to connect
$ python sio_mwe.py 
Attempting polling connection to https://r.thewebsite.net/r/?user=myuser&name=myname&credentials=mysecret&transport=polling&EIO=4
Polling connection accepted with {'sid': 'vacxuBJMCJrBZq7xACqs', 'upgrades': ['websocket'], 'pingInterval': 25000, 'pingTimeout': 20000, 'maxPayload': 1000000}
Engine.IO connection established
Sending packet MESSAGE data 0{}
Attempting WebSocket upgrade to wss://r.thewebsite.net/r/?user=myuser&name=myname&credentials=mysecret&transport=websocket&EIO=4
WebSocket upgrade was successful
WebSocket connection was closed, aborting
Waiting for write loop task to end
Exiting write loop task
Engine.IO connection dropped
Connection failed, new attempt in 0.55 seconds
Exiting read loop task
Attempting polling connection to https://r.thewebsite.net/r/?user=myuser&name=myname&credentials=mysecret&transport=polling&EIO=4
Polling connection accepted with {'sid': '37uV5uncNZ69q-CJACqx', 'upgrades': ['websocket'], 'pingInterval': 25000, 'pingTimeout': 20000, 'maxPayload': 1000000}
Engine.IO connection established
Sending packet MESSAGE data 0{}
Attempting WebSocket upgrade to wss://r.thewebsite.net/r/?user=myuser&name=myname&credentials=mysecret&transport=websocket&EIO=4
WebSocket upgrade was successful
Received packet MESSAGE data 0{"sid":"pvYNYRBEBN0qVIDWACqy"}
Namespace / is connected
-- CONNECTED --
Reconnection successful
Emitting event "signal" [/]
Sending packet MESSAGE data 2["signal","mydata"]
-- EMITTED --

Versions

Client:

  • Python: 3.10
  • python-engineio: 4.9.0
  • python-socketio: 5.11.1

Server:

  • SocketIO: 4.5.0
0

There are 0 answers