From dd73dd3c9f123b5a37e5508df66cd42251829fab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20MANSUY?= Date: Thu, 4 Dec 2025 19:18:40 +0800 Subject: [PATCH] cherry-pick 816feefd --- lightrag/api/gunicorn_config.py | 17 +++++++++++++++-- lightrag/api/lightrag_server.py | 8 ++------ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/lightrag/api/gunicorn_config.py b/lightrag/api/gunicorn_config.py index aa6886f1..65a68d90 100644 --- a/lightrag/api/gunicorn_config.py +++ b/lightrag/api/gunicorn_config.py @@ -169,8 +169,21 @@ def worker_exit(server, worker): """ Executed when a worker is about to exit. - This is called for each worker process when it exits. We should only - clean up worker-local resources here, NOT the shared Manager. + NOTE: When using UvicornWorker (worker_class = "uvicorn.workers.UvicornWorker"), + this hook may NOT be called reliably. UvicornWorker has its own lifecycle + management that prioritizes ASGI lifespan shutdown events. + + The primary cleanup mechanism is handled by: + 1. FastAPI lifespan context manager with GUNICORN_CMD_ARGS check (in lightrag_server.py) + - Workers skip cleanup when GUNICORN_CMD_ARGS is set + 2. on_exit() hook for main process cleanup + + This function serves as a defensive fallback for: + - Non-UvicornWorker scenarios + - Future Gunicorn/Uvicorn behavior changes + - Additional safety layer + + When called, we should only clean up worker-local resources, NOT the shared Manager. The Manager should only be shut down by the main process in on_exit(). """ print("=" * 80) diff --git a/lightrag/api/lightrag_server.py b/lightrag/api/lightrag_server.py index ec8b2f45..47513b77 100644 --- a/lightrag/api/lightrag_server.py +++ b/lightrag/api/lightrag_server.py @@ -328,14 +328,10 @@ def create_app(args): # In Gunicorn mode with preload_app=True, cleanup is handled by worker_exit/on_exit hooks # Only perform cleanup in Uvicorn single-process mode - if "LIGHTRAG_GUNICORN_MODE" not in os.environ: + if "GUNICORN_CMD_ARGS" not in os.environ: + # Clean up shared data - logger.debug("Unvicorn Mode: finalize shared storage...") finalize_share_data() - else: - logger.debug( - "Gunicorn Mode: don not finalize shared storage in worker process" - ) # Initialize FastAPI base_description = (