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

Prevent server from exiting on ECONNABORTED #1874

Merged
merged 5 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 8 additions & 3 deletions tonic/src/transport/server/incoming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,22 @@ use tokio_stream::{Stream, StreamExt};
use tracing::warn;

#[cfg(not(feature = "tls"))]
pub(crate) fn tcp_incoming<IO, IE>(
incoming: impl Stream<Item = Result<IO, IE>>,
pub(crate) fn tcp_incoming<IO>(
incoming: impl Stream<Item = Result<IO, std::io::Error>>,
) -> impl Stream<Item = Result<ServerIo<IO>, crate::Error>>
where
IO: AsyncRead + AsyncWrite + Unpin + Send + 'static,
IE: Into<crate::Error>,
{
async_stream::try_stream! {
let mut incoming = pin!(incoming);

while let Some(item) = incoming.next().await {
if let Some(e) = item.as_ref().err() {
if e.kind() == std::io::ErrorKind::ConnectionAborted {
tracing::debug!(message = e.to_string(), error = %e);
continue;
}
}
yield item.map(ServerIo::new_io)?
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok(_) => item.map(ServerIo::new_io)?

could also be written as

Ok(io) => Ok::<ServerIo<IO>, crate::Error>(ServerIo::new_io(io))?,

but in terms of readability and diff, I opted for the former (any better ideas?).

Expand Down
22 changes: 8 additions & 14 deletions tonic/src/transport/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ impl<L> Server<L> {
}
}

pub(crate) async fn serve_with_shutdown<S, I, F, IO, IE, ResBody>(
pub(crate) async fn serve_with_shutdown<S, I, F, IO, ResBody>(
self,
svc: S,
incoming: I,
Expand All @@ -500,10 +500,9 @@ impl<L> Server<L> {
<<L as Layer<S>>::Service as Service<Request<BoxBody>>>::Future: Send + 'static,
<<L as Layer<S>>::Service as Service<Request<BoxBody>>>::Error:
Into<crate::Error> + Send + 'static,
I: Stream<Item = Result<IO, IE>>,
I: Stream<Item = Result<IO, std::io::Error>>,
IO: AsyncRead + AsyncWrite + Connected + Unpin + Send + 'static,
IO::ConnectInfo: Clone + Send + Sync + 'static,
IE: Into<crate::Error>,
F: Future<Output = ()>,
ResBody: http_body::Body<Data = Bytes> + Send + 'static,
ResBody::Error: Into<crate::Error>,
Expand Down Expand Up @@ -733,7 +732,7 @@ impl<L> Router<L> {
let incoming = TcpIncoming::new(addr, self.server.tcp_nodelay, self.server.tcp_keepalive)
.map_err(super::Error::from_source)?;
self.server
.serve_with_shutdown::<_, _, future::Ready<()>, _, _, ResBody>(
.serve_with_shutdown::<_, _, future::Ready<()>, _, ResBody>(
self.routes.prepare(),
incoming,
None,
Expand Down Expand Up @@ -775,15 +774,11 @@ impl<L> Router<L> {
/// This method discards any provided [`Server`] TCP configuration.
///
/// [`Server`]: struct.Server.html
pub async fn serve_with_incoming<I, IO, IE, ResBody>(
self,
incoming: I,
) -> Result<(), super::Error>
pub async fn serve_with_incoming<I, IO, ResBody>(self, incoming: I) -> Result<(), super::Error>
djc marked this conversation as resolved.
Show resolved Hide resolved
where
I: Stream<Item = Result<IO, IE>>,
I: Stream<Item = Result<IO, std::io::Error>>,
IO: AsyncRead + AsyncWrite + Connected + Unpin + Send + 'static,
IO::ConnectInfo: Clone + Send + Sync + 'static,
IE: Into<crate::Error>,
L: Layer<Routes>,
L::Service:
Service<Request<BoxBody>, Response = Response<ResBody>> + Clone + Send + 'static,
Expand All @@ -794,7 +789,7 @@ impl<L> Router<L> {
ResBody::Error: Into<crate::Error>,
{
self.server
.serve_with_shutdown::<_, _, future::Ready<()>, _, _, ResBody>(
.serve_with_shutdown::<_, _, future::Ready<()>, _, ResBody>(
self.routes.prepare(),
incoming,
None,
Expand All @@ -810,16 +805,15 @@ impl<L> Router<L> {
/// This method discards any provided [`Server`] TCP configuration.
///
/// [`Server`]: struct.Server.html
pub async fn serve_with_incoming_shutdown<I, IO, IE, F, ResBody>(
pub async fn serve_with_incoming_shutdown<I, IO, F, ResBody>(
djc marked this conversation as resolved.
Show resolved Hide resolved
self,
incoming: I,
signal: F,
) -> Result<(), super::Error>
where
I: Stream<Item = Result<IO, IE>>,
I: Stream<Item = Result<IO, std::io::Error>>,
IO: AsyncRead + AsyncWrite + Connected + Unpin + Send + 'static,
IO::ConnectInfo: Clone + Send + Sync + 'static,
IE: Into<crate::Error>,
F: Future<Output = ()>,
L: Layer<Routes>,
L::Service:
Expand Down