diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index ab7664401..02a8a5c72 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -17,3 +17,7 @@ ## Bug Fixes - Fixed bug with formulas raising exception when stopped. + +- Fix a bug that raised `CancelledError` when actor was started with `frequenz.sdk.actor.run` and stopped. + +- Stop catching `BaseException` in `frequenz.sdk.actor.run`. Only `CancelledError` and `Exception` are caught now. \ No newline at end of file diff --git a/src/frequenz/sdk/actor/_run_utils.py b/src/frequenz/sdk/actor/_run_utils.py index 35d85fd54..f47163b7a 100644 --- a/src/frequenz/sdk/actor/_run_utils.py +++ b/src/frequenz/sdk/actor/_run_utils.py @@ -38,18 +38,21 @@ async def run(*actors: Actor) -> None: done_tasks, pending_tasks = await asyncio.wait( pending_tasks, return_when=asyncio.FIRST_COMPLETED ) - # This should always be only one task, but we handle many for extra safety for task in done_tasks: - # Cancellation needs to be checked first, otherwise the other methods - # could raise a CancelledError - if task.cancelled(): + # BackgroundService returns a BaseExceptionGroup containing multiple + # exceptions. The 'task.result()' statement raises these exceptions, + # and 'except*' is used to handle them as a group. If the task raises + # multiple different exceptions, 'except*' will be invoked multiple times, + # once for each exception group. + try: + task.result() + except* asyncio.CancelledError: _logger.info("Actor %s: Cancelled while running.", task.get_name()) - elif exception := task.exception(): - _logger.error( + except* Exception: # pylint: disable=broad-exception-caught + _logger.exception( "Actor %s: Raised an exception while running.", task.get_name(), - exc_info=exception, ) else: _logger.info("Actor %s: Finished normally.", task.get_name())