tokio: peek will block after read
Version └── tokio v1.6.0 └── tokio-macros v1.1.0 (proc-macro)
Platform
Runing in Windows 64-bit
x86_64 GNU/Linux Cross-compilation cargo build --release --target x86_64-pc-windows-gnu
Description I tried this code:
use tokio::io::AsyncReadExt;
#[tokio::main]
async fn main() -> std::io::Result<()> {
let listener = tokio::net::TcpListener::bind("0.0.0.0:2222").await?;
loop {
let (mut socket, _) = listener.accept().await?;
tokio::spawn(async move {
let mut buf = [0; 1];
let _ = socket.read(&mut buf).await;
let _ = socket.peek(&mut buf).await;
});
}
}
telnet 127.0.0.1 2222
It will block in socket.peek
About this issue
- Original URL
- State: open
- Created 6 years ago
- Comments: 18 (7 by maintainers)
Commits related to this issue
- fix: switch to tokio's LengthDelimitedCodec for reading because windows can't peek ;) https://github.com/tokio-rs/tokio/issues/3789 — committed to voltlane/connserver by lionkor 2 years ago
- fix: switch to tokio's LengthDelimitedCodec for reading because windows can't peek ;) https://github.com/tokio-rs/tokio/issues/3789 — committed to voltlane/connserver by lionkor 2 years ago
@Alost If you what I built instead to work around that is a smarter buffer that you can use to peek unlimited quantity of data (compared to the fixed length
BufReader). See: https://github.com/caido/async_buf_readI think the issue is with mio. This can be shown by running
(with mio features
os-pollandnet)Connect to 127.0.0.1 43265 and send messages. On Windows, only two events for read readiness will be received, one on the first message and one on disconnect. This explains why
peekblocks after correctly waking up once, because it is waiting for a second that never comes. However, on Linux, a read readiness event will be fired after every message, which causespeekto wake up every message and work correctly.