diff --git a/mango/util/scheduling.py b/mango/util/scheduling.py index 4810643e..b9a921d3 100644 --- a/mango/util/scheduling.py +++ b/mango/util/scheduling.py @@ -22,14 +22,17 @@ def _raise_exceptions(fut: asyncio.Future): """ - Inline function used as a callback to raise exceptions + Inline function used as a callback to raise exceptions. :param fut: The Future object of the task """ - if fut.exception() is not None: - try: - raise fut.exception() - except Exception: - logger.exception("got exception in scheduled event") + try: + if fut.exception() is not None: + try: + raise fut.exception() + except BaseException: + logger.exception("got exception in scheduled event") + except asyncio.CancelledError: + pass # if this happens the task has been cancelled by mango @dataclass diff --git a/tests/unit_tests/util/scheduling_test.py b/tests/unit_tests/util/scheduling_test.py index c64baec4..bf78c8e5 100644 --- a/tests/unit_tests/util/scheduling_test.py +++ b/tests/unit_tests/util/scheduling_test.py @@ -1,5 +1,6 @@ import asyncio import datetime +import logging import time import pytest @@ -501,3 +502,27 @@ def on_stop(fut): # THEN assert len(l) == 2 assert l[1] == 2 + + +class MyException(Exception): + pass + + +@pytest.mark.asyncio +async def test_exception(caplog): + # GIVEN + scheduler = Scheduler() + l = [] + + async def increase_counter(): + raise MyException() + + # WHEN + t = scheduler.schedule_task(PeriodicScheduledTask(increase_counter, 0.2)) + + # THEN + with caplog.at_level(logging.ERROR): + with pytest.raises(MyException): + await asyncio.wait_for(t, timeout=0.3) + + assert "got exception in scheduled event" in caplog.text