Skip to content

Commit

Permalink
Break when socket is unexpectedly shut down
Browse files Browse the repository at this point in the history
  • Loading branch information
dlon authored and bluejekyll committed Mar 16, 2024
1 parent da41e4a commit d2e64d8
Showing 1 changed file with 42 additions and 4 deletions.
46 changes: 42 additions & 4 deletions crates/server/src/server/server_future.rs
Expand Up @@ -95,6 +95,9 @@ impl<T: RequestHandler> ServerFuture<T> {
let message = match message {
Err(e) => {
warn!("error receiving message on udp_socket: {}", e);
if is_unrecoverable_socket_error(&e) {
break;
}
continue;
}
Ok(message) => message,
Expand Down Expand Up @@ -169,6 +172,9 @@ impl<T: RequestHandler> ServerFuture<T> {
Ok((t, s)) => (t, s),
Err(e) => {
debug!("error receiving TCP tcp_stream error: {}", e);
if is_unrecoverable_socket_error(&e) {
break;
}
continue;
},
},
Expand Down Expand Up @@ -227,7 +233,11 @@ impl<T: RequestHandler> ServerFuture<T> {
reap_tasks(&mut inner_join_set);
}

Ok(())
if shutdown.is_cancelled() {
Ok(())
} else {
Err(ProtoError::from("unexpected close of socket"))
}
});
}

Expand Down Expand Up @@ -298,6 +308,9 @@ impl<T: RequestHandler> ServerFuture<T> {
Ok((t, s)) => (t, s),
Err(e) => {
debug!("error receiving TLS tcp_stream error: {}", e);
if is_unrecoverable_socket_error(&e) {
break;
}
continue;
},
},
Expand Down Expand Up @@ -373,7 +386,11 @@ impl<T: RequestHandler> ServerFuture<T> {
reap_tasks(&mut inner_join_set);
}

Ok(())
if shutdown.is_cancelled() {
Ok(())
} else {
Err(ProtoError::from("unexpected close of socket"))
}
});

Ok(())
Expand Down Expand Up @@ -451,6 +468,9 @@ impl<T: RequestHandler> ServerFuture<T> {
Ok((t, s)) => (t, s),
Err(e) => {
debug!("error receiving TLS tcp_stream error: {}", e);
if is_unrecoverable_socket_error(&e) {
break;
}
continue;
},
},
Expand Down Expand Up @@ -519,7 +539,11 @@ impl<T: RequestHandler> ServerFuture<T> {
reap_tasks(&mut inner_join_set);
}

Ok(())
if shutdown.is_cancelled() {
Ok(())
} else {
Err(ProtoError::from("unexpected close of socket"))
}
});

Ok(())
Expand Down Expand Up @@ -646,6 +670,9 @@ impl<T: RequestHandler> ServerFuture<T> {
Ok((t, s)) => (t, s),
Err(e) => {
debug!("error receiving HTTPS tcp_stream error: {}", e);
if is_unrecoverable_socket_error(&e) {
break;
}
continue;
},
},
Expand Down Expand Up @@ -696,7 +723,11 @@ impl<T: RequestHandler> ServerFuture<T> {
reap_tasks(&mut inner_join_set);
}

Ok(())
if shutdown.is_cancelled() {
Ok(())
} else {
Err(ProtoError::from("unexpected close of socket"))
}
});

Ok(())
Expand Down Expand Up @@ -1214,6 +1245,13 @@ fn sanitize_src_address(src: SocketAddr) -> Result<(), String> {
}
}

fn is_unrecoverable_socket_error(err: &io::Error) -> bool {
matches!(
err.kind(),
io::ErrorKind::NotConnected | io::ErrorKind::ConnectionAborted
)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit d2e64d8

Please sign in to comment.