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

question: What is the proper way how to wait for a BufReader::read_line to finish? #47

Open
zhiburt opened this issue Jul 8, 2021 · 6 comments

Comments

@zhiburt
Copy link
Contributor

zhiburt commented Jul 8, 2021

Hey there,
so I was trying to read_line from a BufReader but it produces WouldBlock if there's no data available.
What is the proper way to force the thing to wait until the data is actually there?

        p.read_line(&mut msg).await.unwrap();
thread 'bash' panicked at 'called `Result::unwrap()` on an `Err` value: Os { code: 11, kind: WouldBlock, message: "Resource temporarily unavailable" }', tests/repl_async.rs:15:37

I use BufReader on a async_fs::File

By the way is it normal? https://docs.rs/futures-io/0.3.15/futures_io/trait.AsyncBufRead.html#tymethod.poll_fill_buf

If no data is available for reading, the method returns Poll::Pending and arranges for the current task (via cx.waker().wake_by_ref()) to receive a notification when the object becomes readable or is closed.

@zhiburt
Copy link
Contributor Author

zhiburt commented Jul 8, 2021

Also might be related as I also get on one test

thread 'python' panicked at 'assertion failed: `(left == right)`
  left: `4`,
 right: `0`', /home/mzhiburt/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-lite-1.12.0/src/io.rs:1700:13

@zhiburt
Copy link
Contributor Author

zhiburt commented Jul 8, 2021

And ongoing question, if you don't mind.
Is it safe to do the following.

future::poll_once(self.inner.read(buf)).await

Where inner is a BufRead<async_file::File>;

Because it just don't work, the buffer is never read 😞

When I use std::fs::File with O_NONBLOCK it works good.

@zhiburt
Copy link
Contributor Author

zhiburt commented Jul 8, 2021

Testing it in a different project and read_line works fine. ...

@zhiburt
Copy link
Contributor Author

zhiburt commented Jul 8, 2021

I can state that it sometimes fails and sometimes not.

p.read_line(&mut msg).await.unwrap(); // works
***
p.read_line(&mut msg).await.unwrap(); // panic on assert_debug

@zhiburt
Copy link
Contributor Author

zhiburt commented Jul 8, 2021

So moving from async-fs to async-io cause a different issue on the same code

thread 'python' panicked at 'assertion failed: buf.is_empty()', /home/mzhiburt/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-lite-1.12.0/src/io.rs:1699:13

@zhiburt
Copy link
Contributor Author

zhiburt commented Jul 8, 2021

Running it in --release to disable debug_asserts drops 6 characters where were available if we would read it in a sync mode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant