Skip to content

net: document pipe try_read*/try_write* readiness behavior#8032

Merged
mattiapitossi merged 4 commits intotokio-rs:masterfrom
barry3406:fix/pipe-try-read-readiness-v2
Apr 19, 2026
Merged

net: document pipe try_read*/try_write* readiness behavior#8032
mattiapitossi merged 4 commits intotokio-rs:masterfrom
barry3406:fix/pipe-try-read-readiness-v2

Conversation

@barry3406
Copy link
Copy Markdown
Contributor

@barry3406 barry3406 commented Apr 9, 2026

Motivation

#7625 reports that calling pipe::Receiver::try_read immediately after creating the receiver returns WouldBlock even when data is already available in the pipe. The same applies to try_read_vectored, try_read_buf, and the try_write* methods on pipe::Sender.

This is intentional: Registration::try_io checks the cached readiness state and short-circuits with WouldBlock until the I/O driver has had a chance to deliver an event for the registration. The same model applies to every other Tokio I/O type. But the pipe docs do not currently call this out, and it's easy to assume try_* is a thin syscall wrapper — which led to the confusion in #7625 (and to my own first attempt at "fixing" it in this PR before @Darksonn and @Thomasdezeeuw kindly explained the actual model).

Solution

Add a # Notes section to all five try_* methods on pipe::Sender and pipe::Receiver explaining that the runtime's I/O driver only delivers readiness events after control has been yielded back to it, and that calling try_* before any .await (such as readable() / writable()) will return WouldBlock even when the operation could otherwise succeed.

No code or behavior changes. The note follows the existing # Notes convention used in udp.rs, tcp/stream.rs, etc., and is positioned between # Return and # Examples to match udp::UdpSocket::try_recv_from which has the same shape.

Verified with the docs.rs-equivalent build from the contributors guide:

RUSTDOCFLAGS="--cfg docsrs --cfg tokio_unstable" RUSTFLAGS="--cfg docsrs --cfg tokio_unstable" cargo +nightly doc -p tokio --all-features --no-deps

No new warnings from pipe.rs, and the rendered HTML on both the Receiver and Sender pages contains all five new # Notes sections.

@mattiapitossi mattiapitossi added A-tokio Area: The main tokio crate M-net Module: tokio/net labels Apr 9, 2026
Comment thread tokio/tests/net_unix_pipe.rs Outdated
Add a Notes section to all five try_* methods on pipe::Sender and
pipe::Receiver explaining that the runtime's I/O driver only delivers
readiness events after control is yielded back to it, so calling
try_read/try_write before any .await returns WouldBlock even when the
operation could otherwise succeed.

This is the same readiness model used by every other Tokio I/O type;
the pipe docs simply did not previously call it out. Refs tokio-rs#7625.
@barry3406 barry3406 force-pushed the fix/pipe-try-read-readiness-v2 branch from 90227a3 to adef662 Compare April 10, 2026 14:34
@barry3406 barry3406 changed the title net: pipe Receiver try_read bypasses readiness check net: document pipe try_read*/try_write* readiness behavior Apr 10, 2026
@mattiapitossi mattiapitossi added the T-docs Topic: documentation label Apr 11, 2026
Comment thread tokio/src/net/unix/pipe.rs Outdated
Copy link
Copy Markdown
Member

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

@Darksonn
Copy link
Copy Markdown
Member

Ah, looks like the WouldBlock link doesn't work.

@mattiapitossi mattiapitossi added the S-waiting-on-author Status: awaiting some action (such as code changes) from the PR or issue author. label Apr 13, 2026
@barry3406
Copy link
Copy Markdown
Contributor Author

good catch, fixed in 3c06d4b — added [WouldBlock]: std::io::ErrorKind::WouldBlock reference definitions under each Notes section to match the convention in async_fd.rs. cargo doc --no-deps now resolves cleanly.

@mattiapitossi
Copy link
Copy Markdown
Member

Thanks!

@mattiapitossi mattiapitossi merged commit 1afb391 into tokio-rs:master Apr 19, 2026
90 checks passed
@mattiapitossi mattiapitossi removed the S-waiting-on-author Status: awaiting some action (such as code changes) from the PR or issue author. label Apr 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-tokio Area: The main tokio crate M-net Module: tokio/net T-docs Topic: documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants