fix(libkernel): answer kevent EV_CLEAR on Windows socket and pipe reads #15518

Merged
Opensource55 merged 2 commits from upstream-pr/15518 into main 2026-11-07 13:18:47 +00:00
Opensource55 commented 2026-11-03 17:10:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Opensource55 on 2026-11-03T17:10:29Z.


What

Waiting edge-triggered (EV_CLEAR) on a socket or a pipe with kevent called NotImplemented_nid_no_patch on Windows, so a title that waits that way died at its first wait. This adds the Windows branch of core/libs/prx/libkernel/Equeue/src/EdgeWatch.cpp. A read edge is a poll of the unread byte count through GuestSockets::Ready (FIONREAD for a socket, the free-space quota for a pipe): a growth, or a first end-of-file, reports. A write edge reports a change of the writable state, because Winsock has no send-queue query and records FD_WRITE again only after a send failed with WSAEWOULDBLOCK.

Watch::Open takes the guest descriptor as well, because the Windows branch asks GuestSockets::Ready by guest descriptor; Linux and macOS keep using the host descriptor and ignore the new argument. The Windows watch registers std::numeric_limits<int>::max() as its wait descriptor instead of the socket: that value is not a host socket, so GuestSockets::WaitAny treats it the way it treats a pipe, waits in one-millisecond slices and lets the wait loop re-evaluate the knote. Waiting on the socket itself makes every wait return while the socket stays readable and unread, which spins: 0.156 s of thread CPU over a 150 ms idle wait, against 0.000 s with the slice.

The kevent EV_CLEAR entry in docs/dev/debt/libkernel.md no longer says Windows throws. It records what stays open there: an activation that arrives while more bytes are consumed than added is missed, every edge carries up to a millisecond of delay, and the write side only tracks the writable state. For that reason EdgeTriggeredSocketWrite and SocketWriteLowWaterWhileFilling stay Linux-only, and the two assertions that expected EV_CLEAR to throw off Linux are gone.

Tested

Windows 10 22H2 (19045.6456), MinGW-W64 GCC 15.2.0 (ucrt, Release build), the repository's own guest_kqueue and guest_kqueue_cleared_race tests (ctest #3559 and #3560); no title was run.

With this change the three cases that were #ifdef __linux__ build and pass on Windows. They cover a read edge on a socket pair (3 bytes report once, a second poll reports nothing, 1 more byte reports again, re-adding with EV_ADD reports immediately, end-of-file reports once), a read edge on a pipe, and EV_CLEAR dispatch through a watched kqueue. A 150 ms idle wait with a socket knote registered costs 0.000 s of thread CPU.

ctest --test-dir build -R "^guest_kqueue" --output-on-failure: 2 of 2 passed in 7.08 s (guest_kqueue 4.60 s, guest_kqueue_cleared_race 2.26 s), so the whole file-event, socket and pipe suite passes on Windows with this change.

Red side: with EdgeWatch.cpp, EdgeWatch.hpp and Kqueue.cpp taken from main and the tests kept, the same run throws kevent: EV_CLEAR on a socket or a pipe off Linux not implemented.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5141426315 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15518). Originally posted by [Opensource55](https://github.com/Opensource55) on 2026-11-03T17:10:29Z. --- ### What Waiting edge-triggered (`EV_CLEAR`) on a socket or a pipe with `kevent` called `NotImplemented_nid_no_patch` on Windows, so a title that waits that way died at its first wait. This adds the Windows branch of `core/libs/prx/libkernel/Equeue/src/EdgeWatch.cpp`. A read edge is a poll of the unread byte count through `GuestSockets::Ready` (`FIONREAD` for a socket, the free-space quota for a pipe): a growth, or a first end-of-file, reports. A write edge reports a change of the writable state, because Winsock has no send-queue query and records `FD_WRITE` again only after a send failed with `WSAEWOULDBLOCK`. `Watch::Open` takes the guest descriptor as well, because the Windows branch asks `GuestSockets::Ready` by guest descriptor; Linux and macOS keep using the host descriptor and ignore the new argument. The Windows watch registers `std::numeric_limits<int>::max()` as its wait descriptor instead of the socket: that value is not a host socket, so `GuestSockets::WaitAny` treats it the way it treats a pipe, waits in one-millisecond slices and lets the wait loop re-evaluate the knote. Waiting on the socket itself makes every wait return while the socket stays readable and unread, which spins: 0.156 s of thread CPU over a 150 ms idle wait, against 0.000 s with the slice. The `kevent EV_CLEAR` entry in `docs/dev/debt/libkernel.md` no longer says Windows throws. It records what stays open there: an activation that arrives while more bytes are consumed than added is missed, every edge carries up to a millisecond of delay, and the write side only tracks the writable state. For that reason `EdgeTriggeredSocketWrite` and `SocketWriteLowWaterWhileFilling` stay Linux-only, and the two assertions that expected `EV_CLEAR` to throw off Linux are gone. ### Tested Windows 10 22H2 (19045.6456), MinGW-W64 GCC 15.2.0 (ucrt, Release build), the repository's own `guest_kqueue` and `guest_kqueue_cleared_race` tests (ctest #3559 and #3560); no title was run. With this change the three cases that were `#ifdef __linux__` build and pass on Windows. They cover a read edge on a socket pair (3 bytes report once, a second poll reports nothing, 1 more byte reports again, re-adding with `EV_ADD` reports immediately, end-of-file reports once), a read edge on a pipe, and `EV_CLEAR` dispatch through a watched kqueue. A 150 ms idle wait with a socket knote registered costs 0.000 s of thread CPU. `ctest --test-dir build -R "^guest_kqueue" --output-on-failure`: 2 of 2 passed in 7.08 s (`guest_kqueue` 4.60 s, `guest_kqueue_cleared_race` 2.26 s), so the whole file-event, socket and pipe suite passes on Windows with this change. Red side: with `EdgeWatch.cpp`, `EdgeWatch.hpp` and `Kqueue.cpp` taken from `main` and the tests kept, the same run throws `kevent: EV_CLEAR on a socket or a pipe off Linux not implemented`. ### Checklist - [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
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!15518
No description provided.