From f1aa8b26b5ce074724c9fe6b11aa2e9d3dd46486 Mon Sep 17 00:00:00 2001 From: Rico Schrage Date: Thu, 2 Jan 2025 17:22:36 +0100 Subject: [PATCH 1/4] Added handling for cancelled tasks for the callback raise exception --- mango/util/scheduling.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/mango/util/scheduling.py b/mango/util/scheduling.py index 4810643..09005ee 100644 --- a/mango/util/scheduling.py +++ b/mango/util/scheduling.py @@ -25,11 +25,14 @@ def _raise_exceptions(fut: asyncio.Future): 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 Exception: + logger.exception("got exception in scheduled event") + except CancelledError: + pass # if this happens the task has been cancelled by mango @dataclass From 97d582f8afae940243986a5049a2cc2acb7ead4b Mon Sep 17 00:00:00 2001 From: Rico Schrage Date: Thu, 2 Jan 2025 17:48:55 +0100 Subject: [PATCH 2/4] Update scheduling.py --- mango/util/scheduling.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mango/util/scheduling.py b/mango/util/scheduling.py index 09005ee..c7594e7 100644 --- a/mango/util/scheduling.py +++ b/mango/util/scheduling.py @@ -31,7 +31,7 @@ def _raise_exceptions(fut: asyncio.Future): raise fut.exception() except Exception: logger.exception("got exception in scheduled event") - except CancelledError: + except asyncio.CancelledError: pass # if this happens the task has been cancelled by mango From e6a34cba8b11a7b36f7e07242c523427ed63f3b1 Mon Sep 17 00:00:00 2001 From: Rico Schrage Date: Thu, 2 Jan 2025 20:23:46 +0100 Subject: [PATCH 3/4] Formatting. --- mango/util/scheduling.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mango/util/scheduling.py b/mango/util/scheduling.py index c7594e7..2ed9ee7 100644 --- a/mango/util/scheduling.py +++ b/mango/util/scheduling.py @@ -22,7 +22,7 @@ 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 """ try: @@ -32,7 +32,7 @@ def _raise_exceptions(fut: asyncio.Future): except Exception: logger.exception("got exception in scheduled event") except asyncio.CancelledError: - pass # if this happens the task has been cancelled by mango + pass # if this happens the task has been cancelled by mango @dataclass From 41986f66490ae9cbaf84cd8abf8d429ce1462083 Mon Sep 17 00:00:00 2001 From: Rico Schrage Date: Fri, 3 Jan 2025 12:10:43 +0100 Subject: [PATCH 4/4] Adding test for exception handling scheduler. --- mango/util/scheduling.py | 2 +- tests/unit_tests/util/scheduling_test.py | 25 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/mango/util/scheduling.py b/mango/util/scheduling.py index 2ed9ee7..b9a921d 100644 --- a/mango/util/scheduling.py +++ b/mango/util/scheduling.py @@ -29,7 +29,7 @@ def _raise_exceptions(fut: asyncio.Future): if fut.exception() is not None: try: raise fut.exception() - except Exception: + except BaseException: logger.exception("got exception in scheduled event") except asyncio.CancelledError: pass # if this happens the task has been cancelled by mango diff --git a/tests/unit_tests/util/scheduling_test.py b/tests/unit_tests/util/scheduling_test.py index c64baec..bf78c8e 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