Skip to content

Commit

Permalink
tokio/asyncfile: fix endless loop on disconnection
Browse files Browse the repository at this point in the history
Handle the case when read() is returning Ok(0), which means that
the connection was closed.

Ref:
tokio-rs/tokio#4389 (reply in thread)
  • Loading branch information
manio committed Feb 4, 2022
1 parent 8dd6bf5 commit a73a18d
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/asyncfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,18 @@ impl AsyncFile {
pub async fn _read(&self, out: &mut [u8]) -> io::Result<usize> {
loop {
let mut guard = self.inner.readable().await?;
match guard.try_io(|inner| inner.get_ref().read(out)) {
match guard.try_io(|inner| {
let res = inner.get_ref().read(out);

//handle Ok(0) results:
if let Ok(len) = res {
if len == 0 {
return Err(Error::new(ErrorKind::Other, "USB disconnected"));
}
}

res
}) {
Ok(result) => return result,
Err(_would_block) => continue,
}
Expand Down

0 comments on commit a73a18d

Please sign in to comment.