Merge pull request #162 from 021gink/graceful-uvicorn-shutdown

Gracefully shutdown uvicorn.Server on task cancellation in run()
This commit is contained in:
Tim Jaeryang Baek 2025-06-06 20:31:24 +04:00 committed by GitHub
commit 663f7312bb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,6 +2,7 @@ import json
import os
import logging
import socket
import asyncio
from contextlib import AsyncExitStack, asynccontextmanager
from typing import Optional
@ -341,4 +342,9 @@ async def run(
)
server = uvicorn.Server(config)
await server.serve()
try:
await server.serve()
except asyncio.CancelledError:
server.should_exit = True
await server.shutdown()
raise