Skip to content
Open
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
40 changes: 36 additions & 4 deletions tonic/src/transport/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -930,10 +930,12 @@ fn serve_connection<B, IO, S, E>(

let mut conn = pin!(builder.serve_connection(hyper_io, hyper_svc));

let mut connection_timeout = pin!(connection_timeout_future(
max_connection_age,
max_connection_age_grace,
));
let mut connection_timeout = pin!(Fuse {
inner: Some(connection_timeout_future(
max_connection_age,
max_connection_age_grace,
))
});

loop {
tokio::select! {
Expand Down Expand Up @@ -1328,6 +1330,36 @@ mod tests {
assert!(matches!(action, TimeoutAction::ForcefulShutdown));
}

#[tokio::test(start_paused = true)]
async fn test_connection_timeout_fused_after_graceful_shutdown() {
// Reproduce #2522: connection_timeout polled after GracefulShutdown
let mut future = pin!(Fuse {
inner: Some(connection_timeout_future(
Some(Duration::from_secs(10)),
None,
))
});

// First poll: should return GracefulShutdown after 10s
let action = tokio::select! {
action = &mut future => action,
_ = tokio::time::sleep(Duration::from_secs(11)) => {
panic!("timeout future should complete after max_connection_age");
}
};
assert!(matches!(action, TimeoutAction::GracefulShutdown));

// Second poll: Fuse should return Pending, not panic
tokio::select! {
_ = &mut future => {
panic!("fused future should not complete again");
}
_ = tokio::time::sleep(Duration::from_secs(1)) => {
// OK: future is fused, returns Pending
}
}
}

#[test]
fn server_tcp_defaults() {
const EXAMPLE_TCP_KEEPALIVE: Duration = Duration::from_secs(10);
Expand Down
Loading