Skip to content

Commit 4162a75

Browse files
llucaxMarenz
authored andcommitted
Make run_forever() more resilient when there is no event loop
We detect if the asyncio event loop is not running and log an error message and exit instead of retrying. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 13b3769 commit 4162a75

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/frequenz/sdk/_internal/_asyncio.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,20 @@ async def run_forever(
4646
while True:
4747
try:
4848
await async_callable()
49+
except RuntimeError as exc:
50+
if "no running event loop" in str(exc):
51+
_logger.exception(
52+
"Something went wrong, no running event loop, skipping execution of %s",
53+
async_callable.__name__,
54+
)
55+
return
4956
except Exception: # pylint: disable=broad-except
57+
if not asyncio.get_event_loop().is_running():
58+
_logger.exception(
59+
"Something went wrong, no running event loop, skipping execution of %s",
60+
async_callable.__name__,
61+
)
62+
return
5063
_logger.exception("Restarting after exception")
5164
await asyncio.sleep(interval_s)
5265

0 commit comments

Comments
 (0)