fix(libkernel): fire EVFILT_WRITE on a socket once free send space reaches NOTE_LOWAT #8596

Merged
ebarr24 merged 1 commit from upstream-pr/8596 into main 2026-10-20 19:27:06 +00:00
ebarr24 commented 2026-10-20 17:48:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ebarr24 on 2026-10-20T17:48:23Z.


What

kevent threw NotImplemented for EVFILT_WRITE with NOTE_LOWAT above 1 on a socket, although the socket readiness check already reports free send-buffer space (SO_SNDBUF less SIOCOUTQ on Linux). The write event now fires once that space reaches the mark, as FreeBSD 11's filt_sowrite compares sbspace with kn_sdata, and stays silent while the space is below it. The debt entry no longer says a write mark is limited to 1. Reference: https://github.com/freebsd/freebsd-src/blob/release/11.0.0/sys/kern/uipc_socket.c

Tested

Windows 11, MinGW-w64 GCC 15.2, Release build with tests. guest_kqueue passes with this change; the new test fails on main.

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: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4939972348 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8596). Originally posted by [ebarr24](https://github.com/ebarr24) on 2026-10-20T17:48:23Z. --- ### What kevent threw NotImplemented for EVFILT_WRITE with NOTE_LOWAT above 1 on a socket, although the socket readiness check already reports free send-buffer space (SO_SNDBUF less SIOCOUTQ on Linux). The write event now fires once that space reaches the mark, as FreeBSD 11's filt_sowrite compares sbspace with kn_sdata, and stays silent while the space is below it. The debt entry no longer says a write mark is limited to 1. Reference: https://github.com/freebsd/freebsd-src/blob/release/11.0.0/sys/kern/uipc_socket.c ### Tested Windows 11, MinGW-w64 GCC 15.2, Release build with tests. `guest_kqueue` passes with this change; the new test fails on `main`. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-20 18:15:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-20T18:15:21Z.


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 7937 docs/dev/debt/libkernel.md L119-120
PR 8523 docs/dev/debt/libkernel.md L120

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 4630 docs/dev/debt/libkernel.md
PR 5021 docs/dev/debt/libkernel.md
PR 6044 core/libs/tests/GuestKqueue.cpp
PR 6149 docs/dev/debt/libkernel.md
PR 6469 docs/dev/debt/libkernel.md
PR 6519 docs/dev/debt/libkernel.md
PR 6740 docs/dev/debt/libkernel.md
PR 6977 docs/dev/debt/libkernel.md
PR 7049 core/libs/prx/libkernel/Equeue/src/Kqueue.cpp
core/libs/tests/GuestKqueue.cpp
docs/dev/debt/libkernel.md
PR 7088 core/libs/prx/libkernel/Equeue/src/Kqueue.cpp
core/libs/tests/GuestKqueue.cpp
docs/dev/debt/libkernel.md
PR 7105 docs/dev/debt/libkernel.md
PR 7121 core/libs/prx/libkernel/Equeue/src/Kqueue.cpp
core/libs/tests/GuestKqueue.cpp
docs/dev/debt/libkernel.md
PR 7149 docs/dev/debt/libkernel.md
PR 7192 docs/dev/debt/libkernel.md
PR 7464 docs/dev/debt/libkernel.md
PR 7509 docs/dev/debt/libkernel.md
PR 7530 docs/dev/debt/libkernel.md
PR 7653 core/libs/prx/libkernel/Equeue/src/Kqueue.cpp
core/libs/tests/GuestKqueue.cpp
docs/dev/debt/libkernel.md
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6297904181 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8596#issuecomment-6297904181). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-20T18:15:21Z. --- <!-- pr-overlap --> 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 7937](https://www.github.com/boykopovar/AnyPS5/pull/7937) | `docs/dev/debt/libkernel.md` [L119-120](https://github.com/boykopovar/AnyPS5/blob/434e965d4620c48e315b25fb869a2d84b39cfb2d/docs/dev/debt/libkernel.md#L119-L120) | | | | [PR 8523](https://www.github.com/boykopovar/AnyPS5/pull/8523) | `docs/dev/debt/libkernel.md` [L120](https://github.com/boykopovar/AnyPS5/blob/434e965d4620c48e315b25fb869a2d84b39cfb2d/docs/dev/debt/libkernel.md#L120) | | | 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 4630](https://www.github.com/boykopovar/AnyPS5/pull/4630) | `docs/dev/debt/libkernel.md` | | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `docs/dev/debt/libkernel.md` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/GuestKqueue.cpp` | | [PR 6149](https://www.github.com/boykopovar/AnyPS5/pull/6149) | `docs/dev/debt/libkernel.md` | | [PR 6469](https://www.github.com/boykopovar/AnyPS5/pull/6469) | `docs/dev/debt/libkernel.md` | | [PR 6519](https://www.github.com/boykopovar/AnyPS5/pull/6519) | `docs/dev/debt/libkernel.md` | | [PR 6740](https://www.github.com/boykopovar/AnyPS5/pull/6740) | `docs/dev/debt/libkernel.md` | | [PR 6977](https://www.github.com/boykopovar/AnyPS5/pull/6977) | `docs/dev/debt/libkernel.md` | | [PR 7049](https://www.github.com/boykopovar/AnyPS5/pull/7049) | `core/libs/prx/libkernel/Equeue/src/Kqueue.cpp`<br>`core/libs/tests/GuestKqueue.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 7088](https://www.github.com/boykopovar/AnyPS5/pull/7088) | `core/libs/prx/libkernel/Equeue/src/Kqueue.cpp`<br>`core/libs/tests/GuestKqueue.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 7105](https://www.github.com/boykopovar/AnyPS5/pull/7105) | `docs/dev/debt/libkernel.md` | | [PR 7121](https://www.github.com/boykopovar/AnyPS5/pull/7121) | `core/libs/prx/libkernel/Equeue/src/Kqueue.cpp`<br>`core/libs/tests/GuestKqueue.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 7149](https://www.github.com/boykopovar/AnyPS5/pull/7149) | `docs/dev/debt/libkernel.md` | | [PR 7192](https://www.github.com/boykopovar/AnyPS5/pull/7192) | `docs/dev/debt/libkernel.md` | | [PR 7464](https://www.github.com/boykopovar/AnyPS5/pull/7464) | `docs/dev/debt/libkernel.md` | | [PR 7509](https://www.github.com/boykopovar/AnyPS5/pull/7509) | `docs/dev/debt/libkernel.md` | | [PR 7530](https://www.github.com/boykopovar/AnyPS5/pull/7530) | `docs/dev/debt/libkernel.md` | | [PR 7653](https://www.github.com/boykopovar/AnyPS5/pull/7653) | `core/libs/prx/libkernel/Equeue/src/Kqueue.cpp`<br>`core/libs/tests/GuestKqueue.cpp`<br>`docs/dev/debt/libkernel.md` |
nikowolf123 commented 2026-10-20 19:04:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by nikowolf123 on 2026-10-20T19:04:36Z.


I checked these against 084dcf8 and replayed GuestSockets::Ready, PollEdge and the epoll wait in a standalone program on Linux 6.18 (WSL2). I did not build this branch.

1. An EV_CLEAR write knote with NOTE_LOWAT above 1 on a Linux TCP socket can wait forever

Without the throw, AddDescriptorKnote gives the mark to an EPOLLET | EPOLLOUT edge knote. PollEdge disarms that knote while the free space is below the mark. Only a new host EPOLLOUT wakeup or a later change of the knote arms it again.

Linux TCP sends that wakeup only after a send or a poll found the socket not writable. tcp_check_space calls tcp_new_space only while SOCK_NOSPACE is set (tcp_input.c#L5639-L5649; the flag is set in tcp.c#L565-L570). sk_stream_write_space then clears the flag on the one wakeup it sends (stream.c#L35-L42).

FreeBSD 11.0 calls sowwakeup on every ACK that frees send space (tcp_input.c#L1808, #L2836-L2844). Through KNOTE_LOCKED (uipc_sockbuf.c#L324) that runs the kn_data >= kn_sdata check in filt_sowrite (uipc_socket.c#L3338-L3339). So on FreeBSD the knote fires once sbspace reaches the mark.

Example: a loopback TCP writer has SO_SNDBUF 32768 and 5120 bytes unacknowledged (POLLOUT set, 27648 free). It adds EV_ADD | EV_CLEAR with NOTE_LOWAT 32768, and the first scan disarms the knote. After the peer has read everything (SIOCOUTQ 0, 32768 free), a 3 s wait still returns nothing in 3 of 3 runs, while a level knote with the same mark fires. In a second run the writer filled the buffer to EAGAIN. The one wakeup came at 16384 free, so a mark of 24576 never fired either, and the same happened with default buffer sizes.

On main this input threw. Now kevent with a NULL timeout hangs without a message, which is what #7092 avoided by keeping the throw for datagram read marks. The new tests use AF_UNIX and never combine EV_CLEAR with a write mark, so they don't reach this. Keeping the throw for this combination would cover it, together with a Linux case in GuestKqueue.cpp that requires the throw:

// AddDescriptorKnote, before WatchEdges
if ((flags & EV_CLEAR) != 0 && change.filter == EVFILT_WRITE && lowWater > 1) {
    NotImplemented_nid_no_patch("kevent: NOTE_LOWAT above 1 on an EV_CLEAR EVFILT_WRITE");
}
// ModifyKnote, in the EditEvent lambda: a plain EV_ADD replaces the mark of an EV_CLEAR knote
if (event.filter.pollFunc == PollEdge && change.filter == EVFILT_WRITE && lowWater > 1) {
    NotImplemented_nid_no_patch("kevent: NOTE_LOWAT above 1 on an EV_CLEAR EVFILT_WRITE");
}

2. The rewritten debt sentence drops two gaps that are still open

libkernel.md#L117 now says a write event fires once the free space reaches the mark, as filt_sowrite does. The first gap it drops: the mark is only compared after the host reports POLLOUT (Udp.cpp#L822). Linux reports POLLOUT for an AF_UNIX stream socket only while at most a quarter of SO_SNDBUF is in use (af_unix.c#L535-L539). For TCP it needs about a third of the buffer free (sock.h#L1373-L1377). For example, on an AF_UNIX pair with SO_SNDBUF 212992 and 45056 bytes queued unread, SO_SNDBUF less SIOCOUTQ is 159488 but POLLOUT is clear, so a mark of 2 does not fire. The old text stated this gate ("the host's writability stands in for one free byte").

The second gap: a write mark above 1 still throws on a socket that is not a stream, because the check at Kqueue.cpp#L137 now applies to EVFILT_WRITE too, but the entry no longer says so.

CONTRIBUTING.md asks an entry to state what differs and what throws, and asks a PR to cut an entry to what is still open rather than describe the corrected behaviour. For example:

A write mark above 1 is taken on stream sockets only, and not on an EV_CLEAR knote with the change above; other cases throw. It is compared with SO_SNDBUF less SIOCOUTQ on Linux and with SO_SNDBUF alone on other hosts, where the value does not shrink while data is queued. The comparison happens only while the host reports the socket writable. Linux does that for an AF_UNIX stream socket only while at most a quarter of SO_SNDBUF is in use, and for TCP only with about a third of it free, so a lower mark acts as that threshold.

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6298904840 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8596#issuecomment-6298904840). Originally posted by [nikowolf123](https://github.com/nikowolf123) on 2026-10-20T19:04:36Z. --- I checked these against 084dcf8 and replayed `GuestSockets::Ready`, `PollEdge` and the epoll wait in a standalone program on Linux 6.18 (WSL2). I did not build this branch. **1. An `EV_CLEAR` write knote with `NOTE_LOWAT` above 1 on a Linux TCP socket can wait forever** Without the throw, [`AddDescriptorKnote`](https://github.com/boykopovar/AnyPS5/blob/084dcf87363a96d42d92545a95f791e0619cb775/core/libs/prx/libkernel/Equeue/src/Kqueue.cpp#L216) gives the mark to an `EPOLLET | EPOLLOUT` edge knote. [`PollEdge`](https://github.com/boykopovar/AnyPS5/blob/084dcf87363a96d42d92545a95f791e0619cb775/core/libs/prx/libkernel/Equeue/src/Kqueue.cpp#L168) disarms that knote while the free space is below the mark. Only a new host `EPOLLOUT` wakeup or a later change of the knote arms it again. Linux TCP sends that wakeup only after a send or a poll found the socket not writable. `tcp_check_space` calls `tcp_new_space` only while `SOCK_NOSPACE` is set ([tcp_input.c#L5639-L5649](https://github.com/torvalds/linux/blob/v6.8/net/ipv4/tcp_input.c#L5639-L5649); the flag is set in [tcp.c#L565-L570](https://github.com/torvalds/linux/blob/v6.8/net/ipv4/tcp.c#L565-L570)). `sk_stream_write_space` then clears the flag on the one wakeup it sends ([stream.c#L35-L42](https://github.com/torvalds/linux/blob/v6.8/net/core/stream.c#L35-L42)). FreeBSD 11.0 calls `sowwakeup` on every ACK that frees send space ([tcp_input.c#L1808](https://github.com/freebsd/freebsd-src/blob/release/11.0.0/sys/netinet/tcp_input.c#L1808), [#L2836-L2844](https://github.com/freebsd/freebsd-src/blob/release/11.0.0/sys/netinet/tcp_input.c#L2836-L2844)). Through `KNOTE_LOCKED` ([uipc_sockbuf.c#L324](https://github.com/freebsd/freebsd-src/blob/release/11.0.0/sys/kern/uipc_sockbuf.c#L324)) that runs the `kn_data >= kn_sdata` check in `filt_sowrite` ([uipc_socket.c#L3338-L3339](https://github.com/freebsd/freebsd-src/blob/release/11.0.0/sys/kern/uipc_socket.c#L3338-L3339)). So on FreeBSD the knote fires once `sbspace` reaches the mark. Example: a loopback TCP writer has `SO_SNDBUF` 32768 and 5120 bytes unacknowledged (`POLLOUT` set, 27648 free). It adds `EV_ADD | EV_CLEAR` with `NOTE_LOWAT` 32768, and the first scan disarms the knote. After the peer has read everything (`SIOCOUTQ` 0, 32768 free), a 3 s wait still returns nothing in 3 of 3 runs, while a level knote with the same mark fires. In a second run the writer filled the buffer to `EAGAIN`. The one wakeup came at 16384 free, so a mark of 24576 never fired either, and the same happened with default buffer sizes. On `main` this input threw. Now `kevent` with a NULL timeout hangs without a message, which is what #7092 avoided by keeping the throw for datagram read marks. The new tests use `AF_UNIX` and never combine `EV_CLEAR` with a write mark, so they don't reach this. Keeping the throw for this combination would cover it, together with a Linux case in `GuestKqueue.cpp` that requires the throw: ```cpp // AddDescriptorKnote, before WatchEdges if ((flags & EV_CLEAR) != 0 && change.filter == EVFILT_WRITE && lowWater > 1) { NotImplemented_nid_no_patch("kevent: NOTE_LOWAT above 1 on an EV_CLEAR EVFILT_WRITE"); } // ModifyKnote, in the EditEvent lambda: a plain EV_ADD replaces the mark of an EV_CLEAR knote if (event.filter.pollFunc == PollEdge && change.filter == EVFILT_WRITE && lowWater > 1) { NotImplemented_nid_no_patch("kevent: NOTE_LOWAT above 1 on an EV_CLEAR EVFILT_WRITE"); } ``` **2. The rewritten debt sentence drops two gaps that are still open** [libkernel.md#L117](https://github.com/boykopovar/AnyPS5/blob/084dcf87363a96d42d92545a95f791e0619cb775/docs/dev/debt/libkernel.md#L117) now says a write event fires once the free space reaches the mark, as `filt_sowrite` does. The first gap it drops: the mark is only compared after the host reports `POLLOUT` ([Udp.cpp#L822](https://github.com/boykopovar/AnyPS5/blob/084dcf87363a96d42d92545a95f791e0619cb775/core/libs/prx/libkernel/Socket/src/Udp.cpp#L822)). Linux reports `POLLOUT` for an `AF_UNIX` stream socket only while at most a quarter of `SO_SNDBUF` is in use ([af_unix.c#L535-L539](https://github.com/torvalds/linux/blob/v6.8/net/unix/af_unix.c#L535-L539)). For TCP it needs about a third of the buffer free ([sock.h#L1373-L1377](https://github.com/torvalds/linux/blob/v6.8/include/net/sock.h#L1373-L1377)). For example, on an `AF_UNIX` pair with `SO_SNDBUF` 212992 and 45056 bytes queued unread, `SO_SNDBUF` less `SIOCOUTQ` is 159488 but `POLLOUT` is clear, so a mark of 2 does not fire. The old text stated this gate ("the host's writability stands in for one free byte"). The second gap: a write mark above 1 still throws on a socket that is not a stream, because the check at [Kqueue.cpp#L137](https://github.com/boykopovar/AnyPS5/blob/084dcf87363a96d42d92545a95f791e0619cb775/core/libs/prx/libkernel/Equeue/src/Kqueue.cpp#L137) now applies to `EVFILT_WRITE` too, but the entry no longer says so. CONTRIBUTING.md asks an entry to state what differs and what throws, and asks a PR to cut an entry to what is still open rather than describe the corrected behaviour. For example: > A write mark above 1 is taken on stream sockets only, and not on an `EV_CLEAR` knote with the change above; other cases throw. It is compared with `SO_SNDBUF` less `SIOCOUTQ` on Linux and with `SO_SNDBUF` alone on other hosts, where the value does not shrink while data is queued. The comparison happens only while the host reports the socket writable. Linux does that for an `AF_UNIX` stream socket only while at most a quarter of `SO_SNDBUF` is in use, and for TCP only with about a third of it free, so a lower mark acts as that threshold. AI-assisted: yes (Claude Code).
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!8596
No description provided.