Skip to content

fix(noq): don't stall tokio UDP sends on OpenBSD send lock contention - #4

Merged
losfair merged 1 commit into
devfrom
claude/wizardly-galileo-wtuew9
Oct 15, 2026
Merged

losfair merged 1 commit into
devfrom
claude/wizardly-galileo-wtuew9

Conversation

@losfair

@losfair losfair commented Oct 15, 2026

Copy link
Copy Markdown
Owner

Problem

On OpenBSD, an endpoint running on the tokio runtime can go permanently silent. Every connection on it stops sending at the same moment, and they all time out later. It receives and handles packets fine, including new Initials, but never answers.

Cause

  1. The EAGAIN is not a full buffer.
    • sendit adds MSG_DONTWAIT for non-blocking descriptors (sys/kern/uipc_syscalls.c).
    • sosend then takes the send buffer lock with sblock(&so->so_snd, SBLOCKWAIT(flags)). That doesn't sleep, so it fails with EWOULDBLOCK when another thread is in sosend on the same socket at the time (sys/kern/uipc_socket.c, uipc_socket2.c).
    • The buffer space check can't be what fails for UDP. Nothing is queued in so_snd, so it is always empty. An oversized datagram is rejected with EMSGSIZE before that check.
  2. Clearing readiness makes it permanent. The tokio runtime's try_send goes through try_io, which clears write readiness on WouldBlock. The sender then waits for kqueue to report the socket writable again, and for a UDP socket that event never comes, because nothing ever drains its send buffer.
  3. One socket, many senders. Each connection sends through its own sender on the shared socket from different tasks, so one lost race stalls them all.

It's most likely when several connections, or one multipath connection's paths, send at once. One example is a burst of CONNECTION_CLOSEs on several paths.

The smol runtime isn't affected: async-io's readiness wait re-arms the socket, and kqueue reports a UDP socket writable straight away.

Evidence

From rapidmasque's OpenBSD smoke test on x86_64 (KVM) and arm64 (emulated); see losfair/rapidmasque#7.

  • The race itself: the kdump of the server shows two concurrent sendmsg(10, …) calls, one of which returns -1 errno 35 Resource temporarily unavailable while the other completes. It was the only failed sendmsg of the run, and after it the server made no further sendmsg calls at all.
  • The receive side keeps working: with the server stalled, a 2 s trace shows it still recvmsging a new client's Initials and sending nothing.
  • With the fix: the sequence that triggered it (multipath clients with extra paths starting and exiting back to back) failed by round 2–3 in every one of 8 OpenBSD jobs without the fix. It passes 6 rounds on both architectures with it.

Fix

On OpenBSD only, try_send retries a WouldBlock send up to 64 times with thread::yield_now(), without touching readiness. If the contention persists, it drops the datagram (Ok(())), as UdpSocketState::send already does for other send errors; QUIC recovers the loss. Elsewhere the behaviour is unchanged.

Testing

  • cargo clippy -p noq --all-targets -- -D warnings passes.
  • cargo +nightly check -p noq --no-default-features --features runtime-tokio --target x86_64-unknown-openbsd -Zbuild-std=std,panic_abort passes with no warnings.
  • cargo test -p noq --lib: 3 tests fail (echo_v6, echo_dualstack, local_addr). They fail identically on dev, because this sandbox has no IPv6.
  • The OpenBSD runtime check is rapidmasque's smoke test, described above.

🤖 Generated with Claude Code

https://claude.ai/code/session_012baC35vUbLPPhuK4pz4uRz


Generated by Claude Code

OpenBSD fails a non-blocking UDP sendmsg with EAGAIN while another
thread holds the socket's send buffer lock: sosend takes it with
RW_NOSLEEP under MSG_DONTWAIT, which sendit sets for non-blocking
descriptors. The buffer itself never fills for UDP. The tokio runtime's
try_send went through try_io, which clears write readiness on
WouldBlock, so the sender then waited for a kqueue EVFILT_WRITE event
that a UDP socket never raises again (nothing ever drains its send
buffer). Every connection sending on that socket stalled for good: an
endpoint shared by several connections, or a multipath connection
sending on several paths at once, went silent until its connections
timed out.

On OpenBSD, retry such a send a bounded number of times without
touching readiness, and drop the datagram if contention persists, as
for other send errors.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012baC35vUbLPPhuK4pz4uRz
@github-actions

Copy link
Copy Markdown

Documentation for this PR has been generated and is available at: https://losfair.github.io/noq/pr/4/docs/noq/

Last updated: 2026-10-15T18:06:48Z

@losfair
losfair merged commit cad1d3a into dev Oct 15, 2026
16 of 34 checks passed
@losfair
losfair deleted the claude/wizardly-galileo-wtuew9 branch October 15, 2026 18:08
losfair pushed a commit to losfair/rapidmasque that referenced this pull request Oct 15, 2026
losfair/noq#4 merged as cad1d3a on dev (same tree as the PR head
2f590c1).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012baC35vUbLPPhuK4pz4uRz
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.

2 participants