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

fix: possible race condition during graceful shutdown #780

Merged
merged 1 commit into from
Mar 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions poem/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ where
let server_graceful_shutdown_token = server_graceful_shutdown_token.clone();

tokio::spawn(async move {
let serve_connection = serve_connection(socket, local_addr, remote_addr, scheme, ep, server_graceful_shutdown_token, idle_timeout);
let serve_connection = serve_connection(socket, local_addr, remote_addr, scheme, ep, server_graceful_shutdown_token.clone(), idle_timeout);

if timeout.is_some() {
tokio::select! {
Expand All @@ -178,8 +178,11 @@ where
}

if alive_connections.fetch_sub(1, Ordering::Acquire) == 1 {
// We have to notify only if there is a registered waiter on shutdown
notify.notify_waiters();
// notify only if shutdown is initiated, to prevent notification when server is active.
// It's a valid state to have 0 alive connections when server is not shutting down.
if server_graceful_shutdown_token.is_cancelled() {
notify.notify_one();
}
}
});
}
Expand Down
Loading