Skip to content

io: mandate stdio operations #7178

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 3 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
8 changes: 5 additions & 3 deletions tokio/src/blocking.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
cfg_rt! {
#[allow(unused_imports)]
pub(crate) use crate::runtime::spawn_blocking;

cfg_fs! {
#[allow(unused_imports)]
cfg_io_blocking! {
pub(crate) use crate::runtime::spawn_mandatory_blocking;
}

Expand All @@ -15,6 +15,7 @@ cfg_not_rt! {
use std::pin::Pin;
use std::task::{Context, Poll};

#[allow(dead_code)]
pub(crate) fn spawn_blocking<F, R>(_f: F) -> JoinHandle<R>
where
F: FnOnce() -> R + Send + 'static,
Expand All @@ -24,7 +25,7 @@ cfg_not_rt! {
panic!("requires the `rt` Tokio feature flag")
}

cfg_fs! {
cfg_io_blocking! {
pub(crate) fn spawn_mandatory_blocking<F, R>(_f: F) -> Option<JoinHandle<R>>
where
F: FnOnce() -> R + Send + 'static,
Expand Down Expand Up @@ -58,6 +59,7 @@ cfg_not_rt! {
}
}

#[allow(dead_code)]
fn assert_send_sync<T: Send + Sync>() {
}
}
49 changes: 43 additions & 6 deletions tokio/src/io/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub(crate) const DEFAULT_MAX_BUF_SIZE: usize = 2 * 1024 * 1024;
enum State<T> {
Idle(Option<Buf>),
Busy(sys::Blocking<(io::Result<usize>, Buf, T)>),
Shutdown,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is there a new state?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At first, I changed the Busy state to:

Busy(Option<sys::Blocking<(io::Result<usize>, Buf, T)>>)

since spawn_mandatory_blocking optionally returns a JoinHandle. But now Busy is representing two states:

  • Busy(Some(jh)): the task has been registered and will be picked up by one of the threads
  • Busy(None): the runtime is shutting down
    I think it makes sense to only use Busy for the first one and introduce the Shutdown state to represent the second one.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess under the current implementation, the spawn_blocking call panics and then you end up in the Idle(None) state.

}

cfg_io_blocking! {
Expand Down Expand Up @@ -73,11 +74,18 @@ where
let mut inner = self.inner.take().unwrap();

let max_buf_size = cmp::min(dst.remaining(), DEFAULT_MAX_BUF_SIZE);
self.state = State::Busy(sys::run(move || {

let rx = sys::run(move || {
// SAFETY: the requirements are satisfied by `Blocking::new`.
let res = unsafe { buf.read_from(&mut inner, max_buf_size) };
(res, buf, inner)
}));
});

self.state = if let Some(rx) = rx {
State::Busy(rx)
} else {
State::Shutdown
};
}
State::Busy(ref mut rx) => {
let (res, mut buf, inner) = ready!(Pin::new(rx).poll(cx))?;
Expand All @@ -97,6 +105,9 @@ where
}
}
}
State::Shutdown => {
return Poll::Ready(Err(gone()));
}
}
}
}
Expand All @@ -121,12 +132,19 @@ where
let n = buf.copy_from(src, DEFAULT_MAX_BUF_SIZE);
let mut inner = self.inner.take().unwrap();

self.state = State::Busy(sys::run(move || {
let rx = sys::run(move || {
let n = buf.len();
let res = buf.write_to(&mut inner).map(|()| n);

(res, buf, inner)
}));
});

self.state = if let Some(rx) = rx {
State::Busy(rx)
} else {
State::Shutdown
};

self.need_flush = true;

return Poll::Ready(Ok(n));
Expand All @@ -139,6 +157,9 @@ where
// If error, return
res?;
}
State::Shutdown => {
return Poll::Ready(Err(gone()));
}
}
}
}
Expand All @@ -153,10 +174,16 @@ where
let buf = buf_cell.take().unwrap();
let mut inner = self.inner.take().unwrap();

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

self.state = if let Some(rx) = rx {
State::Busy(rx)
} else {
State::Shutdown
};

self.need_flush = false;
} else {
Expand All @@ -171,6 +198,9 @@ where
// If error, return
res?;
}
State::Shutdown => {
return Poll::Ready(Err(gone()));
}
}
}
}
Expand Down Expand Up @@ -304,3 +334,10 @@ cfg_fs! {
}
}
}

fn gone() -> io::Error {
io::Error::new(
io::ErrorKind::Other,
crate::util::error::RUNTIME_SHUTTING_DOWN_ERROR,
)
}
2 changes: 1 addition & 1 deletion tokio/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ cfg_io_blocking! {
/// Types in this module can be mocked out in tests.
mod sys {
// TODO: don't rename
pub(crate) use crate::blocking::spawn_blocking as run;
pub(crate) use crate::blocking::spawn_mandatory_blocking as run;
pub(crate) use crate::blocking::JoinHandle as Blocking;
}
}
2 changes: 1 addition & 1 deletion tokio/src/runtime/blocking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
mod pool;
pub(crate) use pool::{spawn_blocking, BlockingPool, Spawner};

cfg_fs! {
cfg_io_blocking! {
pub(crate) use pool::spawn_mandatory_blocking;
}

Expand Down
4 changes: 2 additions & 2 deletions tokio/src/runtime/blocking/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ where
rt.spawn_blocking(func)
}

cfg_fs! {
cfg_io_blocking! {
#[cfg_attr(any(
all(loom, not(test)), // the function is covered by loom tests
test
Expand Down Expand Up @@ -327,7 +327,7 @@ impl Spawner {
}
}

cfg_fs! {
cfg_io_blocking! {
#[track_caller]
#[cfg_attr(any(
all(loom, not(test)), // the function is covered by loom tests
Expand Down
2 changes: 1 addition & 1 deletion tokio/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ cfg_rt! {
pub(crate) use blocking::Mandatory;
}

cfg_fs! {
cfg_io_blocking! {
pub(crate) use blocking::spawn_mandatory_blocking;
}

Expand Down