diff --git a/src/reader.rs b/src/reader.rs index ca5df0f..86d967a 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -124,3 +124,9 @@ impl futures::io::AsyncRead for PipeReader { self.poll_read(cx, buf) } } + +impl Drop for PipeReader { + fn drop(&mut self) { + self.close().ok(); + } +} diff --git a/src/writer.rs b/src/writer.rs index f32c3b2..2d1ac16 100644 --- a/src/writer.rs +++ b/src/writer.rs @@ -100,6 +100,16 @@ impl PipeWriter { } fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll> { + if Arc::strong_count(&self.state) == 1 { + return Poll::Ready(Err(io::Error::new( + io::ErrorKind::BrokenPipe, + format!( + "{}: PipeWriter: The channel is closed", + env!("CARGO_PKG_NAME") + ), + ))); + } + let mut state = match self.state.lock() { Ok(s) => s, Err(err) => { @@ -138,6 +148,12 @@ impl PipeWriter { } } +impl Drop for PipeWriter { + fn drop(&mut self) { + self.close().ok(); + } +} + #[cfg(feature = "tokio")] impl tokio::io::AsyncWrite for PipeWriter { fn poll_write(self: Pin<&mut Self>, cx: &mut Context, buf: &[u8]) -> Poll> {