Skip to content

Commit

Permalink
more places where connection shouldn't be reset too quickly
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Jan 27, 2019
1 parent cf73d2d commit 693b51b
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 9 deletions.
4 changes: 1 addition & 3 deletions engineio/asyncio_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ async def _send_packet(self, pkt):

async def _send_request(
self, method, url, headers=None, body=None): # pragma: no cover
if self.http is None:
if self.http is None or self.http.closed:
self.http = aiohttp.ClientSession()
method = getattr(self.http, method.lower())
try:
Expand Down Expand Up @@ -495,7 +495,6 @@ async def _write_loop(self):
if r is None:
self.logger.warning(
'Connection refused by the server, aborting')
self._reset()
break
if r.status != 200:
self.logger.warning('Unexpected status code %s in server '
Expand All @@ -512,6 +511,5 @@ async def _write_loop(self):
self.logger.warning(
'Write loop: WebSocket connection was closed, '
'aborting')
self._reset()
break
self.logger.info('Exiting write loop task')
2 changes: 0 additions & 2 deletions engineio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,6 @@ def _write_loop(self):
if r is None:
self.logger.warning(
'Connection refused by the server, aborting')
self._reset()
break
if r.status != 200:
self.logger.warning('Unexpected status code %s in server '
Expand All @@ -597,6 +596,5 @@ def _write_loop(self):
except websocket.WebSocketConnectionClosedException:
self.logger.warning(
'WebSocket connection was closed, aborting')
self._reset()
break
self.logger.info('Exiting write loop task')
4 changes: 2 additions & 2 deletions tests/asyncio/test_asyncio_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1054,7 +1054,7 @@ def test_write_loop_polling_bad_connection(self):
c._send_request.mock.assert_called_once_with(
'POST', 'http://foo', body=p.encode(),
headers={'Content-Type': 'application/octet-stream'})
self.assertEqual(c.state, 'disconnected')
self.assertEqual(c.state, 'connected')

def test_write_loop_polling_bad_status(self):
c = asyncio_client.AsyncClient()
Expand Down Expand Up @@ -1150,4 +1150,4 @@ def test_write_loop_websocket_bad_connection(self):
c.ws.send = AsyncMock(
side_effect=websockets.exceptions.ConnectionClosed(1, 'foo'))
_run(c._write_loop())
self.assertEqual(c.state, 'disconnected')
self.assertEqual(c.state, 'connected')
4 changes: 2 additions & 2 deletions tests/common/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,7 @@ def test_write_loop_polling_bad_connection(self):
c._send_request.assert_called_once_with(
'POST', 'http://foo', body=p.encode(),
headers={'Content-Type': 'application/octet-stream'})
self.assertEqual(c.state, 'disconnected')
self.assertEqual(c.state, 'connected')

def test_write_loop_polling_bad_status(self):
c = client.Client()
Expand Down Expand Up @@ -1080,7 +1080,7 @@ def test_write_loop_websocket_bad_connection(self):
c.ws = mock.MagicMock()
c.ws.send.side_effect = websocket.WebSocketConnectionClosedException
c._write_loop()
self.assertEqual(c.state, 'disconnected')
self.assertEqual(c.state, 'connected')

@mock.patch('engineio.client.original_signal_handler')
def test_signal_handler(self, original_handler):
Expand Down

2 comments on commit 693b51b

@imcom
Copy link

@imcom imcom commented on 693b51b Feb 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@miguelgrinberg i am new to this .. but why would you want no reset on above cases?

if self.eio.state == 'connected' and self.reconnection:
            self._reconnect_task = self.start_background_task(
                self._handle_reconnect)

Due to the reconnect impl in python-socketio?

@miguelgrinberg
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The connection is reset a bit later. The bug was that with this early reset the code that follows did not detect this as an interruption and instead handled it as an explicit disconnect. In the end the connection will be reset the same, but the proper handling for an unexpected interruption will be done in addition to that.

Please sign in to comment.