Skip to content

Commit eff167b

Browse files
Stop catching BaseException in actor/_run_utils.py::run method
CancellError is the only BaseException that needs to be catched. Signed-off-by: Elzbieta Kotulska <[email protected]>
1 parent 70733ab commit eff167b

File tree

3 files changed

+3
-61
lines changed

3 files changed

+3
-61
lines changed

RELEASE_NOTES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@
1919
- Fixed bug with formulas raising exception when stopped.
2020

2121
- Fix a bug that raised `CancelledError` when actor was started with `frequenz.sdk.actor.run` and stopped.
22+
23+
- Stop catching `BaseException` in `frequenz.sdk.actor.run`. Only `CancelledError` and `Exception` are caught now.

src/frequenz/sdk/actor/_run_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ async def run(*actors: Actor) -> None:
4949
task.result()
5050
except* asyncio.CancelledError:
5151
_logger.info("Actor %s: Cancelled while running.", task.get_name())
52-
except* BaseException: # pylint: disable=broad-exception-caught
52+
except* Exception: # pylint: disable=broad-exception-caught
5353
_logger.exception(
5454
"Actor %s: Raised an exception while running.",
5555
task.get_name(),

tests/actor/test_actor.py

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -78,31 +78,6 @@ async def _run(self) -> None:
7878
print(f"{self} done (should not happen)")
7979

8080

81-
class RaiseBaseExceptionActor(BaseTestActor):
82-
"""A faulty actor that raises a BaseException as soon as it receives a message."""
83-
84-
def __init__(
85-
self,
86-
recv: Receiver[int],
87-
) -> None:
88-
"""Create an instance.
89-
90-
Args:
91-
recv: A channel receiver for int data.
92-
"""
93-
super().__init__(name="test")
94-
self._recv = recv
95-
96-
async def _run(self) -> None:
97-
"""Start the actor and crash upon receiving a message."""
98-
print(f"{self} started")
99-
self.inc_restart_count()
100-
async for _ in self._recv:
101-
print(f"{self} is about to crash")
102-
raise MyBaseException("This is a test")
103-
print(f"{self} done (should not happen)")
104-
105-
10681
ACTOR_INFO = ("frequenz.sdk.actor._actor", 20)
10782
ACTOR_ERROR = ("frequenz.sdk.actor._actor", 40)
10883
RUN_INFO = ("frequenz.sdk.actor._run_utils", 20)
@@ -309,41 +284,6 @@ async def test_does_not_restart_on_normal_exit(
309284
]
310285

311286

312-
async def test_does_not_restart_on_base_exception(
313-
actor_auto_restart_once: None, # pylint: disable=unused-argument
314-
caplog: pytest.LogCaptureFixture,
315-
) -> None:
316-
"""Create a faulty actor and expect it not to restart because it raises a base exception."""
317-
caplog.set_level("DEBUG", logger="frequenz.sdk.actor._actor")
318-
caplog.set_level("DEBUG", logger="frequenz.sdk.actor._run_utils")
319-
320-
channel: Broadcast[int] = Broadcast(name="channel")
321-
322-
actor = RaiseBaseExceptionActor(channel.new_receiver())
323-
324-
async with asyncio.timeout(1.0):
325-
await channel.new_sender().send(1)
326-
# We can't use pytest.raises() here because known BaseExceptions are handled
327-
# specially by pytest.
328-
try:
329-
await run(actor)
330-
except MyBaseException as error:
331-
assert str(error) == "This is a test"
332-
333-
assert BaseTestActor.restart_count == 0
334-
assert caplog.record_tuples == [
335-
(*RUN_INFO, "Starting 1 actor(s)..."),
336-
(*RUN_INFO, "Actor RaiseBaseExceptionActor[test]: Starting..."),
337-
(*ACTOR_INFO, "Actor RaiseBaseExceptionActor[test]: Started."),
338-
(*ACTOR_ERROR, "Actor RaiseBaseExceptionActor[test]: Raised a BaseException."),
339-
(
340-
*RUN_ERROR,
341-
"Actor RaiseBaseExceptionActor[test]: Raised an exception while running.",
342-
),
343-
(*RUN_INFO, "All 1 actor(s) finished."),
344-
]
345-
346-
347287
async def test_does_not_restart_if_cancelled(
348288
actor_auto_restart_once: None, # pylint: disable=unused-argument
349289
caplog: pytest.LogCaptureFixture,

0 commit comments

Comments
 (0)