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(