Skip to content

Fix poll reporting stale readiness (read + write) - #15501

Draft
cpetig wants to merge 2 commits into
bytecodealliance:mainfrom
cpetig:readiness-fixes
Draft

cpetig wants to merge 2 commits into
bytecodealliance:mainfrom
cpetig:readiness-fixes

Conversation

@cpetig

@cpetig cpetig commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Reimplementation of #14987
Fixes #14986

Poll can report a file/socket as ready after a short try_read/write. Check again.

(This patch fixes the reported problem but the tests still look rough, so I make it draft for now)

Tokio only clears a socket's cached read readiness when a read returns
WouldBlock. `TcpReceiveStream::poll_ready` returned that cached readiness
directly, so a readiness probe (WASIp2 `pollable.ready`, a zero-length
WASIp3 `stream.read`) could report ready on a fresh connection, or after
a short read had drained the socket.

Peek a single byte instead: `poll_peek` waits for data, end of stream or
an error, and clears the readiness on WouldBlock. This costs one
`recv(MSG_PEEK)` per readiness poll.

Add p2 and p3 tests for fresh and drained connections.
Same class of bug as for receive streams: `try_write` only clears tokio's
cached write readiness on WouldBlock, so after a short write has filled
the kernel send buffer `TcpSendStream::poll_ready` still reported the
socket as writable. This is visible through the zero-length WASIp3
`stream.write` readiness probe.

After `poll_write_ready` reports ready, confirm with `poll(POLLOUT)` and,
if the socket isn't writable, clear the readiness via `try_io` returning
WouldBlock, which re-arms the waker. This costs one `poll` syscall per
readiness poll. `POLLOUT` is stricter than "a write would succeed" (Linux
reports not writable while the buffer is more than about half full), the
same as for `poll()` itself. On non-Unix targets behaviour is unchanged.

Add a WASIp3 test: the probe stays pending while the send buffer is full
and completes once the peer has drained it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wasi-sockets: TCP input stream reports ready before any data arrives

1 participant