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

io: impl AsyncSeek for BufReader/BufWriter #3491

Merged
merged 4 commits into from May 14, 2021
Merged

io: impl AsyncSeek for BufReader/BufWriter #3491

merged 4 commits into from May 14, 2021

Conversation

taiki-e
Copy link
Member

@taiki-e taiki-e commented Jan 31, 2021

Motivation

Closes #2291

Solution

The implementation and added tests are basically based on my PRs in futures-rs. (rust-lang/futures-rs#1573, rust-lang/futures-rs#1608)

However, the implementation is complicated than futures-rs due to:

@taiki-e taiki-e added A-tokio Area: The main tokio crate M-io Module: tokio/io labels Jan 31, 2021
@taiki-e

This comment has been minimized.

@taiki-e taiki-e marked this pull request as draft February 2, 2021 00:18
Comment on lines +190 to +193
// 1.x AsyncSeek recommends calling poll_complete before start_seek.
// We don't have to guarantee that the value returned by
// poll_complete called without start_seek is correct,
// so we'll return 0.
Copy link
Member Author

Choose a reason for hiding this comment

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

We don't have to guarantee that the value returned by poll_complete called without start_seek is correct,

@carllerche @Darksonn Is this assumption correct? If not, are there rules I need to follow?

Copy link
Contributor

Choose a reason for hiding this comment

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

Returning Ok(0) if poll_complete is called before start_seek seems reasonable to me.

Comment on lines +178 to +183
// We needs to call seek operation multiple times.
// And we should always call both start_seek and poll_complete,
// as start_seek alone cannot guarantee that the operation will be completed.
// poll_complete receives a Context and returns a Poll, so it cannot be called
// inside start_seek.
*self.project().seek_state = SeekState::Start(pos);
Copy link
Member Author

Choose a reason for hiding this comment

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

This is another reason why this implementation is more complicated than the futures-rs one.

@taiki-e taiki-e changed the title impl AsyncSeek for BufReader/BufWriter/BufStream io: impl AsyncSeek for BufReader/BufWriter/BufStream Feb 6, 2021
@taiki-e taiki-e marked this pull request as ready for review February 6, 2021 15:19
@rafibaum
Copy link

rafibaum commented May 4, 2021

I'm getting some errors from this implementation in the writer when a File is used, where poll_complete isn't getting called before a new start_seek call is queued.

thread 'tokio-runtime-worker' panicked at 'must wait for poll_complete before calling start_seek', /home/rafi/.cargo/git/checkouts/tokio-f564add6bba9226f/fa793aa/tokio/src/fs/file.rs:561:28
stack backtrace:
   0: std::panicking::begin_panic
   1: <tokio::fs::file::File as tokio::io::async_seek::AsyncSeek>::start_seek
   2: <&mut T as tokio::io::async_seek::AsyncSeek>::poll_complete
   3: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
   4: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
   5: tokio::runtime::task::harness::poll_future
   6: tokio::runtime::task::harness::Harness<T,S>::poll
   7: std::thread::local::LocalKey<T>::with
   8: tokio::runtime::thread_pool::worker::Context::run_task
   9: tokio::runtime::thread_pool::worker::Context::run
  10: tokio::macros::scoped_tls::ScopedKey<T>::set
  11: tokio::runtime::thread_pool::worker::run
  12: tokio::loom::std::unsafe_cell::UnsafeCell<T>::with_mut
  13: <std::panic::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once
  14: tokio::runtime::task::harness::Harness<T,S>::poll
  15: tokio::runtime::blocking::pool::Inner::run
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

I fixed this on a personal fork by having the BufWriter implementation call poll_complete if the seek state is Init and also calling ready!(poll_complete)? before calling start_seek.

@taiki-e
Copy link
Member Author

taiki-e commented May 5, 2021

@rafibaum Thanks for finding that problem. I've merged your patch into this PR. (cbd6a5b)

Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

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

This seems fine to me.

@taiki-e taiki-e merged commit 8c395df into tokio-rs:master May 14, 2021
@taiki-e taiki-e deleted the io-buf-rw-seek branch May 14, 2021 10:32
@Darksonn Darksonn mentioned this pull request May 14, 2021
@taiki-e
Copy link
Member Author

taiki-e commented May 25, 2021

For some reason, I seemed to have forgotten about BufStream... 😅

@taiki-e taiki-e changed the title io: impl AsyncSeek for BufReader/BufWriter/BufStream io: impl AsyncSeek for BufReader/BufWriter May 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate M-io Module: tokio/io
Projects
None yet
Development

Successfully merging this pull request may close these issues.

tokio-io: No AsyncSeek implementation for BufReader and BufWriter
3 participants