Skip to content

Close pipes when dropping them, check for closed pipe when flushing a writer #13

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
16 changes: 16 additions & 0 deletions src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ impl PipeWriter {
}

fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
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) => {
Expand Down Expand Up @@ -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<io::Result<usize>> {
Expand Down