Skip to content

Commit

Permalink
fix copy buffered write deadlock
Browse files Browse the repository at this point in the history
cherry-pick of tokio-rs/tokio#4001
  • Loading branch information
cfal committed Aug 26, 2021
1 parent e4d4f8f commit 6dfdb3c
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/copy_bidirectional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use std::task::{Context, Poll};
#[derive(Debug)]
struct CopyBuffer {
read_done: bool,
need_flush: bool,
start_index: usize,
cache_length: usize,
size: usize,
Expand All @@ -31,6 +32,7 @@ impl CopyBuffer {
}
Self {
read_done: false,
need_flush: false,
start_index: 0,
cache_length: 0,
size,
Expand Down Expand Up @@ -74,6 +76,13 @@ impl CopyBuffer {
}
}
Poll::Pending => {
// Try flushing when the reader has no progress to avoid deadlock
// when the reader depends on buffered writer.
if self.need_flush {
ready!(writer.as_mut().poll_flush(cx))?;
self.need_flush = false;
}

read_pending = true;
}
}
Expand Down

0 comments on commit 6dfdb3c

Please sign in to comment.