From 651682829a81768e57ecaed149a8b16de8ca9b8a Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Sun, 28 Aug 2022 12:05:44 +0200 Subject: [PATCH] Tweak worker shutdown --- app/utils/workers.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/utils/workers.py b/app/utils/workers.py index 9dc0065..3d1a859 100644 --- a/app/utils/workers.py +++ b/app/utils/workers.py @@ -54,12 +54,17 @@ class Worker(Generic[T]): {task, stop_task}, return_when=asyncio.FIRST_COMPLETED ) logger.info(f"Waiting for tasks to finish {done=}/{pending=}") - await asyncio.sleep(5) tasks = [t for t in asyncio.all_tasks() if t is not asyncio.current_task()] logger.info(f"Cancelling {len(tasks)} tasks") [task.cancel() for task in tasks] - await asyncio.gather(*tasks, return_exceptions=True) + try: + await asyncio.wait_for( + asyncio.gather(*tasks, return_exceptions=True), + timeout=15, + ) + except asyncio.TimeoutError: + logger.info("Tasks failed to cancel") logger.info("stopping loop")