Skip to content

Add support for non-blocking I/O operations on UDP sockets - #5507

Open
WhySoBad wants to merge 3 commits into
rust-lang:masterfrom
WhySoBad:udp-non-blocking-io
Open

WhySoBad wants to merge 3 commits into
rust-lang:masterfrom
WhySoBad:udp-non-blocking-io

Conversation

@WhySoBad

@WhySoBad WhySoBad commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

This pull request adds support for non-blocking read/recv and write/send to the UDP socket shim.

This branch is based on the branch from #5497. The ioctl commit can thus be ignored in this PR.

@rustbot rustbot added the S-waiting-on-review Status: Waiting for a review to complete label Oct 27, 2026
@WhySoBad
WhySoBad force-pushed the udp-non-blocking-io branch from 6937830 to 66c0724 Compare October 30, 2026 23:16
@rustbot

This comment has been minimized.

@RalfJung RalfJung left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Comment thread src/shims/unix/udp_socket.rs Resolved
Comment thread src/shims/unix/tcp_socket.rs Resolved
Comment thread src/shims/unix/tcp_socket.rs Resolved
Comment thread tests/utils/libc.rs Resolved
@rustbot rustbot added S-waiting-on-author Status: Waiting for the PR author to address review comments and removed S-waiting-on-review Status: Waiting for a review to complete labels Oct 31, 2026
@rustbot

rustbot commented Oct 31, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@WhySoBad
WhySoBad force-pushed the udp-non-blocking-io branch 2 times, most recently from 5952160 to 8f8907f Compare November 3, 2026 17:51
@WhySoBad

WhySoBad commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Waiting for a review to complete and removed S-waiting-on-author Status: Waiting for the PR author to address review comments labels Nov 3, 2026
@RalfJung

RalfJung commented Nov 4, 2026

Copy link
Copy Markdown
Member

(comments sent privately as GH is broken)
@rustbot author

@rustbot rustbot added S-waiting-on-author Status: Waiting for the PR author to address review comments and removed S-waiting-on-review Status: Waiting for a review to complete labels Nov 4, 2026
@WhySoBad
WhySoBad force-pushed the udp-non-blocking-io branch from 8f8907f to fc0efdf Compare November 4, 2026 22:46
@WhySoBad

WhySoBad commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

The line of the CI failure does not exist anymore? I think the CI should pass when you manually trigger it again.

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Waiting for a review to complete and removed S-waiting-on-author Status: Waiting for the PR author to address review comments labels Nov 4, 2026
@RalfJung

RalfJung commented Nov 5, 2026

Copy link
Copy Markdown
Member

I suspect you have to rebase to reproduce the CI failure locally.

@RalfJung RalfJung left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Comment thread src/shims/unix/udp_socket.rs Resolved
@rustbot rustbot added S-waiting-on-author Status: Waiting for the PR author to address review comments and removed S-waiting-on-review Status: Waiting for a review to complete labels Nov 5, 2026
@WhySoBad
WhySoBad force-pushed the udp-non-blocking-io branch 2 times, most recently from 8c4b387 to bea65a2 Compare November 5, 2026 11:10
@rustbot

This comment has been minimized.

@WhySoBad
WhySoBad force-pushed the udp-non-blocking-io branch from bea65a2 to 17d7507 Compare November 5, 2026 23:33
@rustbot

This comment has been minimized.

Comment thread src/shims/unix/udp_socket.rs Resolved
@RalfJung

Copy link
Copy Markdown
Member

I guess we have to map that, this seems too basic to skip.

@WhySoBad
WhySoBad force-pushed the udp-non-blocking-io branch 2 times, most recently from d5095c2 to 5013267 Compare November 10, 2026 20:49
@WhySoBad

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Waiting for a review to complete and removed S-waiting-on-author Status: Waiting for the PR author to address review comments labels Nov 10, 2026
@WhySoBad

Copy link
Copy Markdown
Contributor Author

From this connect(3) man page section I'd assume that it should be fine to change the default destination address multiple times (since we can reset a default destination address?):

If the initiating socket is not connection-mode, then connect() shall set the socket's peer address, and no connection is made. For SOCK_DGRAM sockets, the peer address identifies where all datagrams are sent on subsequent send() functions, and limits the remote sender for subsequent recv() functions. If the sa_family member of address is AF_UNSPEC, the socket's peer address shall be reset.

The BSDs seem to interpret this differently. I'd suggest we just store the default destination address on a connect and then always use sendto with either the explicit destination address or the default destination address. If neither is Some we manually return EDESTADDRREQ. We then (again) no longer need the EDESTADDRREQ mapping.

Parsing non-IP socket addresses is one of the things I'll add to my todo list since we currently don't support AF_UNSPEC (and it's also relevant for being able to bind Unix domain sockets).

@RalfJung

RalfJung commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

The BSDs seem to interpret this differently.

What do they do instead?
Apparently they return "Socket is already connected". Interesting.

From the spec:

For SOCK_DGRAM sockets, the peer address identifies where all datagrams are sent on subsequent send() functions, and limits the remote sender for subsequent recv() functions.

We'd not emulate this correctly by only storing a default sendto. I guess we can discard packets from different senders ourselves but that's beginning to seem a bit dicey.

POSIX also says

If the socket has not already been bound to a local address, connect() shall bind it to an address which, unless the socket's address family is AF_UNIX, is an unused local address.

So it does not seem unreasonable to say that you can connect at most once.
EDIT: Oh that's the local address, not the remote one.

OTOH it also says

[EISCONN]
    The specified socket is connection-mode and is already connected.

which sounds like for non-connection-mode sockets, double-connecting should work.

Wow that spec is so vague...

@RalfJung

Copy link
Copy Markdown
Member

Honestly my first inclination is to say that we should throw "unsupported" when connect is called the 2nd time. 🤷 I don't think this is common enough to warrant preemptively worrying about it too much.

I also found this in the Rust docs:

If your first connect is to a loopback address, subsequent connects to non-loopback addresses might fail, depending on the platform.

@WhySoBad
WhySoBad force-pushed the udp-non-blocking-io branch from 5013267 to 292b303 Compare November 10, 2026 22:07
@WhySoBad

Copy link
Copy Markdown
Contributor Author

Oh fun, Linux and the BSDs take the different branches of this "or" from the sendto(3) man page:

If a peer address has been pre-specified, either the message shall be sent to the address specified by dest_addr (overriding the pre-specified peer address), or the function shall return -1 and set errno to [EISCONN].

@WhySoBad
WhySoBad force-pushed the udp-non-blocking-io branch from 292b303 to a306de0 Compare November 10, 2026 22:29
@RalfJung

Copy link
Copy Markdown
Member

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: Waiting for the PR author to address review comments and removed S-waiting-on-review Status: Waiting for a review to complete labels Nov 11, 2026
Comment thread src/shims/unix/udp_socket.rs Resolved
Comment thread src/shims/unix/udp_socket.rs Resolved
Comment thread src/shims/unix/udp_socket.rs Resolved
@WhySoBad
WhySoBad force-pushed the udp-non-blocking-io branch 2 times, most recently from 5f4e9de to 6dac8a6 Compare November 11, 2026 10:16
@WhySoBad

Copy link
Copy Markdown
Contributor Author

Oh fun, Linux and the BSDs take the different branches of this "or" from the sendto(3) man page:

Do you have a preference on what Miri should do here? Since the POSIX man page explicitly allows either overruling the default destination address or returning EISCONN, I think we should model this as close as possible.

My suggestion would thus be, that for sendto's with an explicit destination address, we return EISCONN on BSD targets if there is already a default destination address set, and on BSD hosts we throw unsupported if there is already a default destination address set.

@RalfJung

Copy link
Copy Markdown
Member

Since the POSIX man page explicitly allows either overruling the default destination address or returning EISCONN, I think we should model this as close as possible.

That sounds reasonable.

@WhySoBad
WhySoBad force-pushed the udp-non-blocking-io branch from 6dac8a6 to 5781b61 Compare November 11, 2026 12:06
@WhySoBad

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Waiting for a review to complete and removed S-waiting-on-author Status: Waiting for the PR author to address review comments labels Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Waiting for a review to complete

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants