Skip to content

Commit

Permalink
fix: Reader should be drainable after writer disconnects
Browse files Browse the repository at this point in the history
Fix an issue where dropping the writer while some data still remained in the pipe caused the reader to return an error instead of allowing you to drain the bytes in the pipe until empty.
  • Loading branch information
sagebind committed Jun 6, 2019
1 parent 5f6d03c commit 05830ab
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/pipe/chunked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,17 @@ impl AsyncRead for Reader {
// of buffers, so this can never happen.
if e.is_full() {
panic!("buffer pool overflow")
} else {
}

// If the writer disconnects, then we'll just discard this
// buffer and any subsequent buffers until we've read
// everything still in the pipe.
else if e.is_disconnected() {
// Nothing!
}

// Some other error occurred.
else {
return Poll::Ready(Err(io::ErrorKind::BrokenPipe.into()));
}
}
Expand Down Expand Up @@ -193,6 +203,23 @@ mod tests {
let mut dest = [0; 5];
assert_eq!(reader.read(&mut dest).await.unwrap(), 5);
assert_eq!(&dest, b"hello");
});
})
}

#[test]
fn reader_still_drainable_after_writer_disconnects() {
block_on(async {
let (mut reader, mut writer) = new(1);

writer.write_all(b"hello").await.unwrap();

drop(writer);

let mut dest = [0; 5];
assert_eq!(reader.read(&mut dest).await.unwrap(), 5);
assert_eq!(&dest, b"hello");

assert_eq!(reader.read(&mut dest).await.unwrap(), 0);
})
}
}

0 comments on commit 05830ab

Please sign in to comment.