Skip to content

Commit 04c59c6

Browse files
committed
fix: handle abnormal connection closure correctly
1 parent d1db159 commit 04c59c6

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

crates/base/src/server.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,19 @@ impl<S: Stream + Unpin> Stream for NotifyOnEos<S> {
5454

5555
struct WorkerService {
5656
worker_req_tx: mpsc::UnboundedSender<WorkerRequestMsg>,
57+
cancel: CancellationToken,
5758
}
5859

5960
impl WorkerService {
60-
fn new(worker_req_tx: mpsc::UnboundedSender<WorkerRequestMsg>) -> Self {
61-
Self { worker_req_tx }
61+
fn new(worker_req_tx: mpsc::UnboundedSender<WorkerRequestMsg>) -> (Self, CancellationToken) {
62+
let cancel = CancellationToken::new();
63+
(
64+
Self {
65+
worker_req_tx,
66+
cancel: cancel.clone(),
67+
},
68+
cancel,
69+
)
6270
}
6371
}
6472

@@ -73,7 +81,7 @@ impl Service<Request<Body>> for WorkerService {
7381

7482
fn call(&mut self, req: Request<Body>) -> Self::Future {
7583
// create a response in a future.
76-
let cancel = CancellationToken::new();
84+
let cancel = self.cancel.child_token();
7785
let worker_req_tx = self.worker_req_tx.clone();
7886
let fut = async move {
7987
let (res_tx, res_rx) = oneshot::channel::<Result<Response<Body>, hyper::Error>>();
@@ -83,7 +91,7 @@ impl Service<Request<Body>> for WorkerService {
8391
let msg = WorkerRequestMsg {
8492
req,
8593
res_tx,
86-
conn_watch: Some(ob_conn_watch_rx),
94+
conn_watch: Some(ob_conn_watch_rx.clone()),
8795
};
8896

8997
worker_req_tx.send(msg)?;
@@ -235,15 +243,21 @@ impl Server {
235243
match msg {
236244
Ok((conn, _)) => {
237245
tokio::task::spawn(async move {
238-
let service = WorkerService::new(main_worker_req_tx);
246+
let (service, cancel) = WorkerService::new(main_worker_req_tx);
247+
let _guard = cancel.drop_guard();
248+
239249
let conn_fut = Http::new()
240250
.serve_connection(conn, service);
241251

242252
if let Err(e) = conn_fut.await {
243253
// Most common cause for these errors are
244254
// when the client closes the connection
245255
// before we could send a response
246-
error!("client connection error ({:?})", e);
256+
if e.is_incomplete_message() {
257+
debug!("connection reset ({:?})", e);
258+
} else {
259+
error!("client connection error ({:?})", e);
260+
}
247261
}
248262
});
249263
}

0 commit comments

Comments
 (0)