Skip to content

Fix issue with actor restarting instead of stopping #1223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: v1.x.x
Choose a base branch
from

Conversation

ela-kotulska-frequenz
Copy link
Contributor

@ela-kotulska-frequenz ela-kotulska-frequenz commented May 22, 2025

When an actor threw an exception during cancellation,
it restarted rather than stopping as expected.
Now it stops and raises and just prints exception.

@ela-kotulska-frequenz ela-kotulska-frequenz self-assigned this May 22, 2025
@Copilot Copilot AI review requested due to automatic review settings May 22, 2025 09:47
@ela-kotulska-frequenz ela-kotulska-frequenz added the type:bug Something isn't working label May 22, 2025
@ela-kotulska-frequenz ela-kotulska-frequenz requested a review from a team as a code owner May 22, 2025 09:47
@github-actions github-actions bot added part:docs Affects the documentation part:tests Affects the unit, integration and performance (benchmarks) tests part:actor Affects an actor ot the actors utilities (decorator, etc.) labels May 22, 2025
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes an issue where actors would restart instead of stopping when exceptions occurred during cancellation. Key changes include the introduction of a new actor subclass that raises an exception during cancellation, updates to the actor’s internal logic to properly handle cancelled state, and a new test that verifies the correct stopping behavior.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
tests/actor/test_actor.py New actor subclass and test to check actor stops on cancellation.
src/frequenz/sdk/actor/_actor.py Updates in actor startup and exception handling during cancellation.
RELEASE_NOTES.md Updated release notes to describe the bug fix.

When an actor throws an exception during cancellation,
it restarts rather than stopping as expected.

Signed-off-by: Elzbieta Kotulska <elzbieta.kotulska@frequenz.com>
When an actor threw an exception during cancellation,
it restarted rather than stopping as expected.
Now it stops and raises an unhandled exception.

Signed-off-by: Elzbieta Kotulska <elzbieta.kotulska@frequenz.com>
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a bug where an actor restarted instead of stopping when an exception was raised during cancellation. Key changes include the addition of a new test case in tests/actor/test_actor.py, an adjustment in cancellation and exception logging logic in src/frequenz/sdk/actor/_actor.py, and updated release notes.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
tests/actor/test_actor.py Added a new test and a specialized actor (RaiseExceptionOnCancelActor) to validate proper stop behavior upon cancellation errors.
src/frequenz/sdk/actor/_actor.py Introduced a cancellation flag, refined exception handling in the run loop, and updated the cancel method to prevent actor restart.
RELEASE_NOTES.md Updated the bug fix section to reflect that actors now stop and surface the unhandled exception during cancellation.

@@ -94,6 +114,12 @@ async def _run_loop(self) -> None:
_logger.info("Actor %s: Cancelled.", self)
raise
except Exception: # pylint: disable=broad-except
if self._is_cancelled:
Copy link
Preview

Copilot AI May 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exception handling branch in the run loop logs the error and breaks when the actor is cancelled. It would be beneficial to add a comment explaining that this behavior is intentional to prevent restarts, which will help future maintainers understand the rationale.

Copilot uses AI. Check for mistakes.

Copy link
Contributor

@llucax llucax left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks sketchy to me, it looks like supporting a random exception to be raised during cancellation is just a fix for a but we just created by raising this exception.

Comment on lines +67 to +72

if self._is_cancelled:
raise RuntimeError(
"Actor %s: was canceled and can't be re-run."
"Please create another actor instance.",
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't go for this at the actor level yet. We have many actors that assume they can be restarted and count on this. I would like to make a big overhaul of how we handle tasks and actors, I would avoid making any breaking changes until then as adapting all existing code to any breaking changes here will be very costly.

name: The name of this background service.
"""
super().__init__(name=name)
self._is_cancelled = False
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we save the main actor task instead and use self._main_task.cancelled() as a single source of truth?

Comment on lines +77 to +78
except asyncio.CancelledError as exc:
raise RuntimeError("Actor should stop.") from exc
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we need to support this, this feels wrong, if a task is cancelled, it should not raise some other exception. Cancellation should always be supported and do nothing if it was already cancelled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
part:actor Affects an actor ot the actors utilities (decorator, etc.) part:docs Affects the documentation part:tests Affects the unit, integration and performance (benchmarks) tests type:bug Something isn't working
Projects
Status: To do
Development

Successfully merging this pull request may close these issues.

2 participants