This commit is contained in:
Raphaël MANSUY 2025-12-04 19:18:40 +08:00
parent 8ebd98ae8e
commit dd73dd3c9f
2 changed files with 17 additions and 8 deletions

View file

@ -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)

View file

@ -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 = (