smol-rs / futures-lite

Futures, streams, and async I/O combinators.
Apache License 2.0
439 stars 25 forks source link

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

Open zhiburt opened 3 years ago

zhiburt commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 :disappointed:

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

zhiburt commented 3 years ago

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

zhiburt commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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