Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
3 tasks done
ocanty opened this issue Jun 3, 2020 · 6 comments
Closed
3 tasks done

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

ocanty opened this issue Jun 3, 2020 · 6 comments
Labels
question Question or problem question-migrate

Comments

@ocanty
Copy link

ocanty commented Jun 3, 2020

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

@ocanty ocanty added the question Question or problem label Jun 3, 2020
@ocanty ocanty closed this as completed Jun 17, 2020
@tiangolo
Copy link
Owner

I imagine you solved your problem, so thanks for closing the issue 👍

@nicoinn
Copy link

nicoinn commented Sep 8, 2020

@ocanty Could you consider sharing your solution ? I have exactly the same question...

@cormacp
Copy link

cormacp commented Sep 21, 2020

Likewise, I've the same question, any insight into the best way to do this programatically?

@ChoOo7
Copy link

ChoOo7 commented Sep 23, 2020

I found a very dirty way :

def crash():
try:
crash()
except:
crash()

My app is inside a docker container with auto restart
so.... a fresh container start after

@janheindejong
Copy link

Same... what's the solution here? :-)

@JaDogg
Copy link

JaDogg commented Feb 27, 2021

How about this?

api = FastAPI()


def self_terminate():
    time.sleep(1)
    parent = psutil.Process(psutil.Process(os.getpid()).ppid())
    parent.kill()


@api.post("/kill")
async def kill():
    threading.Thread(target=self_terminate, daemon=True).start()
    return {"success": True}

I'm using psutil to get parent process and kill it. 👍🏽 This sends a signal to parent process. This worked for me in Windows. (Should work in Linux/MacOS but not sure) You can also send parent process a SIGINT to simulate Ctrl+C 😎

@tiangolo tiangolo changed the title [QUESTION] Shutting down the uvicorn server master from a FastAPI worker Shutting down the uvicorn server master from a FastAPI worker Feb 24, 2023
@tiangolo tiangolo reopened this Feb 28, 2023
Repository owner locked and limited conversation to collaborators Feb 28, 2023
@tiangolo tiangolo converted this issue into discussion #7458 Feb 28, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
question Question or problem question-migrate
Projects
None yet
Development

No branches or pull requests

7 participants