fix(libkernel): fail a send during a TCP connect with ENOTCONN on Linux, as FreeBSD 11.4 does #24939

Merged
Dr-Coconut merged 1 commit from upstream-pr/24939 into main 2026-11-21 12:36:57 +00:00
Dr-Coconut commented 2026-11-21 11:47:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dr-Coconut on 2026-11-21T11:47:54Z.


What

FreeBSD 11.4's sosend_generic fails a send without an address on a stream socket that is not connected with ENOTCONN, and a socket whose connect is still in progress is not connected. On Linux the guest's send went to the host instead, which waits for the handshake on a blocking socket or fails with EAGAIN on a non-blocking one, a gap the libkernel debt file records.

  • A stream connect marks the socket pending (connectPending, Linux only). The mark stays when the host reports EINPROGRESS, EALREADY or EINTR, and clears when the connect succeeds or fails otherwise.
  • SendBeforeConnect, which send, sendto without an address and write already consult before waiting, answers ENOTCONN while the pending socket is not writable (a zero-timeout poll for POLLOUT). Once the host reports it writable, whether the handshake completed or failed, the mark clears and sends go to the host as before.
  • Windows and macOS are unchanged; the debt fragment says such a send still goes to the host there.

GuestTcp.cpp gets a Linux-only case: a loopback listener with listen(fd, 0) holds one connection, so the next non-blocking connect stays in progress (checked with getpeername), and send, write and sendto without an address on it fail with ENOTCONN. The debt fragment rewrites the entry without the Linux gap.

Tested

Commit eb76321b73 on main c52d532062, Release, in a workflow on my fork: https://github.com/Dr-Coconut/AnyPS5/actions/runs/44900233617

  • Linux (GitHub-hosted ubuntu-latest, image ubuntu-26.04, GCC 15.2.0), with the change: guest_tcp, guest_udp, guest_socket_pair and guest_kernel_socket_dontwait pass.
  • Linux with Udp.cpp restored to main and the new case kept: guest_tcp aborts after 0.18 s, in the new case, which runs first; the other three pass.
  • Windows (GitHub-hosted windows-latest, WinLibs GCC 15.2.0 15.2.0posix-14.0.0-ucrt-r7), with the change: all four pass. The change does not alter Windows.

Not run on macOS. No title tested. tools/check_conventions.py passes, and tools/debt_fold.py --check accepts the fragment (on main it currently stops at feat-libwebrtc-rnn-vad.md, which matches no libwebrtc entry). tools/pr_overlap.py --branch merged 26 open pull requests that share files with this branch and reported #12688, #12700 and #12965, which rework the same lines of connect (blocking connects over non-blocking host sockets on Windows) without tracking a pending connect, so they overlap textually. It could not compare three that conflict with main; compared by hand, #8605 changes GuestSockets::Stat, #11896 reworks the receives, and #18190 moves connect's body into connect_nid_no_patch behind a wrapper, which keeps these lines.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5427491764 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24939). Originally posted by [Dr-Coconut](https://github.com/Dr-Coconut) on 2026-11-21T11:47:54Z. --- ### What FreeBSD 11.4's [`sosend_generic`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/uipc_socket.c#L1260-L1292) fails a send without an address on a stream socket that is not connected with `ENOTCONN`, and a socket whose `connect` is still in progress is not connected. On Linux the guest's send went to the host instead, which waits for the handshake on a blocking socket or fails with `EAGAIN` on a non-blocking one, a gap the libkernel debt file records. - A stream `connect` marks the socket pending (`connectPending`, Linux only). The mark stays when the host reports `EINPROGRESS`, `EALREADY` or `EINTR`, and clears when the connect succeeds or fails otherwise. - `SendBeforeConnect`, which `send`, `sendto` without an address and `write` already consult before waiting, answers `ENOTCONN` while the pending socket is not writable (a zero-timeout `poll` for `POLLOUT`). Once the host reports it writable, whether the handshake completed or failed, the mark clears and sends go to the host as before. - Windows and macOS are unchanged; the debt fragment says such a send still goes to the host there. `GuestTcp.cpp` gets a Linux-only case: a loopback listener with `listen(fd, 0)` holds one connection, so the next non-blocking `connect` stays in progress (checked with `getpeername`), and `send`, `write` and `sendto` without an address on it fail with `ENOTCONN`. The debt fragment rewrites the entry without the Linux gap. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Commit eb76321b73 on `main` c52d532062, Release, in a workflow on my fork: https://github.com/Dr-Coconut/AnyPS5/actions/runs/44900233617 - Linux (GitHub-hosted `ubuntu-latest`, image ubuntu-26.04, GCC 15.2.0), with the change: `guest_tcp`, `guest_udp`, `guest_socket_pair` and `guest_kernel_socket_dontwait` pass. - Linux with `Udp.cpp` restored to `main` and the new case kept: `guest_tcp` aborts after 0.18 s, in the new case, which runs first; the other three pass. - Windows (GitHub-hosted `windows-latest`, WinLibs GCC 15.2.0 `15.2.0posix-14.0.0-ucrt-r7`), with the change: all four pass. The change does not alter Windows. Not run on macOS. No title tested. `tools/check_conventions.py` passes, and `tools/debt_fold.py --check` accepts the fragment (on `main` it currently stops at `feat-libwebrtc-rnn-vad.md`, which matches no libwebrtc entry). `tools/pr_overlap.py --branch` merged 26 open pull requests that share files with this branch and reported #12688, #12700 and #12965, which rework the same lines of `connect` (blocking connects over non-blocking host sockets on Windows) without tracking a pending connect, so they overlap textually. It could not compare three that conflict with `main`; compared by hand, #8605 changes `GuestSockets::Stat`, #11896 reworks the receives, and #18190 moves `connect`'s body into `connect_nid_no_patch` behind a wrapper, which keeps these lines. ### 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) and the licence notice of code adapted from upstream (see AGENTS.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 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-11-21 12:20:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T12:20:51Z.

Review: COMMENTED


Reviewed — comments

The change tracks Linux stream connects still in progress and returns ENOTCONN for sends while the host socket is not writable; I found one edge case in the readiness check.

What I ran

Reviewed head eb76321b732ccdc8b4a611cf7667c1cf52499d50. No commands or tests were run in this review; the test results in the PR description are author-reported.

Results

Check Result Note
Changed code review passed Three changed files read
Tests and conventions not run No local commands run

Findings

Blocking

None.

Non-blocking

  • core/libs/prx/libkernel/Socket/src/Udp.cpp:1026: SendBeforeConnect treats every nonzero result from the zero-timeout poll as writable, including -1 on an interrupted or failed poll. That clears connectPending and lets the send reach the host, contrary to the stated behavior that sends stay blocked until the pending connect completes or fails. Consider clearing the mark only when poll returns positive and the returned events indicate completion; retry or preserve pending state on errors.

Not verified

  • No tests were run. The PR description reports Linux and Windows test runs, a Linux negative control, and convention/debt checks; those results were not independently verified here.
  • No console or macOS behavior was verified.
  • Read all changed files listed in the manifest: core/libs/prx/libkernel/Socket/src/Udp.cpp, core/libs/tests/GuestTcp.cpp, and docs/dev/debt/changes/fix-libkernel-send-during-connect.md. files.json was not available through the reader; no additional excluded-file list was verified.

To get this to a good state

Nothing is required. The poll-error case is optional feedback.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6005138558 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24939#pullrequestreview-6005138558). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-21T12:20:51Z. **Review:** COMMENTED --- # Reviewed — comments The change tracks Linux stream connects still in progress and returns `ENOTCONN` for sends while the host socket is not writable; I found one edge case in the readiness check. ## What I ran Reviewed head `eb76321b732ccdc8b4a611cf7667c1cf52499d50`. No commands or tests were run in this review; the test results in the PR description are author-reported. ## Results | Check | Result | Note | |---|---|---| | Changed code review | passed | Three changed files read | | Tests and conventions | not run | No local commands run | ## Findings ### Blocking None. ### Non-blocking - `core/libs/prx/libkernel/Socket/src/Udp.cpp:1026`: `SendBeforeConnect` treats every nonzero result from the zero-timeout `poll` as writable, including `-1` on an interrupted or failed poll. That clears `connectPending` and lets the send reach the host, contrary to the stated behavior that sends stay blocked until the pending connect completes or fails. Consider clearing the mark only when `poll` returns positive and the returned events indicate completion; retry or preserve pending state on errors. ## Not verified - No tests were run. The PR description reports Linux and Windows test runs, a Linux negative control, and convention/debt checks; those results were not independently verified here. - No console or macOS behavior was verified. - Read all changed files listed in the manifest: `core/libs/prx/libkernel/Socket/src/Udp.cpp`, `core/libs/tests/GuestTcp.cpp`, and `docs/dev/debt/changes/fix-libkernel-send-during-connect.md`. `files.json` was not available through the reader; no additional excluded-file list was verified. ## To get this to a good state Nothing is required. The poll-error case is optional feedback.
PrivacyOnion commented 2026-11-21 12:24:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-21T12:24:38Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against eb76321b73:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_tcp_tests built cleanly.
  • CTest (guest_tcp):
    1/1 Test #5962: guest_tcp ........................   Passed    0.71 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libkernel/Socket/src/Udp.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #5962: guest_tcp ........................Subprocess aborted***Exception:   0.50 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Checked core/libs/prx/libkernel/Socket/src/Udp.cpp SendBeforeConnect: verified pollfd {socket.value, POLLOUT, 0} check with zero timeout properly detects uncompleted connects and sets connectPending = false once writable under modeMutex
    • Checked core/libs/prx/libkernel/Socket/src/Udp.cpp connect_nid_postfix: verified connectPending state transitions on EINPROGRESS, EALREADY, and EINTR, with mutex locking around error handling and cleanup on immediate completion
    • Checked error codes for FreeBSD 11.4 compliance: verified send/sendto/write correctly return ENOTCONN (57) before connection completion as documented in the debt fragment
    • Checked concurrency and locking in Socket struct: verified modeMutex protections across connectPending reads, writes, and poll interactions
    • Checked core/libs/tests/GuestTcp.cpp: verified non-blocking connect properly holds in progress via listen backlog exhaustion and tests send, write, and sendto without address returning NotConnected (57)
    • Checked AGENTS.md compliance: verified technical debt fragment format in docs/dev/debt/changes/fix-libkernel-send-during-connect.md and absence of illegal code comments

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6005162842 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24939#pullrequestreview-6005162842). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-21T12:24:38Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `eb76321b73`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_tcp_tests` built cleanly. - **CTest** (`guest_tcp`): ``` 1/1 Test #5962: guest_tcp ........................ Passed 0.71 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libkernel/Socket/src/Udp.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #5962: guest_tcp ........................Subprocess aborted***Exception: 0.50 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - Checked core/libs/prx/libkernel/Socket/src/Udp.cpp SendBeforeConnect: verified pollfd {socket.value, POLLOUT, 0} check with zero timeout properly detects uncompleted connects and sets connectPending = false once writable under modeMutex - Checked core/libs/prx/libkernel/Socket/src/Udp.cpp connect_nid_postfix: verified connectPending state transitions on EINPROGRESS, EALREADY, and EINTR, with mutex locking around error handling and cleanup on immediate completion - Checked error codes for FreeBSD 11.4 compliance: verified send/sendto/write correctly return ENOTCONN (57) before connection completion as documented in the debt fragment - Checked concurrency and locking in Socket struct: verified modeMutex protections across connectPending reads, writes, and poll interactions - Checked core/libs/tests/GuestTcp.cpp: verified non-blocking connect properly holds in progress via listen backlog exhaustion and tests send, write, and sendto without address returning NotConnected (57) - Checked AGENTS.md compliance: verified technical debt fragment format in docs/dev/debt/changes/fix-libkernel-send-during-connect.md and absence of illegal code comments Looks Good To Me
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!24939
No description provided.