-
Couldn't load subscription status.
- Fork 20
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
Fix issue with actor restarting instead of stopping #1223
Conversation
There was a problem hiding this 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 <[email protected]>
51c6e89 to
1045eea
Compare
There was a problem hiding this 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. |
There was a problem hiding this 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.
| name: The name of this background service. | ||
| """ | ||
| super().__init__(name=name) | ||
| self._is_cancelled = False |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checked and in that case self._main_task.cancelled() returns False, because task was not cancelled successfully.
| except asyncio.CancelledError as exc: | ||
| raise RuntimeError("Actor should stop.") from exc |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if a task is cancelled, it should not raise some other exception.
Yes but bugs happens and anything can raise exception. The problem is in this case we restart the actor and await actor.stop() never ends... :/
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 <[email protected]>
1045eea to
02e30d7
Compare
When an actor threw an exception during cancellation,
it restarted rather than stopping as expected.
Now it stops and raises and just prints exception.