libkernel: a socket accepted from a non-blocking listener is blocking (FreeBSD inherits the mode); fix branch ready #3938

Closed
opened 2026-10-11 19:25:25 +00:00 by husseinkad · 2 comments
husseinkad commented 2026-10-11 19:25:25 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-11T19:25:25Z.


accept on a non-blocking listener returns a socket that is blocking: fcntl(F_GETFL) on it shows no O_NONBLOCK, and a recv on it with no data waits instead of returning EWOULDBLOCK. FreeBSD 11.4 accept calls kern_accept4 with ACCEPT4_INHERIT, which copies the listener's non-blocking flag (SS_NBIO) to the accepted socket. A title that sets its listener non-blocking and then polls the accepted sockets would block in recv on Windows.

Fix branch: https://github.com/husseinkad/AnyPS5/tree/fix/kernel-socket-accept-nonblock (one commit on current main, Socket/src/Udp.cpp and tests/GuestTcp.cpp). When the listener is non-blocking, the accepted socket is put in non-blocking mode through the same SetNonBlocking helper that fcntl and ioctl use, so the tracked flag that F_GETFL reads stays consistent. A blocking listener is unchanged.

Checks:

  • The new guest_tcp case fails on main (assertion abort) and passes with the change; guest_(tcp|udp|poll|socket|dup) pass.
  • Probed through the guest exports on loopback, IPv4 and IPv6, against main: the only difference is that F_GETFL on an accepted socket returns 6 instead of 2 when the listener is non-blocking. An empty non-blocking accept still gives EWOULDBLOCK with the address buffer untouched; recv on the inherited socket gives 35 when empty; turning the listener back to blocking (by fcntl or FIONBIO, also through a duplicated descriptor) makes later accepts blocking, and an already accepted socket keeps its own mode; poll results are identical.
  • Two independent reviews found nothing wrong. check_conventions.py is clean and pr_overlap.py finds no overlap (#3592 also edits Udp.cpp, in the error table, so a trivial rebase may be needed).

Not covered: sceNetAccept in libSceNet/Export.cpp has its own accept path that does not inherit the mode either. On Linux the host accepted socket is blocking, so the new test would hang there without the fix; only Windows was built and run.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5804375834 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/3938). Originally posted by [husseinkad](https://github.com/husseinkad) on 2026-10-11T19:25:25Z. --- `accept` on a non-blocking listener returns a socket that is blocking: `fcntl(F_GETFL)` on it shows no `O_NONBLOCK`, and a `recv` on it with no data waits instead of returning `EWOULDBLOCK`. FreeBSD 11.4 `accept` calls `kern_accept4` with `ACCEPT4_INHERIT`, which copies the listener's non-blocking flag (`SS_NBIO`) to the accepted socket. A title that sets its listener non-blocking and then polls the accepted sockets would block in `recv` on Windows. Fix branch: https://github.com/husseinkad/AnyPS5/tree/fix/kernel-socket-accept-nonblock (one commit on current main, `Socket/src/Udp.cpp` and `tests/GuestTcp.cpp`). When the listener is non-blocking, the accepted socket is put in non-blocking mode through the same `SetNonBlocking` helper that `fcntl` and `ioctl` use, so the tracked flag that `F_GETFL` reads stays consistent. A blocking listener is unchanged. Checks: - The new `guest_tcp` case fails on main (assertion abort) and passes with the change; `guest_(tcp|udp|poll|socket|dup)` pass. - Probed through the guest exports on loopback, IPv4 and IPv6, against main: the only difference is that `F_GETFL` on an accepted socket returns 6 instead of 2 when the listener is non-blocking. An empty non-blocking accept still gives `EWOULDBLOCK` with the address buffer untouched; `recv` on the inherited socket gives 35 when empty; turning the listener back to blocking (by `fcntl` or `FIONBIO`, also through a duplicated descriptor) makes later accepts blocking, and an already accepted socket keeps its own mode; `poll` results are identical. - Two independent reviews found nothing wrong. `check_conventions.py` is clean and `pr_overlap.py` finds no overlap (#3592 also edits `Udp.cpp`, in the error table, so a trivial rebase may be needed). Not covered: `sceNetAccept` in `libSceNet/Export.cpp` has its own accept path that does not inherit the mode either. On Linux the host accepted socket is blocking, so the new test would hang there without the fix; only Windows was built and run. AI-assisted: yes
MarcZX10 commented 2026-10-13 03:36:16 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-13T03:36:16Z.


The accepted-socket inheritance fix from the linked branch is already present on current main c13dc7e6b440725babd25ccec4262ccc4c78dfc3: accept_nid_postfix copies the listener nonblocking state and applies it with SetNonBlocking, and guest_tcp includes AcceptedSocketsFollowListenerBlocking. I built guest_tcp_tests on Linux/GCC 12.2 against this main and ctest -R ^guest_tcp$ passes (1/1); conventions and test registration also pass. No PR is needed for this issue as currently written. The separate sceNetAccept path in #4898 remains a distinct implementation.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6137634493 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/3938#issuecomment-6137634493). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-13T03:36:16Z. --- The accepted-socket inheritance fix from the linked branch is already present on current main `c13dc7e6b440725babd25ccec4262ccc4c78dfc3`: `accept_nid_postfix` copies the listener nonblocking state and applies it with `SetNonBlocking`, and `guest_tcp` includes `AcceptedSocketsFollowListenerBlocking`. I built `guest_tcp_tests` on Linux/GCC 12.2 against this main and `ctest -R ^guest_tcp$` passes (1/1); conventions and test registration also pass. No PR is needed for this issue as currently written. The separate `sceNetAccept` path in #4898 remains a distinct implementation.
oneandonlydean commented 2026-10-13 07:37:47 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-13T07:37:47Z.


Closing: fixed by #3958. Reopen if an accepted socket still comes back blocking on current main.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6140706449 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/3938#issuecomment-6140706449). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-13T07:37:47Z. --- Closing: fixed by #3958. Reopen if an accepted socket still comes back blocking on current main.
Sign in to join this conversation.
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#3938
No description provided.