Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/socketio/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ async def connect(self, url, headers={}, auth=None, transports=None,
self.connection_namespaces = namespaces
self.namespaces = {}
self.failed_namespaces = []
self._connection_dropped = False
if self._connect_event is None:
self._connect_event = self.eio.create_event()
else:
Expand Down Expand Up @@ -180,6 +181,8 @@ async def connect(self, url, headers={}, auth=None, transports=None,
'One or more namespaces failed to connect: '
+ ', '.join(self.failed_namespaces))

if self._connection_dropped:
raise exceptions.ConnectionError('Connection dropped')
self.connected = True

async def wait(self):
Expand Down Expand Up @@ -590,6 +593,7 @@ async def _handle_eio_message(self, data):

async def _handle_eio_disconnect(self, reason):
"""Handle the Engine.IO disconnection event."""
self._connection_dropped = True
self.logger.info('Engine.IO connection dropped')
will_reconnect = self.reconnection and self.eio.state == 'connected'
if self.connected:
Expand Down
1 change: 1 addition & 0 deletions src/socketio/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def __init__(self, reconnection=True, reconnection_attempts=0,
self.namespace_handlers = {}
self.callbacks = {}
self._binary_packet = None
self._connection_dropped = False
self._connect_event = None
self._reconnect_task = None
self._reconnect_abort = None
Expand Down
4 changes: 4 additions & 0 deletions src/socketio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def connect(self, url, headers={}, auth=None, transports=None,
self.connection_namespaces = namespaces
self.namespaces = {}
self.failed_namespaces = []
self._connection_dropped = False
if self._connect_event is None:
self._connect_event = self.eio.create_event()
else:
Expand Down Expand Up @@ -172,6 +173,8 @@ def connect(self, url, headers={}, auth=None, transports=None,
'One or more namespaces failed to connect: '
+ ', '.join(self.failed_namespaces))

if self._connection_dropped:
raise exceptions.ConnectionError('Connection dropped')
self.connected = True

def wait(self):
Expand Down Expand Up @@ -540,6 +543,7 @@ def _handle_eio_message(self, data):

def _handle_eio_disconnect(self, reason):
"""Handle the Engine.IO disconnection event."""
self._connection_dropped = True
self.logger.info('Engine.IO connection dropped')
will_reconnect = self.reconnection and self.eio.state == 'connected'
if self.connected:
Expand Down
19 changes: 19 additions & 0 deletions tests/async/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,25 @@ async def mock_connect():
)
assert c.connected is True

async def test_connect_dropped_during_handshake(self):
c = async_client.AsyncClient()
c.eio.connect = mock.AsyncMock()
c._connect_event = mock.MagicMock()

async def mock_connect():
c.namespaces = {'/': '123'}
await c._handle_eio_disconnect('transport error')
return True

c._connect_event.wait = mock_connect
with pytest.raises(exceptions.ConnectionError):
await c.connect(
'url',
wait=True,
wait_timeout=0.01,
)
assert c.connected is False

async def test_connect_wait_two_namespaces(self):
c = async_client.AsyncClient()
c.eio.connect = mock.AsyncMock()
Expand Down
19 changes: 19 additions & 0 deletions tests/common/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,25 @@ def mock_connect(timeout):
)
assert c.connected is True

def test_connect_dropped_during_handshake(self):
c = client.Client()
c.eio.connect = mock.MagicMock()
c._connect_event = mock.MagicMock()

def mock_connect(timeout):
c.namespaces = {'/': '123'}
c._handle_eio_disconnect('transport error')
return True

c._connect_event.wait = mock_connect
with pytest.raises(exceptions.ConnectionError):
c.connect(
'url',
wait=True,
wait_timeout=0.01,
)
assert c.connected is False

def test_connect_wait_two_namespaces(self):
c = client.Client()
c.eio.connect = mock.MagicMock()
Expand Down