uvicorn icon indicating copy to clipboard operation
uvicorn copied to clipboard

If the lifetime is frozen, the app will not respond to the Ctrl + C

Open suiseriff opened this issue 2 months ago • 0 comments

Initial Checks

  • [x] I confirm this was discussed, and the maintainers suggest I open an issue.
  • [x] I'm aware that if I created this issue without a discussion, it may be closed without a response.

Discussion Link

https://github.com/Kludex/uvicorn/discussions/2716

Description

When the application hangs during its lifespan, it becomes completely unresponsive to the Ctrl+C command (SIGINT), preventing a graceful shutdown. This forces users to resort to using the kill -9 command.

Example Code

import asyncio
from collections.abc import AsyncIterator
from contextlib import asynccontextmanager

import uvicorn
from fastapi import FastAPI

@asynccontextmanager
async def lifespan(_: FastAPI) -> AsyncIterator[None]:
    await asyncio.sleep(5000)  # Simulates hanging operation
    yield

def create_app() -> FastAPI:
    return FastAPI(lifespan=lifespan)

if __name__ == "__main__":
    uvicorn.run(create_app(), host="0.0.0.0", port=8000)

Python, Uvicorn & OS Version

Running uvicorn 0.38.0 with CPython 3.12.11 on Linux
fastapi: 0.121.0
OS: Arch Linux x86_64
Kernel: 6.17.7-arch1-1

suiseriff avatar Nov 08 '25 18:11 suiseriff