Skip to content

Commit 0836138

Browse files
Fix infinite loop in actor decorator for finished actors
Previously, the actor decorator was causing an infinite loop for actors that had finished normally (without exceptions) even when the restart_limit attribute was set to zero. This was particularly problematic in test cases where the restart_limit attribute was intended to limit the number of times an actor can be restarted. To address this issue, the patch modified the code to consider the restart_limit attribute in cases where exceptions different than CancelledError are raised or when an actor finishes normally. This ensures that the actor decorator does not cause an infinite loop for finished actors, and the restart_limit attribute works as expected. Signed-off-by: Daniel Zullo <[email protected]>
1 parent 94b031e commit 0836138

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/frequenz/sdk/actor/_decorator.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -201,23 +201,23 @@ async def _start_actor(self) -> None:
201201
"""
202202
logger.debug("Starting actor: %s", cls.__name__)
203203
number_of_restarts = 0
204-
while True:
204+
while (
205+
self.restart_limit is None or number_of_restarts <= self.restart_limit
206+
):
207+
if number_of_restarts > 0:
208+
logger.info("Restarting actor: %s", cls.__name__)
209+
205210
try:
206211
await super().run()
207212
except asyncio.CancelledError:
208213
logger.debug("Cancelling actor: %s", cls.__name__)
209214
raise
210215
except Exception: # pylint: disable=broad-except
211216
logger.exception("Actor (%s) crashed", cls.__name__)
212-
if (
213-
self.restart_limit is None
214-
or number_of_restarts < self.restart_limit
215-
):
216-
number_of_restarts += 1
217-
logger.info("Restarting actor: %s", cls.__name__)
218-
else:
219-
logger.info("Shutting down actor: %s", cls.__name__)
220-
break
217+
finally:
218+
number_of_restarts += 1
219+
220+
logger.info("Shutting down actor: %s", cls.__name__)
221221

222222
async def _stop(self) -> None:
223223
"""Stop an running actor."""

0 commit comments

Comments
 (0)