Skip to content

Commit 70733ab

Browse files
Stop raising exception when actor is stopped.
BacgroundService raises BaseExceptionGroup. We can check if task was cancelled by checking if there are CancelledError in the list of exception. Signed-off-by: Elzbieta Kotulska <[email protected]>
1 parent 855a181 commit 70733ab

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

RELEASE_NOTES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@
1717
## Bug Fixes
1818

1919
- Fixed bug with formulas raising exception when stopped.
20+
21+
- Fix a bug that raised `CancelledError` when actor was started with `frequenz.sdk.actor.run` and stopped.

src/frequenz/sdk/actor/_run_utils.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,21 @@ async def run(*actors: Actor) -> None:
3838
done_tasks, pending_tasks = await asyncio.wait(
3939
pending_tasks, return_when=asyncio.FIRST_COMPLETED
4040
)
41-
4241
# This should always be only one task, but we handle many for extra safety
4342
for task in done_tasks:
44-
# Cancellation needs to be checked first, otherwise the other methods
45-
# could raise a CancelledError
46-
if task.cancelled():
43+
# BackgroundService returns a BaseExceptionGroup containing multiple
44+
# exceptions. The 'task.result()' statement raises these exceptions,
45+
# and 'except*' is used to handle them as a group. If the task raises
46+
# multiple different exceptions, 'except*' will be invoked multiple times,
47+
# once for each exception group.
48+
try:
49+
task.result()
50+
except* asyncio.CancelledError:
4751
_logger.info("Actor %s: Cancelled while running.", task.get_name())
48-
elif exception := task.exception():
49-
_logger.error(
52+
except* BaseException: # pylint: disable=broad-exception-caught
53+
_logger.exception(
5054
"Actor %s: Raised an exception while running.",
5155
task.get_name(),
52-
exc_info=exception,
5356
)
5457
else:
5558
_logger.info("Actor %s: Finished normally.", task.get_name())

tests/actor/test_actor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,6 @@ async def cancel_actor() -> None:
390390
(*RUN_INFO, "Actor EchoActor[EchoActor]: Starting..."),
391391
(*ACTOR_INFO, "Actor EchoActor[EchoActor]: Started."),
392392
(*ACTOR_INFO, "Actor EchoActor[EchoActor]: Cancelled."),
393-
(*RUN_ERROR, "Actor EchoActor[EchoActor]: Raised an exception while running."),
393+
(*RUN_INFO, "Actor EchoActor[EchoActor]: Cancelled while running."),
394394
(*RUN_INFO, "All 1 actor(s) finished."),
395395
]

0 commit comments

Comments
 (0)