From f06a6b5334f792725593a3eed852015dc24cf119 Mon Sep 17 00:00:00 2001 From: "seer-by-sentry[bot]" <157164994+seer-by-sentry[bot]@users.noreply.github.com> Date: Thu, 17 Jul 2025 11:37:42 +0000 Subject: [PATCH] feat: Ensure background task cancellation in `startup_event` --- src/server/server_utils.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/server/server_utils.py b/src/server/server_utils.py index b0371661..80c31bec 100644 --- a/src/server/server_utils.py +++ b/src/server/server_utils.py @@ -61,11 +61,12 @@ async def lifespan(_: FastAPI) -> AsyncGenerator[None, None]: """ task = asyncio.create_task(_remove_old_repositories()) - yield # app runs while the background task is alive - - task.cancel() # ask the worker to stop - with suppress(asyncio.CancelledError): - await task # swallow the cancellation signal + try: + yield # app runs while the background task is alive + finally: + task.cancel() # ask the worker to stop + with suppress(asyncio.CancelledError): + await task # swallow the cancellation signal async def _remove_old_repositories(