Skip to content
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

Propagate AsyncWrite flushes to lower IO layers for stdout / stderr. #1528

Merged
merged 1 commit into from
Sep 13, 2019
Merged
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
41 changes: 32 additions & 9 deletions tokio-fs/src/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use self::State::*;
pub(crate) struct Blocking<T> {
inner: Option<T>,
state: State<T>,
/// true if the lower IO layer needs flushing
need_flush: bool,
}

#[derive(Debug)]
Expand All @@ -39,6 +41,7 @@ impl<T> Blocking<T> {
Blocking {
inner: Some(inner),
state: State::Idle(Some(Buf::with_capacity(0))),
need_flush: false,
}
}
}
Expand Down Expand Up @@ -119,6 +122,7 @@ where

(res, buf, inner)
}));
self.need_flush = true;

return Ready(Ok(n));
}
Expand All @@ -135,16 +139,35 @@ where
}

fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
let (res, buf, inner) = match self.state {
Idle(_) => return Ready(Ok(())),
Busy(ref mut rx) => ready!(Pin::new(rx).poll(cx)),
};

// The buffer is not used here
self.state = Idle(Some(buf));
self.inner = Some(inner);
loop {
let need_flush = self.need_flush;
match self.state {
// The buffer is not used here
Idle(ref mut buf_cell) => {
if need_flush {
let buf = buf_cell.take().unwrap();
let mut inner = self.inner.take().unwrap();

self.state = Busy(sys::run(move || {
let res = inner.flush().map(|_| 0);
(res, buf, inner)
}));

self.need_flush = false;
} else {
return Ready(Ok(()));
}
}
Busy(ref mut rx) => {
let (res, buf, inner) = ready!(Pin::new(rx).poll(cx));
self.state = Idle(Some(buf));
self.inner = Some(inner);

Ready(res.map(|_| ()))
// If error, return
res?;
}
}
}
}

fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
Expand Down