Skip to content
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

Graceful server shutdown #24

Open
genericmoniker opened this issue Mar 13, 2021 · 0 comments
Open

Graceful server shutdown #24

genericmoniker opened this issue Mar 13, 2021 · 0 comments

Comments

@genericmoniker
Copy link
Owner

When running the server in a terminal and sending Ctrl+C, it logs:

2021-03-13 09:11:51,204 INFO:     uvicorn.error -> Shutting down
2021-03-13 09:11:51,312 INFO:     uvicorn.error -> Waiting for connections to close. (CTRL+C to force quit)

I had hoped to have clean shutdown using hooks to cancel active tasks and return from any SSE event handlers:

    application = Starlette(
        debug=True,
        routes=routes,
        on_startup=[plugins.startup],
        on_shutdown=[plugins.shutdown, event_bus.shutdown],
    )

But per the documentation:

The shutdown handlers will run once all connections have been closed, and any in-process background tasks have completed.

So it is bad timing. The on_shutdown functions won't get called until the things they are trying to do are already done. What I really need is a hypothetical before_shutdown hook that gets called when starting the shutdown process. There is a feature request for that in Starlette.

This SO question is the same problem, but with websockets: https://stackoverflow.com/questions/58133694/graceful-shutdown-of-uvicorn-starlette-app-with-websockets

The best answer so far has monkey-patching uvicorn's signal handling, which is pragmatic but not very elegant.

genericmoniker added a commit that referenced this issue Mar 21, 2021
But not really working because of #24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant