Skip to content

Commit

Permalink
server: swallow TLS errors in the accept loop (#1990)
Browse files Browse the repository at this point in the history
* fix: swallow TLS errors in the accept loop

* chore: address feedback
  • Loading branch information
PDXKimani authored Oct 15, 2024
1 parent 71dcf86 commit a00200e
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions tonic/src/transport/server/incoming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ where
while let Some(item) = incoming.next().await {
yield match item {
Ok(_) => item.map(ServerIo::new_io)?,
Err(e) => match handle_accept_error(e) {
Err(e) => match handle_tcp_accept_error(e) {
ControlFlow::Continue(()) => continue,
ControlFlow::Break(e) => Err(e)?,
}
Expand Down Expand Up @@ -74,11 +74,16 @@ where
yield io;
}

SelectOutput::Err(e) => match handle_accept_error(e) {
SelectOutput::TcpErr(e) => match handle_tcp_accept_error(e) {
ControlFlow::Continue(()) => continue,
ControlFlow::Break(e) => Err(e)?,
}

SelectOutput::TlsErr(e) => {
tracing::debug!(error = %e, "tls accept error");
continue;
}

SelectOutput::Done => {
break;
}
Expand All @@ -87,7 +92,7 @@ where
}
}

fn handle_accept_error(e: impl Into<crate::Error>) -> ControlFlow<crate::Error> {
fn handle_tcp_accept_error(e: impl Into<crate::Error>) -> ControlFlow<crate::Error> {
let e = e.into();
tracing::debug!(error = %e, "accept loop error");
if let Some(e) = e.downcast_ref::<io::Error>() {
Expand All @@ -97,8 +102,6 @@ fn handle_accept_error(e: impl Into<crate::Error>) -> ControlFlow<crate::Error>
| io::ErrorKind::ConnectionReset
| io::ErrorKind::BrokenPipe
| io::ErrorKind::Interrupted
| io::ErrorKind::InvalidData // Raised if TLS handshake failed
| io::ErrorKind::UnexpectedEof // Raised if TLS handshake failed
| io::ErrorKind::WouldBlock
| io::ErrorKind::TimedOut
) {
Expand All @@ -121,7 +124,7 @@ where
return match incoming.try_next().await {
Ok(Some(stream)) => SelectOutput::Incoming(stream),
Ok(None) => SelectOutput::Done,
Err(e) => SelectOutput::Err(e.into()),
Err(e) => SelectOutput::TcpErr(e.into()),
};
}

Expand All @@ -130,15 +133,15 @@ where
match stream {
Ok(Some(stream)) => SelectOutput::Incoming(stream),
Ok(None) => SelectOutput::Done,
Err(e) => SelectOutput::Err(e.into()),
Err(e) => SelectOutput::TcpErr(e.into()),
}
}

accept = tasks.join_next() => {
match accept.expect("JoinSet should never end") {
Ok(Ok(io)) => SelectOutput::Io(io),
Ok(Err(e)) => SelectOutput::Err(e),
Err(e) => SelectOutput::Err(e.into()),
Ok(Err(e)) => SelectOutput::TlsErr(e),
Err(e) => SelectOutput::TlsErr(e.into()),
}
}
}
Expand All @@ -148,7 +151,8 @@ where
enum SelectOutput<A> {
Incoming(A),
Io(ServerIo<A>),
Err(crate::Error),
TcpErr(crate::Error),
TlsErr(crate::Error),
Done,
}

Expand Down

0 comments on commit a00200e

Please sign in to comment.