-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 Shutdown ignores Websocket handler task #3003
Comments
Bellow what |
Sorry, I thought it was more obvious what I meant. I am simply talking about the end of main in a typical axum application. The last command is typically serving the axum app, after which the tokio runtime is immediately shut down. The purpose of a sleep would simply be a quick hack for keeping the tokio runtime alive a bit longer. Then it is easy to see that websocket connections are kept open for too long. I have created a quick example by combining the websocket and graceful-shutdown examples. It demonstrates quite well that there is no connection whatsoever between the graceful shutdown future and the websocket handlers. |
I have now solved/worked around this issue by tracking all websocket handlers using tokio_util::task::TaskTracker along with CancellationTokens. I still think axum should track the future spawned inside on_upgrade |
Thanks, I see what the issue is now. When we upgrade the connection, it seems the whole stack considers the original connection as closed but its io is transferred and we don't track that anymore. Propagating some kind of a channel there so that we keep track of it for the purpose of graceful shutdown should be easy enough. That said, I don't think that's where the story ends because what I think will happen is all middlewares will also run as if the request has finished (which it kind of did since we produced a response, but also not quite). This could break for example layers trying to limit the number of concurrent connections or maybe some tracing layers. |
Bug Report
Version
axum 0.7.4
Platform
Crates
axum
Description
I have the following scenario:
Arc
) to the application stateThe concrete reason the state needs to be dropped is that it may contain a file handle that is exclusive and that I want to access after shutting down.
The problem:
The task spawned in
ws.on_upgrade
is not connected to the graceful shutdown future at all. I can listen to a globalCancellationToken
and quit the connection myself, but that still does not guarantee me that the connection is closed when the graceful shutdown future completes.If I don't immediately quit the application after the server quits, the websocket connection happily continues running. Try placing a
sleep
below theaxum::serve
.I would expect websockets to behave the same as SSE, i.e. blocking graceful shutdown until completion (#2673).
The text was updated successfully, but these errors were encountered: