Skip to content

Commit

Permalink
Fix hang on KeyboardInterrupt when running with asyncio. (#95)
Browse files Browse the repository at this point in the history
* Fix hang on KeyboardInterrupt when running with asyncio.

The bare except in the _service_task function also catches and
ignores asyncio.CancelledError exceptions. This means the function
will never gracefully exit when cancelling the task.

The solution I propose here adds an additional except handler
specifically for asyncio.CancelledError which will then stop the
service task.

This allows f.e. aiohttp to cancel all pending tasks and allows
the process to terminate even when there are connected clients.

* Also exit service task on KeyboardInterrupt.
  • Loading branch information
iksteen authored and miguelgrinberg committed Feb 11, 2019
1 parent f9f88a7 commit c60499e
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions engineio/asyncio_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,9 @@ async def _service_task(self): # pragma: no cover
if not socket.closing and not socket.closed:
await socket.check_ping_timeout()
await self.sleep(sleep_interval)
except (KeyboardInterrupt, asyncio.CancelledError):
self.logger.debug('service task cancelled')
break
except:
# an unexpected exception has occurred, log it and continue
self.logger.exception('service task exception')

0 comments on commit c60499e

Please sign in to comment.