Skip to content

Commit 97b3e25

Browse files
bmuddhaDodecahedr0x
authored andcommitted
fix: remove graceful http connection shutdown (#773)
1 parent 7c3e8b6 commit 97b3e25

File tree

3 files changed

+24
-24
lines changed

3 files changed

+24
-24
lines changed

magicblock-aperture/src/server/http/mod.rs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use hyper_util::{
66
rt::{TokioExecutor, TokioIo},
77
server::conn,
88
};
9+
use log::info;
910
use magicblock_core::link::DispatchEndpoints;
1011
use tokio::{
1112
net::{TcpListener, TcpStream},
@@ -74,7 +75,10 @@ impl HttpServer {
7475
// Accept a new incoming connection.
7576
Ok((stream, _)) = self.socket.accept() => self.handle(stream),
7677
// Or, break the loop if the cancellation token is triggered.
77-
_ = self.cancel.cancelled() => break,
78+
_ = self.cancel.cancelled() => {
79+
info!("HTTP server shutdown signal has been received");
80+
break
81+
}
7882
}
7983
}
8084

@@ -84,6 +88,7 @@ impl HttpServer {
8488
drop(self.shutdown);
8589
// Wait for the shutdown signal, which fires when all connections are closed.
8690
let _ = self.shutdown_rx.await;
91+
info!("HTTP server has shutdown");
8792
}
8893

8994
/// Spawns a new task to handle a single incoming TCP connection.
@@ -104,23 +109,13 @@ impl HttpServer {
104109
let builder = conn::auto::Builder::new(TokioExecutor::new());
105110
let connection = builder.serve_connection(io, handler);
106111
tokio::pin!(connection);
107-
let mut terminating = false;
108-
109112
// This loop manages the connection's lifecycle.
110-
loop {
111-
tokio::select! {
112-
// Poll the connection itself. This branch
113-
// completes when the client disconnects.
114-
_ = &mut connection => {
115-
break;
116-
}
117-
// If the cancellation token is triggered, initiate a graceful shutdown
118-
// of the Hyper connection.
119-
_ = cancel.cancelled(), if !terminating => {
120-
connection.as_mut().graceful_shutdown();
121-
terminating = true;
122-
}
123-
}
113+
tokio::select! {
114+
// Poll the connection itself. This branch
115+
// completes when the client disconnects.
116+
_ = connection => {},
117+
// If the cancellation token is triggered, force terminate the connection
118+
_ = cancel.cancelled() => {},
124119
}
125120
// Drop the shutdown handle for this connection, signaling
126121
// that one fewer outstanding connection is active.

magicblock-aperture/src/server/websocket/connection.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,13 @@ impl ConnectionHandler {
102102

103103
loop {
104104
tokio::select! {
105-
// Prioritize reading frames from the client.
105+
// Prioritize fast system shutdown
106106
biased;
107107

108+
// 0. We force shutdown the connection, without close frame
109+
_ = self.cancel.cancelled() => break,
110+
111+
108112
// 1. Handle an incoming frame from the client's WebSocket.
109113
Ok(frame) = self.ws.read_frame() => {
110114
// Record inbound client activity
@@ -163,10 +167,7 @@ impl ConnectionHandler {
163167
}
164168
}
165169

166-
// 4. Handle the global server shutdown signal.
167-
_ = self.cancel.cancelled() => break,
168-
169-
// 5. Run cleanup logic for this connection (e.g., an expiring sub).
170+
// 4. Run cleanup logic for this connection (e.g., an expiring sub).
170171
_ = self.dispatcher.cleanup() => {}
171172

172173
else => {

magicblock-aperture/src/server/websocket/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use hyper::{
1010
Request, Response,
1111
};
1212
use hyper_util::rt::TokioIo;
13-
use log::warn;
13+
use log::{info, warn};
1414
use tokio::{
1515
net::{TcpListener, TcpStream},
1616
sync::oneshot::Receiver,
@@ -94,13 +94,17 @@ impl WebsocketServer {
9494
self.handle(stream);
9595
},
9696
// The server shutdown signal has been received.
97-
_ = self.state.cancel.cancelled() => break,
97+
_ = self.state.cancel.cancelled() => {
98+
info!("Websocket server shutdown signal has been received");
99+
break
100+
}
98101
}
99102
}
100103
// Drop the main `ConnectionState` which holds the original `Shutdown` handle.
101104
drop(self.state);
102105
// Wait for all spawned connection tasks to finish.
103106
let _ = self.shutdown.await;
107+
info!("Websocket server has shutdown");
104108
}
105109

106110
/// Spawns a task to handle a new TCP stream as a potential WebSocket connection.

0 commit comments

Comments
 (0)