Skip to content

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

@suiseriff

Description

@suiseriff

Initial Checks

  • I confirm this was discussed, and the maintainers suggest I open an issue.
  • 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions