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

test graceful shutdown with idle connections #3969

Merged
merged 9 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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: 9 additions & 0 deletions .changesets/fix_geal_idle_connections.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### Fix router hang when opening the explorer, prometheus or health check page ([Issue #3941](https://github.com/apollographql/router/issues/3941))

The Router did not gracefully shutdown when an idle connections are made by a client, and would instead hang. In particular, web browsers make such connection in anticipation of future traffic.

This is now fixed, and the Router will now gracefully shut down in a timely fashion.

---

By [@Geal](https://github.com/Geal) in https://github.com/apollographql/router/pull/3969
59 changes: 57 additions & 2 deletions apollo-router/src/axum_factory/listeners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

use std::collections::HashMap;
use std::collections::HashSet;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::time::Duration;
use std::time::Instant;
Expand All @@ -16,6 +18,7 @@ use multimap::MultiMap;
use tokio::net::UnixListener;
use tokio::sync::mpsc;
use tokio::sync::Notify;
use tower_service::Service;

use crate::configuration::Configuration;
use crate::http_server_factory::Listener;
Expand Down Expand Up @@ -229,6 +232,9 @@ pub(super) fn serve_router_on_listen_addr(

match res {
NetworkStream::Tcp(stream) => {
let received_first_request = Arc::new(AtomicBool::new(false));
let app = IdleConnectionChecker::new(received_first_request.clone(), app);

stream
.set_nodelay(true)
.expect(
Expand All @@ -251,7 +257,12 @@ pub(super) fn serve_router_on_listen_addr(
let c = connection.as_mut();
c.graceful_shutdown();

let _= connection.await;
// if the connection was idle and we never received the first request,
// hyper's graceful shutdown would wait indefinitely, so instead we
// close the connection right away
if received_first_request.load(Ordering::Relaxed) {
let _= connection.await;
}
}
}
}
Expand All @@ -278,6 +289,9 @@ pub(super) fn serve_router_on_listen_addr(
}
},
NetworkStream::Tls(stream) => {
let received_first_request = Arc::new(AtomicBool::new(false));
let app = IdleConnectionChecker::new(received_first_request.clone(), app);

stream.get_ref().0
.set_nodelay(true)
.expect(
Expand Down Expand Up @@ -305,7 +319,12 @@ pub(super) fn serve_router_on_listen_addr(
let c = connection.as_mut();
c.graceful_shutdown();

let _= connection.await;
// if the connection was idle and we never received the first request,
// hyper's graceful shutdown would wait indefinitely, so instead we
// close the connection right away
if received_first_request.load(Ordering::Relaxed) {
let _= connection.await;
}
}
}
}
Expand Down Expand Up @@ -405,6 +424,42 @@ pub(super) fn serve_router_on_listen_addr(
(server, shutdown_sender)
}

struct IdleConnectionChecker<S> {
received_request: Arc<AtomicBool>,
inner: S,
}

impl<S> IdleConnectionChecker<S> {
fn new(b: Arc<AtomicBool>, service: S) -> Self {
IdleConnectionChecker {
received_request: b,
inner: service,
}
}
}
impl<S, B> Service<http::Request<B>> for IdleConnectionChecker<S>
where
S: Service<http::Request<B>>,
{
type Response = <S as Service<http::Request<B>>>::Response;

type Error = <S as Service<http::Request<B>>>::Error;

type Future = <S as Service<http::Request<B>>>::Future;

fn poll_ready(
&mut self,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<std::result::Result<(), Self::Error>> {
self.inner.poll_ready(cx)
}

fn call(&mut self, req: http::Request<B>) -> Self::Future {
self.received_request.store(true, Ordering::Relaxed);
self.inner.call(req)
}
}

#[cfg(test)]
mod tests {
use std::net::SocketAddr;
Expand Down
16 changes: 16 additions & 0 deletions apollo-router/tests/lifecycle_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,22 @@ async fn test_reload_via_sighup() -> Result<(), BoxError> {
Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn test_shutdown_with_idle_connection() -> Result<(), BoxError> {
let mut router = IntegrationTest::builder()
.config(HAPPY_CONFIG)
.build()
.await;
router.start().await;
router.assert_started().await;
let _conn = std::net::TcpStream::connect("127.0.0.1:4000").unwrap();
router.execute_default_query().await;
tokio::time::timeout(Duration::from_millis(100), router.graceful_shutdown())
.await
.unwrap();
Ok(())
}

async fn command_output(command: &mut Command) -> String {
let output = command.output().await.unwrap();
let success = output.status.success();
Expand Down