Fix UnboundLocalError in health(), switch from on_event to lifespan

This commit is contained in:
2026-04-12 19:41:08 +00:00
parent 774964a4db
commit 7ecbac2dc0

View File

@@ -31,19 +31,19 @@ LISTEN_PORT = int(os.environ.get("MIDDLEWARE_PORT", "8002"))
# Extend this set as more incompatibilities are discovered. # Extend this set as more incompatibilities are discovered.
STRIP_PARAMS = {"logprobs", "top_logprobs"} STRIP_PARAMS = {"logprobs", "top_logprobs"}
app = FastAPI()
client: httpx.AsyncClient | None = None client: httpx.AsyncClient | None = None
_sglang_ready = False _sglang_ready = False
@app.on_event("startup") async def _lifespan(app_instance):
async def startup():
global client global client
client = httpx.AsyncClient( client = httpx.AsyncClient(
timeout=httpx.Timeout(300.0, connect=10.0), timeout=httpx.Timeout(300.0, connect=10.0),
) )
# Background task: wait for SGLang to become available # Background task: wait for SGLang to become available
asyncio.create_task(_wait_for_sglang()) asyncio.create_task(_wait_for_sglang())
yield
await client.aclose()
async def _wait_for_sglang(): async def _wait_for_sglang():
@@ -64,14 +64,13 @@ async def _wait_for_sglang():
await asyncio.sleep(2) await asyncio.sleep(2)
@app.on_event("shutdown") app = FastAPI(lifespan=_lifespan)
async def shutdown():
await client.aclose()
@app.get("/health") @app.get("/health")
async def health(): async def health():
"""Health check — haproxy polls this. Returns 200 only if SGLang is up.""" """Health check — haproxy polls this. Returns 200 only if SGLang is up."""
global _sglang_ready
if not _sglang_ready: if not _sglang_ready:
return Response(content="SGLang not ready", status_code=503) return Response(content="SGLang not ready", status_code=503)
try: try: