Skip to content

Commit

Permalink
Catch and log all errors that occur in event handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Sep 6, 2023
1 parent 5357808 commit 2bef6d6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
26 changes: 14 additions & 12 deletions src/engineio/asyncio_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,13 +469,10 @@ async def _trigger_event(self, event, *args, **kwargs):
run_async = kwargs.pop('run_async', False)
ret = None
if event in self.handlers:
if asyncio.iscoroutinefunction(self.handlers[event]) is True:
if run_async:
return self.start_background_task(self.handlers[event],
*args)
else:
if asyncio.iscoroutinefunction(self.handlers[event]):
async def run_async_handler():
try:
ret = await self.handlers[event](*args)
return await self.handlers[event](*args)
except asyncio.CancelledError: # pragma: no cover
pass
except:
Expand All @@ -484,21 +481,26 @@ async def _trigger_event(self, event, *args, **kwargs):
# if connect handler raised error we reject the
# connection
return False
else:
if run_async:
async def async_handler():
return self.handlers[event](*args)

return self.start_background_task(async_handler)
if run_async:
ret = self.start_background_task(run_async_handler)
else:
ret = await run_async_handler()
else:
async def run_sync_handler():
try:
ret = self.handlers[event](*args)
return self.handlers[event](*args)
except:
self.logger.exception(event + ' handler error')
if event == 'connect':
# if connect handler raised error we reject the
# connection
return False

if run_async:
ret = self.start_background_task(run_sync_handler)
else:
ret = await run_sync_handler()
return ret

async def _service_task(self): # pragma: no cover
Expand Down
9 changes: 6 additions & 3 deletions src/engineio/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,9 +637,7 @@ def _trigger_event(self, event, *args, **kwargs):
"""Invoke an event handler."""
run_async = kwargs.pop('run_async', False)
if event in self.handlers:
if run_async:
return self.start_background_task(self.handlers[event], *args)
else:
def run_handler():
try:
return self.handlers[event](*args)
except:
Expand All @@ -649,6 +647,11 @@ def _trigger_event(self, event, *args, **kwargs):
# connection
return False

if run_async:
return self.start_background_task(run_handler)
else:
return run_handler()

def _get_socket(self, sid):
"""Return the socket object for a given session."""
try:
Expand Down
6 changes: 2 additions & 4 deletions tests/asyncio/test_asyncio_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -1103,8 +1103,7 @@ def foo_handler(arg):
s = asyncio_server.AsyncServer()
s.on('message', handler=foo_handler)
fut = _run(s._trigger_event('message', 'bar', run_async=True))
with pytest.raises(ZeroDivisionError):
asyncio.get_event_loop().run_until_complete(fut)
asyncio.get_event_loop().run_until_complete(fut)
assert result == ['bar']

def test_trigger_event_coroutine_async_error(self):
Expand All @@ -1117,8 +1116,7 @@ async def foo_handler(arg):
s = asyncio_server.AsyncServer()
s.on('message', handler=foo_handler)
fut = _run(s._trigger_event('message', 'bar', run_async=True))
with pytest.raises(ZeroDivisionError):
asyncio.get_event_loop().run_until_complete(fut)
asyncio.get_event_loop().run_until_complete(fut)
assert result == ['bar']

def test_create_queue(self):
Expand Down

0 comments on commit 2bef6d6

Please sign in to comment.