From 9043f7863cb1b0c4febc18ab9db9ad1ee22c87c7 Mon Sep 17 00:00:00 2001 From: "Yang, Bo" Date: Thu, 2 May 2024 14:33:58 -0700 Subject: [PATCH] [BugFix] Prevent the task of `_force_log` from being garbage collected The task returned by `asyncio.create_task` should not be discarded. See https://github.com/python/cpython/issues/88831 --- vllm/entrypoints/openai/api_server.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/vllm/entrypoints/openai/api_server.py b/vllm/entrypoints/openai/api_server.py index 8b3c5ea9de9c0..947d2db0f4b15 100644 --- a/vllm/entrypoints/openai/api_server.py +++ b/vllm/entrypoints/openai/api_server.py @@ -33,6 +33,7 @@ openai_serving_completion: OpenAIServingCompletion logger = init_logger(__name__) +_running_tasks = set() @asynccontextmanager async def lifespan(app: fastapi.FastAPI): @@ -43,7 +44,9 @@ async def _force_log(): await engine.do_log_stats() if not engine_args.disable_log_stats: - asyncio.create_task(_force_log()) + task = asyncio.create_task(_force_log()) + _running_tasks.add(task) + task.add_done_callback(_running_tasks.remove) yield