From bd2dc8dad63a4e7235dc71ea4e5b860467b79d1f Mon Sep 17 00:00:00 2001 From: Aman Dwivedi <2k22.it.2211600@gmail.com> Date: Tue, 22 Jul 2025 16:16:12 +0530 Subject: [PATCH] Add timeout and error handling in FastAPI uvicorn server --- examples/high_level_api/fastapi_server.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/examples/high_level_api/fastapi_server.py b/examples/high_level_api/fastapi_server.py index ee59767d6..0a0567052 100644 --- a/examples/high_level_api/fastapi_server.py +++ b/examples/high_level_api/fastapi_server.py @@ -24,15 +24,24 @@ To actually see the implementation of the server, see llama_cpp/server/app.py """ - import os import uvicorn - from llama_cpp.server.app import create_app +import asyncio if __name__ == "__main__": app = create_app() - uvicorn.run( - app, host=os.getenv("HOST", "localhost"), port=int(os.getenv("PORT", 8000)) - ) + try: + # Run the server with timeout config (graceful shutdown handling) + uvicorn.run( + app, + host=os.getenv("HOST", "localhost"), + port=int(os.getenv("PORT", 8000)), + timeout_keep_alive=10, # Optional: disconnect inactive clients after 10s + timeout_notify=5 # Optional: timeout for graceful shutdown notification + ) + except asyncio.TimeoutError: + print("⏰ Server startup timed out.") + except Exception as e: + print(f"🚨 An unexpected error occurred: {e}")