Skip to content

Shutting down the uvicorn server master from a FastAPI worker #1509

@ocanty

Description

@ocanty

First check

  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.

Description

I want to kill the entire uvicorn server whenever one of my workers encounters an issue it cannot resolve.

I currently run fastapi inside the bundled Docker container recommended in the documentation, when my worker cannot connect to an upstream api because it is down/unavailable, I can call sys.quit(-1) to kill the worker with an error code, but this only causes uvicorn to restart the worker.

Is there any interface to kill the uvicorn master server and return an error code so that it kills my Docker container? The only alternative I can see is using health checks instead of killing the container

image

One solution I have found would be using os.kill to kill pid 1/sending a SIGTERM but I'm not sure if ASGI may have a procedure/interface for this rather than bluntly signalling the process. I have searched the uvicorn and Starlette docs and found nothing

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions