fix(libkernel): report a socket shut for reading in poll and select and wake their waiters #11223

Merged
Ragbie merged 5 commits from upstream-pr/11223 into main 2026-11-05 04:28:29 +00:00
Ragbie commented 2026-10-25 22:07:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Ragbie on 2026-10-25T22:07:12Z.


What

Follow-up to #10767 for poll and select. On Windows, a guest socket whose receive side is shut does not look readable to them, and a thread already waiting is not woken:

  • With nothing queued, poll reports nothing after shutdown(SHUT_RD) and select leaves the socket out of the read set. Winsock's WSAPoll reports nothing after SD_RECEIVE.
  • With bytes queued before the shutdown, poll reports POLLHUP | POLLERR without POLLIN, because Winsock resets the loopback connection.
  • A thread blocked in poll when another thread shuts the socket sleeps until its timeout, or forever with -1. A select waiter notices only at its next internal slice.
  • After shutdown(SHUT_RDWR), poll reports POLLIN | POLLOUT without POLLHUP.

FreeBSD 11.4 pollscan also clears POLLOUT whenever it reports POLLHUP, which #9512 already does on Linux.

FreeBSD 11.4 sopoll_generic adds the requested POLLIN | POLLRDNORM once the receive side is SBS_CANTRCVMORE, and POLLHUP when the send side is SBS_CANTSENDMORE too. select uses soreadable, which includes that state. sorflush → socantrcvmore → sorwakeup wakes every poller of the socket, whichever descriptor it polls through.

The change, in core/libs/prx/libkernel/Socket/src/Udp.cpp:

  • poll reports a guest socket whose receive side is shut as readable for the requested POLLIN / POLLRDNORM. It adds POLLHUP, and drops POLLOUT, when both sides are shut. Such a socket makes the call return at once.
  • GuestSockets::Poll, which select uses for kernel sockets, reports the same socket as readable.
  • Both register the calling thread's existing waker socket (the one equeue waits use) for the descriptors they poll for reading, and add it to the host poll set. shutdown wakes the waiters of every descriptor that refers to the socket (dup, dup2, F_DUPFD). poll waits only for the time left, and polls again when only the waker fired.
  • Technical debt: the SHUT_RD entry notes the extra POLLHUP | POLLERR the Windows reset produces, and #9512's poll on a stream socket entry now covers the Windows both-shut case and FreeBSD's pollrescan, which keeps POLLOUT for a socket that hangs up during the wait.

Tested

Windows 11, WinLibs GCC 15.2.0, and Linux (Ubuntu 24.04 container, GCC 13.3.0), on main 6cc5ec671 with #10767's commits below this PR's two:

  • Rebased onto main 73f4f7e06 without conflicts. On this head, on Windows, guest_poll, guest_select, guest_kqueue and guest_select_wait pass with --repeat until-fail:5, and the 157 tests matching socket|tcp|udp|kqueue|equeue|guest_net|select|poll|net_|recv|send|shutdown (without the 12 select_tests_* tests of the test-selection tool, which the pattern also matches) with --repeat until-fail:3. On Linux the same four pass with --repeat until-fail:5 and the 117 matching tests with --repeat until-fail:3. Earlier, on main c547deaac, the only rebase conflict was the wait condition in poll_nid_postfix, where #9509 added the macOS urgent-data check; both conditions are kept, and outside macOS that check is always false. The results below are from the earlier base 6cc5ec671.

  • guest_poll: readability after SHUT_RD with and without queued bytes; a waiter woken within the file's usual bounds (at least 40 ms, under 4 s, 5 s timeout) through the descriptor itself and through a dup of it; SHUT_RDWR gives exactly POLLIN | POLLHUP.

  • guest_select: readability after SHUT_RD, and a waiter. With select's own slices this case cannot tell the waker from the next slice; the poll waiter does.

  • Without the fix, guest_poll and guest_select fail on Windows and pass on Linux, where the host already reports and wakes. With it, guest_poll, guest_select, guest_kqueue and guest_select_wait pass with --repeat until-fail:5, and the tests matching socket|tcp|udp|kqueue|equeue|guest_net|select|poll|net_ pass with --repeat until-fail:3 (116 on Windows, 78 on Linux).

  • Windows full build and ctest: 99% tests passed, 5 tests failed out of 2394. agc_write_tracking_metadata_commit, agc_host_import_cut_lifetime, agc_driver_depth_plane_partial_store and agc_driver_image_plane_address_on_line fail on main on this machine too, and so does guest_wide_formatting (%Z gives the host's non-ASCII zone name here; #10981 and #10987 address it). check_skips.py: all skips expected.

  • Linux full ctest: 99% tests passed, 24 tests failed out of 2296; the same 24 fail on main in this container (22 agc_* on its lavapipe and kernel without UFFD_FEATURE_WP_ASYNC, pr_overlap_encoding and guest_system_load_exec_standard_error).

  • macOS: not tested.

Not covered here: a thread already blocked in a host recv on Windows is not released by SHUT_RD from another thread, where FreeBSD's sorflush wakes it and the receive returns 0. That changes the blocking receive path and goes in its own pull request.

Overlaps: #7090 adds entries next to this PR's in docs/dev/debt/libkernel.md; since #10767 merged that is the only conflicting file. Neither needs the other: whichever lands second resolves it and keeps both entries.

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:5009192321 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11223). Originally posted by [Ragbie](https://github.com/Ragbie) on 2026-10-25T22:07:12Z. --- ### What Follow-up to #10767 for `poll` and `select`. On Windows, a guest socket whose receive side is shut does not look readable to them, and a thread already waiting is not woken: - With nothing queued, `poll` reports nothing after `shutdown(SHUT_RD)` and `select` leaves the socket out of the read set. Winsock's `WSAPoll` reports nothing after `SD_RECEIVE`. - With bytes queued before the shutdown, `poll` reports `POLLHUP | POLLERR` without `POLLIN`, because Winsock resets the loopback connection. - A thread blocked in `poll` when another thread shuts the socket sleeps until its timeout, or forever with `-1`. A `select` waiter notices only at its next internal slice. - After `shutdown(SHUT_RDWR)`, `poll` reports `POLLIN | POLLOUT` without `POLLHUP`. FreeBSD 11.4 [`pollscan`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/sys_generic.c#L1592-L1598) also clears `POLLOUT` whenever it reports `POLLHUP`, which #9512 already does on Linux. FreeBSD 11.4 [`sopoll_generic`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_socket.c#L3060-L3103) adds the requested `POLLIN | POLLRDNORM` once the receive side is `SBS_CANTRCVMORE`, and `POLLHUP` when the send side is `SBS_CANTSENDMORE` too. `select` uses [`soreadable`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/sys/socketvar.h#L216-L217), which includes that state. `sorflush` → `socantrcvmore` → `sorwakeup` wakes every poller of the socket, whichever descriptor it polls through. The change, in `core/libs/prx/libkernel/Socket/src/Udp.cpp`: - `poll` reports a guest socket whose receive side is shut as readable for the requested `POLLIN` / `POLLRDNORM`. It adds `POLLHUP`, and drops `POLLOUT`, when both sides are shut. Such a socket makes the call return at once. - `GuestSockets::Poll`, which `select` uses for kernel sockets, reports the same socket as readable. - Both register the calling thread's existing waker socket (the one equeue waits use) for the descriptors they poll for reading, and add it to the host poll set. `shutdown` wakes the waiters of every descriptor that refers to the socket (`dup`, `dup2`, `F_DUPFD`). `poll` waits only for the time left, and polls again when only the waker fired. - Technical debt: the `SHUT_RD` entry notes the extra `POLLHUP | POLLERR` the Windows reset produces, and #9512's `poll on a stream socket` entry now covers the Windows both-shut case and FreeBSD's `pollrescan`, which keeps `POLLOUT` for a socket that hangs up during the wait. ### Tested Windows 11, WinLibs GCC 15.2.0, and Linux (Ubuntu 24.04 container, GCC 13.3.0), on `main` `6cc5ec671` with #10767's commits below this PR's two: - Rebased onto `main` `73f4f7e06` without conflicts. On this head, on Windows, `guest_poll`, `guest_select`, `guest_kqueue` and `guest_select_wait` pass with `--repeat until-fail:5`, and the 157 tests matching `socket|tcp|udp|kqueue|equeue|guest_net|select|poll|net_|recv|send|shutdown` (without the 12 `select_tests_*` tests of the test-selection tool, which the pattern also matches) with `--repeat until-fail:3`. On Linux the same four pass with `--repeat until-fail:5` and the 117 matching tests with `--repeat until-fail:3`. Earlier, on `main` `c547deaac`, the only rebase conflict was the wait condition in `poll_nid_postfix`, where #9509 added the macOS urgent-data check; both conditions are kept, and outside macOS that check is always false. The results below are from the earlier base `6cc5ec671`. - `guest_poll`: readability after `SHUT_RD` with and without queued bytes; a waiter woken within the file's usual bounds (at least 40 ms, under 4 s, 5 s timeout) through the descriptor itself and through a `dup` of it; `SHUT_RDWR` gives exactly `POLLIN | POLLHUP`. - `guest_select`: readability after `SHUT_RD`, and a waiter. With select's own slices this case cannot tell the waker from the next slice; the `poll` waiter does. - Without the fix, `guest_poll` and `guest_select` fail on Windows and pass on Linux, where the host already reports and wakes. With it, `guest_poll`, `guest_select`, `guest_kqueue` and `guest_select_wait` pass with `--repeat until-fail:5`, and the tests matching `socket|tcp|udp|kqueue|equeue|guest_net|select|poll|net_` pass with `--repeat until-fail:3` (116 on Windows, 78 on Linux). - Windows full build and `ctest`: `99% tests passed, 5 tests failed out of 2394`. `agc_write_tracking_metadata_commit`, `agc_host_import_cut_lifetime`, `agc_driver_depth_plane_partial_store` and `agc_driver_image_plane_address_on_line` fail on `main` on this machine too, and so does `guest_wide_formatting` (`%Z` gives the host's non-ASCII zone name here; #10981 and #10987 address it). `check_skips.py`: all skips expected. - Linux full `ctest`: `99% tests passed, 24 tests failed out of 2296`; the same 24 fail on `main` in this container (22 `agc_*` on its lavapipe and kernel without `UFFD_FEATURE_WP_ASYNC`, `pr_overlap_encoding` and `guest_system_load_exec_standard_error`). - macOS: not tested. Not covered here: a thread already blocked in a host `recv` on Windows is not released by `SHUT_RD` from another thread, where FreeBSD's `sorflush` wakes it and the receive returns 0. That changes the blocking receive path and goes in its own pull request. Overlaps: #7090 adds entries next to this PR's in `docs/dev/debt/libkernel.md`; since #10767 merged that is the only conflicting file. Neither needs the other: whichever lands second resolves it and keeps both entries. ### 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
MarcZX10 commented 2026-10-25 22:18:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T22:18:19Z.

Review: APPROVED


Reviewed exact head 649a0d231dee80ad6792617ca5fa1353a5a73b3b. I found no actionable issue in the shutdown state propagation or poll/select/kqueue waiter wakeups, including duplicated socket descriptors. Local git diff --check and tools/check_conventions.py pass against current main. The PR reports focused Windows and Linux test runs; the hosted checks currently cover conventions, intake and labels only, with product builds skipped by repository policy. I did not run a local product build in this relinker-only workspace.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5645631227 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11223#pullrequestreview-5645631227). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T22:18:19Z. **Review:** APPROVED --- Reviewed exact head `649a0d231dee80ad6792617ca5fa1353a5a73b3b`. I found no actionable issue in the shutdown state propagation or poll/select/kqueue waiter wakeups, including duplicated socket descriptors. Local `git diff --check` and `tools/check_conventions.py` pass against current `main`. The PR reports focused Windows and Linux test runs; the hosted checks currently cover conventions, intake and labels only, with product builds skipped by repository policy. I did not run a local product build in this relinker-only workspace.
MarcZX10 commented 2026-10-26 06:42:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-26T06:42:57Z.

Review: DISMISSED


Re-reviewed the updated exact head e36c33c82503fe956b889187e1c10bbe71b0d663. The shutdown handling and duplicated-descriptor wakeup changes have substantial Windows/Linux regression evidence in the latest report; I did not rerun a product build locally. One merge-order blocker remains: the exact merge preview with open #7090 conflicts in core/libs/prx/libkernel/Socket/src/Udp.cpp and docs/dev/debt/libkernel.md; #7090 implements Unix SCM_RIGHTS in recvmsg, while this PR also changes that receive path and the same debt file. The preview with open #11006 conflicts in docs/dev/debt/libkernel.md. Please coordinate with both authors on which branch lands first, preserve both changes, and declare any required prerequisite on the checklist Depends on line. The existing Depends on: #10767 and #11141’s reverse dependency do not capture these two overlaps. Current hosted checks do not run product builds for this path; the independent report is local evidence.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5648272242 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11223#pullrequestreview-5648272242). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-26T06:42:57Z. **Review:** DISMISSED --- Re-reviewed the updated exact head `e36c33c82503fe956b889187e1c10bbe71b0d663`. The shutdown handling and duplicated-descriptor wakeup changes have substantial Windows/Linux regression evidence in the latest report; I did not rerun a product build locally. One merge-order blocker remains: the exact merge preview with open #7090 conflicts in `core/libs/prx/libkernel/Socket/src/Udp.cpp` and `docs/dev/debt/libkernel.md`; #7090 implements Unix `SCM_RIGHTS` in `recvmsg`, while this PR also changes that receive path and the same debt file. The preview with open #11006 conflicts in `docs/dev/debt/libkernel.md`. Please coordinate with both authors on which branch lands first, preserve both changes, and declare any required prerequisite on the checklist `Depends on` line. The existing `Depends on: #10767` and #11141’s reverse dependency do not capture these two overlaps. Current hosted checks do not run product builds for this path; the independent report is local evidence.
MarcZX10 commented 2026-10-29 10:13:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T10:13:44Z.

Review: DISMISSED


Re-reviewed exact head 92efd5c137e1b32c6a7c1cd5215207cf2949eeb4 against current main f99d7fece3.

The #7090 overlap is resolved: both authors agreed that whichever PR lands second preserves both recvmsg changes, and the current merge preview is clean. I am no longer treating that as a blocker.

One integration check remains. The PR's latest reported Windows/Linux runs are on e36c33c82 merged with 0f34ebb85, before this head's rebase to c547deaac. Current main has since changed Udp.cpp's poll collection and GuestPoll.cpp's peer-closed-stream expectations (including Apple-specific handling). The exact merge preview with current main is clean, but I have not run the product tests locally. Please report the focused guest_poll, guest_select, and waiter/socket tests on this head integrated with current main, including Linux and Windows results; include macOS only if available. Once that evidence is available, I can clear the remaining integration concern.

Local checks: current-main merge preview clean; git diff --check passes. Hosted Windows passes; Linux and specialized product jobs are skipped by policy. No local product build or CTest run was performed.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5696056700 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11223#pullrequestreview-5696056700). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-29T10:13:44Z. **Review:** DISMISSED --- Re-reviewed exact head `92efd5c137e1b32c6a7c1cd5215207cf2949eeb4` against current `main` `f99d7fece3`. The #7090 overlap is resolved: both authors agreed that whichever PR lands second preserves both `recvmsg` changes, and the current merge preview is clean. I am no longer treating that as a blocker. One integration check remains. The PR's latest reported Windows/Linux runs are on `e36c33c82` merged with `0f34ebb85`, before this head's rebase to `c547deaac`. Current `main` has since changed `Udp.cpp`'s poll collection and `GuestPoll.cpp`'s peer-closed-stream expectations (including Apple-specific handling). The exact merge preview with current `main` is clean, but I have not run the product tests locally. Please report the focused `guest_poll`, `guest_select`, and waiter/socket tests on this head integrated with current `main`, including Linux and Windows results; include macOS only if available. Once that evidence is available, I can clear the remaining integration concern. Local checks: current-main merge preview clean; `git diff --check` passes. Hosted Windows passes; Linux and specialized product jobs are skipped by policy. No local product build or CTest run was performed.
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!11223
No description provided.