Skip to content

Commit b5c8f3f

Browse files
committed
Update cancel_and_await to exit immediately if task is already done
Signed-off-by: Sahas Subramanian <[email protected]>
1 parent 78a1edb commit b5c8f3f

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

src/frequenz/sdk/_internal/asyncio.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@
1313
async def cancel_and_await(task: asyncio.Task[Any]) -> None:
1414
"""Cancel a task and wait for it to finish.
1515
16+
Exits immediately if the task is already done.
17+
1618
The `CancelledError` is suppresed, but any other exception will be propagated.
1719
1820
Args:
1921
task: The task to be cancelled and waited for.
2022
"""
23+
if task.done():
24+
return
2125
task.cancel()
2226
try:
2327
await task

0 commit comments

Comments
 (0)