fix(libkernel): poll kqueues as kqueue_poll answers #7605

Merged
HQasper merged 1 commit from upstream-pr/7605 into main 2026-10-24 10:32:34 +00:00
HQasper commented 2026-10-18 16:41:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by HQasper on 2026-10-18T16:41:11Z.


What

Follow-up to #7248 (merged). poll threw for a kqueue descriptor on both hosts ("neither a socket, a pipe, a file nor a directory"), and #7139's guest_poll used that as its example of an unsupported descriptor.

  • A kqueue is ready for POLLIN and POLLRDNORM while one of its events is triggered, and never for any other event, as FreeBSD 11.4's kqueue_poll answers. It uses #7248's KqueueReadable.
  • A kqueue cannot be waited on, so while one is polled, poll checks it every millisecond and waits on the other descriptors in slices of 1 ms. This is the loop Windows already used for pipes. Linux now runs the same loop with ::poll in place of WSAPoll. Without a kqueue it makes a single host call with the full timeout, as before.
  • A kqueue closed during the wait reports POLLNVAL. One whose number was reused is polled again with the rest of the timeout, as FreeBSD 11.4's pollrescan looks the descriptor up again.
  • docs/dev/debt/libkernel.md: the poll entry describes the above, including the same close window as select in #7248. A close that has removed the kqueue but not yet closed its host descriptor is seen as an unknown descriptor, which throws.
  • poll_nid_postfix was already counted as implemented, so tools/progress.py does not change.

Tested

Windows 11 Pro 26300, MinGW-w64 (WinLibs) Release build of this branch on main 81cb86339. guest_poll, guest_select, guest_select_wait, guest_kqueue, guest_equeue_events and every guest_socket* test pass. The new kqueue cases in guest_poll cover:

  • an empty kqueue, then one with an EVFILT_USER event added with EV_CLEAR: not ready, also over a 30 ms wait;
  • the event triggered from another thread during an infinite poll next to a socket: only the kqueue is reported, with POLLIN;
  • POLLIN | POLLRDNORM reported while the event is pending, nothing once kevent has collected it;
  • POLLNVAL after close.

Without the Udp.cpp change guest_poll fails (the kqueue throws, exit 0xc0000409).

Full ctest -j 24: 1489 of 1490 pass. The one failure, agc_driver_pixel_lds, is unrelated: it loses the device on this RTX 5090 (NVIDIA 617.42) on plain main too.

Linux not tested.

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:4910878611 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7605). Originally posted by [HQasper](https://github.com/HQasper) on 2026-10-18T16:41:11Z. --- ### What Follow-up to #7248 (merged). `poll` threw for a kqueue descriptor on both hosts ("neither a socket, a pipe, a file nor a directory"), and #7139's `guest_poll` used that as its example of an unsupported descriptor. - A kqueue is ready for `POLLIN` and `POLLRDNORM` while one of its events is triggered, and never for any other event, as FreeBSD 11.4's [`kqueue_poll`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/kern_event.c#L1900-L1924) answers. It uses #7248's `KqueueReadable`. - A kqueue cannot be waited on, so while one is polled, `poll` checks it every millisecond and waits on the other descriptors in slices of 1 ms. This is the loop Windows already used for pipes. Linux now runs the same loop with `::poll` in place of `WSAPoll`. Without a kqueue it makes a single host call with the full timeout, as before. - A kqueue closed during the wait reports `POLLNVAL`. One whose number was reused is polled again with the rest of the timeout, as FreeBSD 11.4's [`pollrescan`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/sys_generic.c#L1499-L1519) looks the descriptor up again. - `docs/dev/debt/libkernel.md`: the `poll` entry describes the above, including the same close window as `select` in #7248. A close that has removed the kqueue but not yet closed its host descriptor is seen as an unknown descriptor, which throws. - `poll_nid_postfix` was already counted as implemented, so `tools/progress.py` does not change. ### Tested Windows 11 Pro 26300, MinGW-w64 (WinLibs) Release build of this branch on `main` 81cb86339. `guest_poll`, `guest_select`, `guest_select_wait`, `guest_kqueue`, `guest_equeue_events` and every `guest_socket*` test pass. The new kqueue cases in `guest_poll` cover: - an empty kqueue, then one with an `EVFILT_USER` event added with `EV_CLEAR`: not ready, also over a 30 ms wait; - the event triggered from another thread during an infinite `poll` next to a socket: only the kqueue is reported, with `POLLIN`; - `POLLIN | POLLRDNORM` reported while the event is pending, nothing once `kevent` has collected it; - `POLLNVAL` after `close`. Without the `Udp.cpp` change `guest_poll` fails (the kqueue throws, exit `0xc0000409`). Full `ctest -j 24`: 1489 of 1490 pass. The one failure, `agc_driver_pixel_lds`, is unrelated: it loses the device on this RTX 5090 (NVIDIA 617.42) on plain `main` too. Linux not tested. ### 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
wachi35676 commented 2026-10-19 00:12:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-19T00:12:56Z.


Adding Linux, which the description lists as untested, since the Linux poll path now goes through the shared loop. Tested at 36e9b0b3e on main 8a456334f.

  • WSL Ubuntu 24.04, gcc 13.3: guest_poll, guest_select, guest_select_wait, guest_kqueue, guest_equeue* and guest_socket* (16 tests) pass. With Udp.cpp reverted to main, guest_poll aborts there too.
  • Windows 11 / GCC 15.2: 30 tests pass, and guest_poll passes 5/5. The negative control aborts with poll: descriptor 4 is neither a socket, a pipe, a file nor a directory.

The readiness rule matches FreeBSD 11.4's kqueue_poll (POLLIN|POLLRDNORM while kq_count != 0).

AI-assisted: yes (Claude Code); results above were run on this machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6256916219 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7605#issuecomment-6256916219). Originally posted by [wachi35676](https://github.com/wachi35676) on 2026-10-19T00:12:56Z. --- Adding Linux, which the description lists as untested, since the Linux `poll` path now goes through the shared loop. Tested at `36e9b0b3e` on `main` `8a456334f`. - **WSL Ubuntu 24.04, gcc 13.3:** `guest_poll`, `guest_select`, `guest_select_wait`, `guest_kqueue`, `guest_equeue*` and `guest_socket*` (16 tests) pass. With `Udp.cpp` reverted to `main`, `guest_poll` aborts there too. - **Windows 11 / GCC 15.2:** 30 tests pass, and `guest_poll` passes 5/5. The negative control aborts with `poll: descriptor 4 is neither a socket, a pipe, a file nor a directory`. The readiness rule matches FreeBSD 11.4's `kqueue_poll` (`POLLIN|POLLRDNORM` while `kq_count != 0`). AI-assisted: yes (Claude Code); results above were run on this machine.
EagleBearerYS commented 2026-10-22 00:40:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-22T00:40:55Z.


@HQasper a merge-order note: #9512 adds a poll on a stream socket entry to docs/dev/debt/libkernel.md right after the poll entry this PR rewrites; the code changes merge cleanly. This one goes first: #9512 lists it under Depends on and rebases the debt file on it.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6329834959 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7605#issuecomment-6329834959). Originally posted by [EagleBearerYS](https://github.com/EagleBearerYS) on 2026-10-22T00:40:55Z. --- @HQasper a merge-order note: #9512 adds a `poll on a stream socket` entry to `docs/dev/debt/libkernel.md` right after the `poll` entry this PR rewrites; the code changes merge cleanly. This one goes first: #9512 lists it under Depends on and rebases the debt file on it.
EagleBearerYS commented 2026-10-22 02:18:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-22T02:18:57Z.


@HQasper one more merge-order note: #9492 adds a line after the WSAPoll call in poll, next to this PR's change to the same loop for kqueues. This one goes first here too: #9492 lists it under Depends on and rebases on it.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6331103253 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7605#issuecomment-6331103253). Originally posted by [EagleBearerYS](https://github.com/EagleBearerYS) on 2026-10-22T02:18:57Z. --- @HQasper one more merge-order note: #9492 adds a line after the `WSAPoll` call in `poll`, next to this PR's change to the same loop for kqueues. This one goes first here too: #9492 lists it under Depends on and rebases on it.
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!7605
No description provided.