Skip to content

fix(libkernel): take NOTE_LOWAT on socket read and write events - #7092

Merged
oneandonlydean merged 2 commits into
boykopovar:mainfrom
milescoviello:fix/kevent-note-lowat
Oct 17, 2026
Merged

oneandonlydean merged 2 commits into
boykopovar:mainfrom
milescoviello:fix/kevent-note-lowat

Conversation

@milescoviello

@milescoviello milescoviello commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

What

kevent threw NOTE_LOWAT not implemented. Wine's wineserver registers each of its sockets with EVFILT_READ and EVFILT_WRITE carrying NOTE_LOWAT and a mark of 1, so Forever Launcher (PPSA99030) stops on it as its wineserver starts. NOTE_LOWAT now sets the low-water mark of a socket's read and write events, as FreeBSD 11's filt_soread and filt_sowrite read kn_sdata:

  • EVFILT_READ on a socket fires once the buffered bytes reach the mark. It also fires at end of file, and on a listening socket, which FreeBSD checks before the mark. A read mark above 1 is taken on stream sockets only. On a datagram socket, Linux's FIONREAD reports only the next datagram, where FreeBSD's sbavail counts every queued byte, so such a mark throws.
  • EVFILT_WRITE on a socket takes a mark of 1. The host's writability stands in for one free byte, since the host doesn't report the send buffer's free space. Other write marks throw.
  • A later change of the knote replaces the mark (or clears it without NOTE_LOWAT), as kqueue_register replaces kn_sdata.
  • A mark below 1 throws. It makes FreeBSD's read filter fire even with nothing buffered, which the host's readiness can't express.
  • A pipe ignores the mark, as filt_piperead does.

The debt entries say the same.

Tested

Ubuntu 24.04, GCC 13.3, Release, main 864729d.

#7075, #7088 and #7049 change lines next to these in Kqueue.cpp, GuestKqueue.cpp and the libkernel debt file (#7075 also edits the two debt lines that list NOTE_LOWAT as throwing). The conflicts are textual, and I'll rebase this one if any of them lands first.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: PPSA99030 (Forever Launcher), after feat(libkernel): pass descriptors in SCM_RIGHTS messages on Unix sockets #7090 and the PRs for its earlier stops; no bring-up discussion exists yet
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

kevent threw for NOTE_LOWAT. On a socket's EVFILT_READ it now sets the
low-water mark as FreeBSD 11's filt_soread does: the event fires once the
buffered bytes reach it, or at end of file or on a listening socket, and
a later change of the knote replaces it. EVFILT_WRITE takes a mark of 1,
where the host's writability stands in for a free byte; other write marks
and marks below 1 still throw. A pipe ignores the mark, as filt_piperead
does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@oneandonlydean

Copy link
Copy Markdown
Collaborator

Thanks, this matches FreeBSD 11's filt_soread/filt_sowrite well: the listening-socket bypass, EOF/error firing past the mark, write mark 1 only, the mark reset on re-add, and pipes ignoring the flag all look right.

One change before merge: on Linux, FIONREAD on a UDP socket returns the size of the next datagram only (udp_ioctl -> first_packet_length), while FreeBSD's sbavail counts every queued byte (Windows also returns the total). So with a read mark N > 1 on a datagram socket and several small datagrams queued whose total reaches N, FreeBSD fires and this never does. That turns a loud "not implemented" throw on main into a silent hang. Could you keep throwing for a read mark above 1 on non-stream sockets (or, if you need it, sum the queue some other way)? Wine's mark 1 isn't affected.

Minor, optional: Listening() does a getsockopt on every poll of a below-mark socket. It's cheap, but could be cached once the socket starts listening.

…ream sockets

On Linux, FIONREAD on a datagram socket reports the next datagram only,
where FreeBSD's sbavail counts every queued byte, so a read mark above 1
there could never fire. kevent now throws for it as before; stream
sockets and marks of 1 are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@milescoviello

Copy link
Copy Markdown
Contributor Author

Thanks @oneandonlydean, good catch on the datagram case. 91fbafd keeps throwing for a read mark above 1 on any socket that isn't a stream (a new GuestSockets::Type), and adds a datagram case to the test: a read mark of 2 throws, and a mark of 1 fires on a one-byte datagram. Stream sockets and Wine's mark of 1 are unchanged, and the debt entry says why. guest_kqueue passes 3 of 3 and still fails with main's Kqueue.cpp.

I left Listening() uncached: it only runs while a socket is below its mark, and a cached flag would need updating from listen and close.

AI-assisted: yes (Claude Code)

@github-actions

github-actions Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 6343 core/libs/prx/libkernel/Equeue/src/Kqueue.cpp L129
docs/dev/debt/libkernel.md L112
PR 6542 docs/dev/debt/libkernel.md L111-112
PR 7049 core/libs/tests/GuestKqueue.cpp L652
PR 7075 docs/dev/debt/libkernel.md L112, L174
PR 7088 core/libs/prx/libkernel/Equeue/src/Kqueue.cpp L129, L222-224
docs/dev/debt/libkernel.md L112, L174
PR 7121 core/libs/prx/libkernel/Equeue/src/Kqueue.cpp L222-228
docs/dev/debt/libkernel.md L112-113
PR 7129 core/libs/prx/libkernel/Equeue/src/Kqueue.cpp L129, L145-147, L151
core/libs/prx/libkernel/Socket/include/SocketRuntime.hpp L18
core/libs/tests/GuestKqueue.cpp L272
docs/dev/debt/libkernel.md L112, L173-174
PR 7139 docs/dev/debt/libkernel.md L173-174
PR 7248 docs/dev/debt/libkernel.md L173-174

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 5021 core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 6044 core/libs/tests/GuestKqueue.cpp
PR 6222 core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 6447 core/libs/prx/libkernel/Equeue/Equeue.cpp
PR 6510 core/libs/prx/libkernel/Equeue/Equeue.cpp
core/libs/prx/libkernel/Equeue/Equeue.hpp
core/libs/prx/libkernel/Equeue/src/Kqueue.cpp
core/libs/tests/GuestKqueue.cpp
docs/dev/debt/libkernel.md
PR 6710 docs/dev/debt/libkernel.md
PR 6962 core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 7201 core/libs/prx/libkernel/Socket/src/Udp.cpp

@oneandonlydean
oneandonlydean merged commit b5e570d into boykopovar:main Oct 17, 2026
8 of 9 checks passed
ycarogabriel-lgtm added a commit to ycarogabriel-lgtm/AnyPS5 that referenced this pull request Oct 17, 2026
XNU's FIONREAD on a datagram socket counts the queued sender addresses
as well as the payload (35 bytes for one byte on an AF_UNIX pair, 51
over UDP), so kevent reported that size as EVFILT_READ data and fstat
as st_size. With NOTE_LOWAT now honoured (boykopovar#7092), guest_kqueue saw the
inflated count and aborted. SO_NREAD returns the next datagram's size,
which matches Linux and the ioctl(FIONREAD) path in this file already
uses it for datagrams.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ycarogabriel-lgtm added a commit to ycarogabriel-lgtm/AnyPS5 that referenced this pull request Oct 18, 2026
XNU's FIONREAD on a datagram socket counts the queued sender addresses
as well as the payload (35 bytes for one byte on an AF_UNIX pair, 51
over UDP), so kevent reported that size as EVFILT_READ data and fstat
as st_size. With NOTE_LOWAT now honoured (boykopovar#7092), guest_kqueue saw the
inflated count and aborted. SO_NREAD returns the next datagram's size,
which matches Linux and the ioctl(FIONREAD) path in this file already
uses it for datagrams.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ycarogabriel-lgtm added a commit to ycarogabriel-lgtm/AnyPS5 that referenced this pull request Oct 25, 2026
XNU's FIONREAD on a datagram socket counts the queued sender addresses
as well as the payload (35 bytes for one byte on an AF_UNIX pair, 51
over UDP), so kevent reported that size as EVFILT_READ data and fstat
as st_size. With NOTE_LOWAT now honoured (boykopovar#7092), guest_kqueue saw the
inflated count and aborted. SO_NREAD returns the next datagram's size,
which matches Linux and the ioctl(FIONREAD) path in this file already
uses it for datagrams.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants