Skip to content

Commit

Permalink
Ignore NotConnected error in poll_shutdown()
Browse files Browse the repository at this point in the history
We try to write buffered data to the stream before shutting it down. This
seems to be in line with the guidance from Tokio's AsyncWrite docs:

> Invocation of a shutdown implies an invocation of flush. Once this method
> returns `Ready` it implies that a flush successfully happened before the
> shutdown happened. That is, callers don't need to call flush before
> calling shutdown. They can rely that by calling shutdown any pending
> buffered data will be written out.

We propagate errors from the write. However, if we get a `NotConnected`
error during shutdown we might just as well ignore it.
  • Loading branch information
djc committed Feb 20, 2024
1 parent ff32e1e commit addf108
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub struct Stream<'a, IO, C> {
pub io: &'a mut IO,
pub session: &'a mut C,
pub eof: bool,
pub shutdown: bool,
}

impl<'a, IO: AsyncRead + AsyncWrite + Unpin, C, SD> Stream<'a, IO, C>
Expand All @@ -77,6 +78,7 @@ where
// The state so far is only used to detect EOF, so either Stream
// or EarlyData state should both be all right.
eof: false,
shutdown: false,
}
}

Expand Down Expand Up @@ -332,10 +334,24 @@ where
}

fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
if self.shutdown {
return Poll::Ready(Ok(()));
}

while self.session.wants_write() {
ready!(self.write_io(cx))?;
match ready!(self.write_io(cx)) {
Ok(_) => {}
Err(err) if err.kind() == io::ErrorKind::NotConnected => {
self.shutdown = true;
return Poll::Ready(Ok(()));
}
Err(err) => return Poll::Ready(Err(err)),
}
}
Pin::new(&mut self.io).poll_shutdown(cx)

ready!(Pin::new(&mut self.io).poll_shutdown(cx))?;
self.shutdown = true;
Poll::Ready(Ok(()))
}
}

Expand Down

0 comments on commit addf108

Please sign in to comment.