libsmol opened issue #14986:
Summary
On WASIp2, a TCP
input-streamcan report itself ready before the peer has sent anything:subscribe().ready()returnstrueright after the connection is set up. A guest that waits on the pollable and then does a blocking read ends up blocking in the read instead, and in some embeddings wasi-libc'spoll()reports an idle socket as readable.Reproduction
With the
setuphelper fromcrates/test-programs/src/bin/p2_tcp_streams.rs:
rust setup(net, family, |server, client| { // Nothing has been sent in either direction, yet this fails: assert!(!client.input.subscribe().ready()); });On
main(ffff2e2) this fails in bothp2::sync::p2_tcp_streamsandp2::async_::p2_tcp_streams. The accompanying pull request adds it as a test.Through wasi-libc, in an embedder that uses
wasmtime_wasi::p2::add_to_linker_syncwith Wasmtime 50.0.0: after accepting a connection whose peer never sends,poll()withPOLLINand a one-second timeout returns 1 immediately withrevents == POLLIN, although there is nothing to read. I couldn't reproduce this second case through thewasmtimeCLI with the same program.Both were observed on Linux (aarch64).
Cause
TcpReceiveStream::poll_readyincrates/wasi/src/sockets/tcp.rsreturnstokio::net::TcpStream::poll_read_ready, which reflects tokio's cached readiness. Tokio clears read readiness only when an operation returnsWouldBlock, and for these sockets it can already be set before any data has arrived. In a standalone tokio program, for example, a stream returned byTcpListener::acceptis reported readable before anything has been sent, whilepoll_peekon the same stream correctly returnsPending.Proposed fix
I'll open a pull request that makes
poll_readypeek a single byte, which waits for data, the end of the stream or an error, and clears tokio's readiness when there's nothing to read yet. Happy to take a different approach if you'd prefer.
This issue and the accompanying pull request were prepared by an AI coding assistant (Claude Code, running Anthropic's Claude Opus 5.5) on behalf of the libsmol project, which ran into this while running RustPython under Wasmtime. We've tried to verify everything described here, but please let us know if anything looks off or unclear.
tschneidereit closed issue #14986:
Summary
On WASIp2, a TCP
input-streamcan report itself ready before the peer has sent anything:subscribe().ready()returnstrueright after the connection is set up. A guest that waits on the pollable and then does a blocking read ends up blocking in the read instead, and in some embeddings wasi-libc'spoll()reports an idle socket as readable.Reproduction
With the
setuphelper fromcrates/test-programs/src/bin/p2_tcp_streams.rs:
rust setup(net, family, |server, client| { // Nothing has been sent in either direction, yet this fails: assert!(!client.input.subscribe().ready()); });On
main(ffff2e2) this fails in bothp2::sync::p2_tcp_streamsandp2::async_::p2_tcp_streams. The accompanying pull request adds it as a test.Through wasi-libc, in an embedder that uses
wasmtime_wasi::p2::add_to_linker_syncwith Wasmtime 50.0.0: after accepting a connection whose peer never sends,poll()withPOLLINand a one-second timeout returns 1 immediately withrevents == POLLIN, although there is nothing to read. I couldn't reproduce this second case through thewasmtimeCLI with the same program.Both were observed on Linux (aarch64).
Cause
TcpReceiveStream::poll_readyincrates/wasi/src/sockets/tcp.rsreturnstokio::net::TcpStream::poll_read_ready, which reflects tokio's cached readiness. Tokio clears read readiness only when an operation returnsWouldBlock, and for these sockets it can already be set before any data has arrived. In a standalone tokio program, for example, a stream returned byTcpListener::acceptis reported readable before anything has been sent, whilepoll_peekon the same stream correctly returnsPending.Proposed fix
I'll open a pull request that makes
poll_readypeek a single byte, which waits for data, the end of the stream or an error, and clears tokio's readiness when there's nothing to read yet. Happy to take a different approach if you'd prefer.
This issue and the accompanying pull request were prepared by an AI coding assistant (Claude Code, running Anthropic's Claude Opus 5.5) on behalf of the libsmol project, which ran into this while running RustPython under Wasmtime. We've tried to verify everything described here, but please let us know if anything looks off or unclear.
tschneidereit commented on issue #14986:
Please note our AI tool use policy: it's not acceptable to let your agent open issues or PRs on your behalf. "We've tried to verify everything described here" is definitely not meeting our bar: you must invest the time to not "try" but do verify the contents of the PR. Expecting us to point out to you anything that looks off or unclear is a clear violation of our prohibition on extractive contributions.
I'll close this, but feel free to open a new issue once you're fully confident it's worth our time.
cpetig commented on issue #14986:
:thinking: I can confirm that a very similar issue bit me while porting an internal network protocol to wasip3 with cooperative threads. Everything works out of the box, but the thread synchronization code blocks at program exit.
So I have another totally unrelated real-world example (with small Rust reproduction code) which hits a similar problem.
Does the following patch solve the problem for you as well?
diff --git a/crates/wasi/src/sockets/tcp.rs b/crates/wasi/src/sockets/tcp.rs
index b2526d9897..3cc245d70a 100644
--- a/crates/wasi/src/sockets/tcp.rs
+++ b/crates/wasi/src/sockets/tcp.rs
@@ -662,7 +662,11 @@ pub(crate) struct TcpReceiveStream {
}
impl TcpReceiveStream {
pub(crate) fn poll_ready(&mut self, cx: &mut std::task::Context<'_>) -> Poll<()> {
- self.inner.poll_read_ready(cx).map(|_| ())
- //
poll_read_readycan report stale readiness after a shorttry_read; peeking- // clears it on
WouldBlock. Errors are left for the subsequent read to report.- let mut byte = [0u8; 1];
- let mut buf = tokio::io::ReadBuf::new(&mut byte);
- self.inner.poll_peek(cx, &mut buf).map(|_| ())
}pub(crate) fn poll_read(
cpetig edited a comment on issue #14986:
:thinking: I can confirm that a very similar issue bit me while porting an internal network protocol to wasip3 with cooperative threads. Everything works out of the box, but the thread synchronization code blocks at program exit.
So I have another totally unrelated real-world example (with small Rust reproduction code) which hits a similar problem.
Does the following patch solve the problem for you as well?
diff --git a/crates/wasi/src/sockets/tcp.rs b/crates/wasi/src/sockets/tcp.rs index b2526d9897..3cc245d70a 100644 --- a/crates/wasi/src/sockets/tcp.rs +++ b/crates/wasi/src/sockets/tcp.rs @@ -662,7 +662,11 @@ pub(crate) struct TcpReceiveStream { } impl TcpReceiveStream { pub(crate) fn poll_ready(&mut self, cx: &mut std::task::Context<'_>) -> Poll<()> { - self.inner.poll_read_ready(cx).map(|_| ()) + // `poll_read_ready` can report stale readiness after a short `try_read`; peeking + // clears it on `WouldBlock`. Errors are left for the subsequent read to report. + let mut byte = [0u8; 1]; + let mut buf = tokio::io::ReadBuf::new(&mut byte); + self.inner.poll_peek(cx, &mut buf).map(|_| ()) } pub(crate) fn poll_read(
cpetig commented on issue #14986:
Same example as a Rust script (easier to deploy, no Cargo.toml needed)
cargo +nightly -Zscript build --manifest-path poll_repro.rs --target wasm32-wasip3 --target-dir t wasmtime run -W component-model-threading -S inherit-network t/wasm32-wasip3/debug/poll_repro.wasm
cpetig reopened issue #14986:
Summary
On WASIp2, a TCP
input-streamcan report itself ready before the peer has sent anything:subscribe().ready()returnstrueright after the connection is set up. A guest that waits on the pollable and then does a blocking read ends up blocking in the read instead, and in some embeddings wasi-libc'spoll()reports an idle socket as readable.Reproduction
With the
setuphelper fromcrates/test-programs/src/bin/p2_tcp_streams.rs:
rust setup(net, family, |server, client| { // Nothing has been sent in either direction, yet this fails: assert!(!client.input.subscribe().ready()); });On
main(ffff2e2) this fails in bothp2::sync::p2_tcp_streamsandp2::async_::p2_tcp_streams. The accompanying pull request adds it as a test.Through wasi-libc, in an embedder that uses
wasmtime_wasi::p2::add_to_linker_syncwith Wasmtime 50.0.0: after accepting a connection whose peer never sends,poll()withPOLLINand a one-second timeout returns 1 immediately withrevents == POLLIN, although there is nothing to read. I couldn't reproduce this second case through thewasmtimeCLI with the same program.Both were observed on Linux (aarch64).
Cause
TcpReceiveStream::poll_readyincrates/wasi/src/sockets/tcp.rsreturnstokio::net::TcpStream::poll_read_ready, which reflects tokio's cached readiness. Tokio clears read readiness only when an operation returnsWouldBlock, and for these sockets it can already be set before any data has arrived. In a standalone tokio program, for example, a stream returned byTcpListener::acceptis reported readable before anything has been sent, whilepoll_peekon the same stream correctly returnsPending.Proposed fix
I'll open a pull request that makes
poll_readypeek a single byte, which waits for data, the end of the stream or an error, and clears tokio's readiness when there's nothing to read yet. Happy to take a different approach if you'd prefer.
This issue and the accompanying pull request were prepared by an AI coding assistant (Claude Code, running Anthropic's Claude Opus 5.5) on behalf of the libsmol project, which ran into this while running RustPython under Wasmtime. We've tried to verify everything described here, but please let us know if anything looks off or unclear.
cpetig commented on issue #14986:
Same bug also manifests when compiling to a wasip2 target.
hxcliff commented on issue #14986:
4\. :thinking: I can confirm that a very similar issue bit me while porting an internal network protocol to wasip3 with cooperative threads. Everything works out of the box, but the thread synchronization code blocks at program exit.:thinking: 我可以确认,在将内部网络协议移植到使用协作线程的 wasip3 时,我遇到了一个非常类似的问题。一切开箱即用,但线程同步代码在程序退出时阻塞了。
So I have another totally unrelated real-world example (with small Rust reproduction code) which hits a similar problem.所以我还有另一个完全无关的真实案例(附有简短的 Rust 复现代码),它遇到了类似的问题。
Does the following patch solve the problem for you as well?以下补丁是否也能为你解决该问题?
`` diff --git a/crates/wasi/src/sockets/tcp.rs b/crates/wasi/src/sockets/tcp.rs index b2526d9897..3cc245d70a 100644 --- a/crates/wasi/src/sockets/tcp.rs +++ b/crates/wasi/src/sockets/tcp.rs @@ -662,7 +662,11 @@ pub(crate) struct TcpReceiveStream { } impl TcpReceiveStream { pub(crate) fn poll_ready(&mut self, cx: &mut std::task::Context<'_>) -> Poll<()> { - self.inner.poll_read_ready(cx).map(|_| ()) + //poll_read_readycan report stale readiness after a shorttry_read; peeking + // clears it onWouldBlock`. Errors are left for the subsequent read to report.
+ let mut byte = [0u8; 1];
+ let mut buf = tokio::io::ReadBuf::new(&mut byte);
+ self.inner.poll_peek(cx, &mut buf).map(|_| ())
}pub(crate) fn poll_read( ``` [reproduction.zip](https://github.com/user-attachments/files/34267361/reproduction.zip)Yes, it can be resolved. This https://github.com/bytecodealliance/wasmtime/pull/14987 can also resolve the issue.
Last updated: Nov 19 2026 at 20:05 UTC