fix(libSceNet): report a stream whose peer closed as readable on Windows #9493

Merged
EagleBearerYS merged 3 commits from upstream-pr/9493 into main 2026-10-25 17:22:52 +00:00
EagleBearerYS commented 2026-10-21 23:21:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-21T23:21:24Z.


Opened again from #5376: title-less conformance fixes of this kind are merged now, for example #7365 and #8411.

What

When the peer of a stream socket closes, Winsock's WSAPoll reports POLLHUP without POLLRDNORM, whether or not the socket's own sending side is shut down. sceNetEpollWait passes that on, so on Windows a socket whose peer has closed comes back as SCE_NET_EPOLLHUP alone and never as readable. A loop that waits for SCE_NET_EPOLLIN to read the end of the stream never gets it, while every wait returns at once.

FreeBSD's sopoll_generic reports a socket that can receive no more as readable, and adds POLLHUP only once it can send no more either. Linux's poll does the same, so sceNetEpollWait already reports it this way on Linux.

On Windows, a stream socket's POLLHUP without POLLERR now reports POLLRDNORM when reading was asked for. It keeps POLLHUP only after sceNetShutdown has shut the socket's own sending side, which libSceNet now records as sending_shut, next to the read_shut that main records.

Unchanged:

  • A reset or refused connection (POLLHUP with POLLERR) keeps the flags WSAPoll reports.
  • sceNetSocketAbort shuts the host socket down without being recorded as a shutdown.
  • libkernel's poll gets the same change for its own stream sockets in #9492.
  • select already counts a hang-up as readable.

No title is claimed.

Overlap: #8598, now on main, changed how sceNetEpollWait captures its registrations: each one is a Watch that holds the registration and the socket's handle. The merge of main into this branch (0a01fca94f) keeps that, with each Watch holding the copy of the socket's Sock this PR reads, whose handle is the same shared_ptr.

Tested

On GitHub's runners from my fork, merged with main at a6dcea9279 (run):

The new CheckEpollClosedStream in guest_sce_net checks three states in order:

  • after the peer closes, sceNetEpollWait reports exactly SCE_NET_EPOLLIN;
  • once the end of the stream is read, it reports SCE_NET_EPOLLIN | SCE_NET_EPOLLOUT when both are asked for;
  • after sceNetShutdown of the sending side, it reports SCE_NET_EPOLLIN | SCE_NET_EPOLLHUP.

Results:

  • Windows: with main's Export.cpp guest_sce_net fails. With the change, it passes 10 runs in a row.
  • Linux: guest_sce_net passes with main's code and, 10 runs in a row, with the change.
  • Windows: every test registered under core/libs/prx/libc (142) and core/libs/prx/libkernel (207) passes. Linux: libkernel (197) passes; in libc (115), the timing test precise_wait failed with and without the change in this run.
  • After the merge with main at 17f5335562 (run): guest_sce_net and #8598's eight net_epoll_lifetime_* tests pass 10 runs in a row on both runners, and with main's Export.cpp guest_sce_net fails on Windows. The libc and libkernel suites pass on Windows (143, 207) and libkernel on Linux (197); precise_wait failed on Linux with and without the change again.
  • tools/check_conventions.py --base origin/main passes.
  • Not tested: macOS, a console, any title.

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:4959678585 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9493). Originally posted by [EagleBearerYS](https://github.com/EagleBearerYS) on 2026-10-21T23:21:24Z. --- Opened again from #5376: title-less conformance fixes of this kind are merged now, for example #7365 and #8411. ### What When the peer of a stream socket closes, Winsock's `WSAPoll` reports `POLLHUP` without `POLLRDNORM`, whether or not the socket's own sending side is shut down. `sceNetEpollWait` passes that on, so on Windows a socket whose peer has closed comes back as `SCE_NET_EPOLLHUP` alone and never as readable. A loop that waits for `SCE_NET_EPOLLIN` to read the end of the stream never gets it, while every wait returns at once. FreeBSD's [`sopoll_generic`](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/sys/kern/uipc_socket.c#L3080-L3086) reports a socket that can receive no more as readable, and adds `POLLHUP` only once it can send no more either. Linux's `poll` does the same, so `sceNetEpollWait` already reports it this way on Linux. On Windows, a stream socket's `POLLHUP` without `POLLERR` now reports `POLLRDNORM` when reading was asked for. It keeps `POLLHUP` only after `sceNetShutdown` has shut the socket's own sending side, which libSceNet now records as `sending_shut`, next to the `read_shut` that `main` records. Unchanged: - A reset or refused connection (`POLLHUP` with `POLLERR`) keeps the flags `WSAPoll` reports. - `sceNetSocketAbort` shuts the host socket down without being recorded as a shutdown. - libkernel's `poll` gets the same change for its own stream sockets in #9492. - `select` already counts a hang-up as readable. No title is claimed. **Overlap:** #8598, now on `main`, changed how `sceNetEpollWait` captures its registrations: each one is a `Watch` that holds the registration and the socket's handle. The merge of `main` into this branch (0a01fca94f) keeps that, with each `Watch` holding the copy of the socket's `Sock` this PR reads, whose handle is the same `shared_ptr`. ### Tested On GitHub's runners from my fork, merged with `main` at a6dcea9279 ([run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/39688498954)): The new `CheckEpollClosedStream` in `guest_sce_net` checks three states in order: - after the peer closes, `sceNetEpollWait` reports exactly `SCE_NET_EPOLLIN`; - once the end of the stream is read, it reports `SCE_NET_EPOLLIN | SCE_NET_EPOLLOUT` when both are asked for; - after `sceNetShutdown` of the sending side, it reports `SCE_NET_EPOLLIN | SCE_NET_EPOLLHUP`. Results: - Windows: with `main`'s `Export.cpp` `guest_sce_net` fails. With the change, it passes 10 runs in a row. - Linux: `guest_sce_net` passes with `main`'s code and, 10 runs in a row, with the change. - Windows: every test registered under `core/libs/prx/libc` (142) and `core/libs/prx/libkernel` (207) passes. Linux: libkernel (197) passes; in libc (115), the timing test `precise_wait` failed with and without the change in this run. - After the merge with `main` at 17f5335562 ([run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/39724057024)): `guest_sce_net` and #8598's eight `net_epoll_lifetime_*` tests pass 10 runs in a row on both runners, and with `main`'s `Export.cpp` `guest_sce_net` fails on Windows. The libc and libkernel suites pass on Windows (143, 207) and libkernel on Linux (197); `precise_wait` failed on Linux with and without the change again. - `tools/check_conventions.py --base origin/main` passes. - Not tested: macOS, a console, any title. ### 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-21 23:27:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T23:27:47Z.

Review: APPROVED


Reviewed the Windows epoll translation and regression test. The change limits the adjustment to stream sockets reporting POLLHUP without POLLERR, adds the read bit only when requested, and retains hang-up after the local sending side is shut down. The test covers peer close, EOF, requested writable readiness and the subsequent local shutdown state. The reported Windows and Linux focused suites pass; conventions, intake, eligibility, labels and report checks pass, while hosted product build jobs are skipped.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5607469687 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9493#pullrequestreview-5607469687). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-21T23:27:47Z. **Review:** APPROVED --- Reviewed the Windows epoll translation and regression test. The change limits the adjustment to stream sockets reporting `POLLHUP` without `POLLERR`, adds the read bit only when requested, and retains hang-up after the local sending side is shut down. The test covers peer close, EOF, requested writable readiness and the subsequent local shutdown state. The reported Windows and Linux focused suites pass; conventions, intake, eligibility, labels and report checks pass, while hosted product build jobs are skipped.
MarcZX10 commented 2026-10-22 02:11:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T02:11:29Z.

Review: APPROVED


Re-reviewed the current head and manually checked the shared-file warnings. #8598 changes registration lifetime and snapshots; it composes with this PR's event translation, with the documented small rebase needed if both merge. The other shared-file warnings concern unrelated exports or tests. The Windows mapping is limited to stream without , only sets read readiness when requested, and preserves the local-send-shutdown state. Reported Windows/Linux tests exercise those states. Hosted policy checks pass; product and relinker jobs are skipped. I did not run local tests.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5608483280 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9493#pullrequestreview-5608483280). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T02:11:29Z. **Review:** APPROVED --- Re-reviewed the current head and manually checked the shared-file warnings. #8598 changes registration lifetime and snapshots; it composes with this PR's event translation, with the documented small rebase needed if both merge. The other shared-file warnings concern unrelated exports or tests. The Windows mapping is limited to stream without , only sets read readiness when requested, and preserves the local-send-shutdown state. Reported Windows/Linux tests exercise those states. Hosted policy checks pass; product and relinker jobs are skipped. I did not run local tests.
MarcZX10 commented 2026-10-22 02:13:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T02:13:07Z.

Review: APPROVED


Re-reviewed the current head and manually checked the shared-file warnings. #8598 changes registration lifetime and snapshots; it composes with this PR's event translation, with the documented small rebase needed if both merge. The other shared-file warnings concern unrelated exports or tests. The Windows mapping is limited to stream sockets reporting POLLHUP without POLLERR, only sets read readiness when requested, and preserves the local-send-shutdown state. Reported Windows/Linux tests exercise those states. Hosted policy checks pass; product and relinker jobs are skipped. I did not run local tests.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5608492786 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9493#pullrequestreview-5608492786). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T02:13:07Z. **Review:** APPROVED --- Re-reviewed the current head and manually checked the shared-file warnings. #8598 changes registration lifetime and snapshots; it composes with this PR's event translation, with the documented small rebase needed if both merge. The other shared-file warnings concern unrelated exports or tests. The Windows mapping is limited to stream sockets reporting `POLLHUP` without `POLLERR`, only sets read readiness when requested, and preserves the local-send-shutdown state. Reported Windows/Linux tests exercise those states. Hosted policy checks pass; product and relinker jobs are skipped. I did not run local tests.
MarcZX10 commented 2026-10-22 04:27:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T04:27:01Z.

Review: DISMISSED


The Windows stream event translation is narrowly scoped to POLLHUP without POLLERR; it adds requested read readiness, preserves POLLERR, and retains hang-up after local send shutdown. The regression checks peer close, EOF, requested writable readiness and local shutdown. The #8598 registration-snapshot overlap is documented as composable with a rebase if needed. The branch and reported tests are based on a6dcea9279, while current upstream main is ab347d144d. Please rebase and rerun guest_sce_net and conventions on current main, then refresh the overlap scan and preserve #8598’s registration changes. Hosted product jobs are skipped; I did not run local tests.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5609258876 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9493#pullrequestreview-5609258876). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T04:27:01Z. **Review:** DISMISSED --- The Windows stream event translation is narrowly scoped to `POLLHUP` without `POLLERR`; it adds requested read readiness, preserves `POLLERR`, and retains hang-up after local send shutdown. The regression checks peer close, EOF, requested writable readiness and local shutdown. The #8598 registration-snapshot overlap is documented as composable with a rebase if needed. The branch and reported tests are based on a6dcea9279, while current upstream main is ab347d144d88fae054d9770cf0db8d3565735c02. Please rebase and rerun `guest_sce_net` and conventions on current main, then refresh the overlap scan and preserve #8598’s registration changes. Hosted product jobs are skipped; I did not run local tests.
jotitadev commented 2026-10-23 06:46:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-23T06:46:03Z.

Review: COMMENTED


macOS test: the new CheckEpollClosedStream fails, XNU has the same problem

Apple M2 Max, macOS 27.2, Apple clang (Xcode 27), x86-64 Release build under Rosetta 2, BUILD_TESTING=ON, full default build. Head 0a01fca94f. main does not build on macOS, so the platform is #8658 (feat/macos-port) @ fa063d8b07 with this PR's diff applied (clean apply). guest_sce_net passes on #8658 alone.

With this PR, guest_sce_net aborts in the new case. The first wait after the peer closes returns events=0x11 (SCE_NET_EPOLLIN | SCE_NET_EPOLLHUP), not 0x1. XNU is not FreeBSD here either. A plain loopback probe (poll on the accepted socket after the client closes):

POLLIN|POLLOUT requested: revents=0x11   (POLLIN|POLLHUP, no POLLOUT)
POLLOUT requested alone:  revents=0x4    (POLLOUT)
after recv() == 0:        revents=0x11

So XNU sets POLLHUP as soon as the peer closes, and drops POLLOUT while POLLHUP is set, even though this side can still send. #8658 already handles this for libkernel's poll in ClosedStreamEvents (Socket/src/Udp.cpp, #if defined(_WIN32) || defined(__APPLE__)): it adds the readable bit, re-polls POLLOUT alone when POLLOUT was asked for, and keeps POLLHUP only after a send shutdown.

As a check, I enabled closed_stream_events on __APPLE__ too, using POLLIN instead of POLLRDNORM and with the same POLLOUT re-poll, and called it after ::poll in sceNetEpollWait. With that, the three waits return 0x1, 0x3 and 0x11 as the test expects, and guest_sce_net plus the net_* tests pass 20/20, 3/3. This PR does not have to carry the macOS part, since main does not build on macOS yet. But once #8658 (or the port) lands, this test will fail on macOS until the _WIN32 guard also covers __APPLE__. The experiment diff is attached below in case it helps.

macOS experiment (on top of this PR)
-#ifdef _WIN32
+#if defined(_WIN32) || defined(__APPLE__)
 short closed_stream_events(const Sock& socket, short native, short requested) {
     if (socket.type != NET_SOCK_STREAM || (native & (POLLHUP | POLLERR)) != POLLHUP) return native;
+#ifdef _WIN32
     native = static_cast<short>(native | (requested & POLLRDNORM));
+#else
+    native = static_cast<short>(native | (requested & POLLIN));
+    if (!socket.sending_shut && (requested & POLLOUT)) {
+        pollfd writable{socket.native->value, POLLOUT, 0};
+        if (::poll(&writable, 1, 0) == 1 && (writable.revents & POLLOUT)) native = static_cast<short>(native | POLLOUT);
+    }
+#endif
     return socket.sending_shut ? native : static_cast<short>(native & ~POLLHUP);
 }
 #endif
...
         const int ready_count = ::poll(descriptors.data(), descriptors.size(), wait_ms);
+#ifdef __APPLE__
+        for (std::size_t index = 0; index < descriptors.size(); ++index)
+            descriptors[index].revents = closed_stream_events(watches[index].socket, descriptors[index].revents, descriptors[index].events);
+#endif
 #endif

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5624853896 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9493#pullrequestreview-5624853896). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-23T06:46:03Z. **Review:** COMMENTED --- ### macOS test: the new `CheckEpollClosedStream` fails, XNU has the same problem Apple M2 Max, macOS 27.2, Apple clang (Xcode 27), x86-64 Release build under Rosetta 2, `BUILD_TESTING=ON`, full default build. Head `0a01fca94f`. `main` does not build on macOS, so the platform is #8658 (`feat/macos-port`) @ `fa063d8b07` with this PR's diff applied (clean apply). `guest_sce_net` passes on #8658 alone. With this PR, `guest_sce_net` aborts in the new case. The first wait after the peer closes returns `events=0x11` (`SCE_NET_EPOLLIN | SCE_NET_EPOLLHUP`), not `0x1`. XNU is not FreeBSD here either. A plain loopback probe (`poll` on the accepted socket after the client `close`s): ``` POLLIN|POLLOUT requested: revents=0x11 (POLLIN|POLLHUP, no POLLOUT) POLLOUT requested alone: revents=0x4 (POLLOUT) after recv() == 0: revents=0x11 ``` So XNU sets `POLLHUP` as soon as the peer closes, and drops `POLLOUT` while `POLLHUP` is set, even though this side can still send. #8658 already handles this for libkernel's `poll` in `ClosedStreamEvents` (`Socket/src/Udp.cpp`, `#if defined(_WIN32) || defined(__APPLE__)`): it adds the readable bit, re-polls `POLLOUT` alone when `POLLOUT` was asked for, and keeps `POLLHUP` only after a send shutdown. As a check, I enabled `closed_stream_events` on `__APPLE__` too, using `POLLIN` instead of `POLLRDNORM` and with the same `POLLOUT` re-poll, and called it after `::poll` in `sceNetEpollWait`. With that, the three waits return `0x1`, `0x3` and `0x11` as the test expects, and `guest_sce_net` plus the `net_*` tests pass 20/20, 3/3. This PR does not have to carry the macOS part, since `main` does not build on macOS yet. But once #8658 (or the port) lands, this test will fail on macOS until the `_WIN32` guard also covers `__APPLE__`. The experiment diff is attached below in case it helps. <details><summary>macOS experiment (on top of this PR)</summary> ```diff -#ifdef _WIN32 +#if defined(_WIN32) || defined(__APPLE__) short closed_stream_events(const Sock& socket, short native, short requested) { if (socket.type != NET_SOCK_STREAM || (native & (POLLHUP | POLLERR)) != POLLHUP) return native; +#ifdef _WIN32 native = static_cast<short>(native | (requested & POLLRDNORM)); +#else + native = static_cast<short>(native | (requested & POLLIN)); + if (!socket.sending_shut && (requested & POLLOUT)) { + pollfd writable{socket.native->value, POLLOUT, 0}; + if (::poll(&writable, 1, 0) == 1 && (writable.revents & POLLOUT)) native = static_cast<short>(native | POLLOUT); + } +#endif return socket.sending_shut ? native : static_cast<short>(native & ~POLLHUP); } #endif ... const int ready_count = ::poll(descriptors.data(), descriptors.size(), wait_ms); +#ifdef __APPLE__ + for (std::size_t index = 0; index < descriptors.size(); ++index) + descriptors[index].revents = closed_stream_events(watches[index].socket, descriptors[index].revents, descriptors[index].events); +#endif #endif ``` </details> Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
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!9493
No description provided.