Skip to content

fix(libSceNet): wake a receive on an unbound datagram socket at shutdown on macOS - #10052

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
maximilianfv:fix/libscenet-macos-udp-shutdown
Oct 23, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
maximilianfv:fix/libscenet-macos-udp-shutdown

Conversation

@maximilianfv

Copy link
Copy Markdown
Contributor

What

On macOS, sceNetShutdown(s, 0) on a UDP socket that was never bound does not wake a thread blocked in sceNetRecvfrom on it: the host shutdown(SHUT_RD) does not interrupt the blocked recvfrom, so the receiver hangs.

Windows already avoids the host receive in this case: wait_for_local_address waits on the socket table until a bind, connect or send gives the socket a local address, or until a shutdown, abort or close ends the wait. This PR turns that path on for macOS:

  • has_local_address on macOS checks the port from getsockname, since an unbound socket reports port 0 there instead of failing with WSAEINVAL.
  • wait_for_datagram uses poll instead of WSAPoll.
  • The five call sites in sceNetConnect, sceNetRecv, sceNetRecvfrom, sceNetSendto and sceNetRecvmsg are enabled for __APPLE__ as well as _WIN32.
  • The docs/dev/debt/libSceNet.md entry for this wait now names macOS.

The Windows and Linux code paths are unchanged.

The code is ported from #8658 by @iamthehimansh (credited as co-author on the commit). This PR splits out only this fix, so it can be reviewed and merged on its own.

Tested

Apple M1 Pro, macOS 27.0.1, x86_64 build under Rosetta 2.

main alone does not build on macOS yet (libc, fixed by #4627), so I tested on mugurc's macOS series (ci/macos-series-2, 27e968974) with this change applied. The changed lines and the helpers they use are identical to this branch.

  • guest_sce_net, with the test object rebuilt at -O0 -g and a breakpoint on abort: without the change it stops in CheckUnboundDatagramShutdown, at the check that the receive returned 0 within 1 s of sceNetShutdown. With the change that check passes, and the test stops later at a sceNetResolverStartNtoa6 check that main already changed in 548aecf, which the series does not include.
  • The 58 network tests (ctest -R 'net|sock|udp|tcp|poll|ssl|http|epoll|refused|resolver'), with and without the change: the same failures in both runs (guest_sce_net, guest_ssl_transport_kernel, guest_ssl_transport_net). guest_refused_connect also failed in the run without the change; it fails intermittently on this machine.

Not tested on Windows or Linux. Their code is unchanged apart from the #if 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
  • 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 (Claude Code found the failing test, ported the change from feat: macOS host support (Apple Silicon via Rosetta 2, Vulkan on MoltenVK) #8658, ran the tests and wrote this description; I am responsible for every line)

🤖 Generated with Claude Code

…own on macOS

On macOS, shutdown(SHUT_RD) on a UDP socket that was never bound does not
wake a thread blocked in recv on it, so sceNetShutdown left the receiver
hanging. Use the receive wait that Windows already has: block on the
socket table until the socket gets a local port or is shut, then poll.
On macOS an unbound socket reports port 0 from getsockname, so the check
looks at the port instead of the Winsock error.

Ported from boykopovar#8658.

Co-authored-by: Himansh Raj <iamthehimansh@gmail.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@maximilianfv

Copy link
Copy Markdown
Contributor Author

guest_sce_net now passes end to end

To see the whole test pass, I added the two missing pieces to the same macOS test tree: 548aecf from main (deterministic resolver failures) and #9728 (sceNetCtlGetIfStat byte counters on macOS, which the test reaches next).

With those applied, toggling only this PR's change:

guest_sce_net
Without this change Subprocess aborted (in CheckUnboundDatagramShutdown)
With this change Passed

Same machine as above: Apple M1 Pro, macOS 27.0.1, x86_64 build under Rosetta 2.

AI-assisted: yes (Claude Code applied the commits, ran the tests and wrote this comment).

@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 9733 docs/dev/debt/libSceNet.md L44-45

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 5836 core/libs/prx/libSceNet/Export.cpp
docs/dev/debt/libSceNet.md
PR 7201 core/libs/prx/libSceNet/Export.cpp
PR 8658 core/libs/prx/libSceNet/Export.cpp
PR 9515 core/libs/prx/libSceNet/Export.cpp
PR 9578 core/libs/prx/libSceNet/Export.cpp
docs/dev/debt/libSceNet.md

@maximilianfv

Copy link
Copy Markdown
Contributor Author

On the overlap report:

AI-assisted: yes (Claude Code ran the merge check and wrote this comment).

@fogydes

fogydes commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

OK. I re-derived the structure from the branch rather than trusting the description: the macOS arm is an extension of the existing Windows wait, and Linux is untouched.

  1. Main wraps has_local_address / note_local_address / wait_for_datagram in a single #ifdef _WIN32 block (Export.cpp:688–740). The PR widens that one guard to #if defined(_WIN32) || defined(__APPLE__) and splits only two things per-platform: has_local_address becomes a getsockname port != 0 check on macOS (:690–697) and wait_for_datagram gains a ::poll(POLLIN) arm (:714–733); <poll.h> is already in the non-_WIN32 include block. The Linux preprocessor outcome is unchanged — the widened guard still excludes Linux — and I checked main's guard map (#ifdef/#else/#endif line numbers across the file) to confirm no #endif movement silently changes that. [verified]
  2. The five widened call sites match the claim: sceNetConnect, sceNetRecv, sceNetRecvfrom, sceNetSendto, sceNetRecvmsg. [verified]
  3. The covering test pre-exists on main and is untouched by this PR: CheckUnboundDatagramShutdown (GuestSceNet.cpp:442) blocks in sceNetRecvfrom on an unbound datagram socket and requires the receive to return 0 shortly after sceNetShutdown; guest_sce_net is registered with SKIP_RETURN_CODE 77 (libSceNet/CMakeLists.txt). So the fixture exercises the behavior on any platform that compiles the widened paths. [verified]
  4. The macOS failure itself (blocked recvfrom not woken by sceNetShutdown without this change) is [author-reported only] — I can't run macOS here. That's fine for a platform-guarded fix; CI on a macOS runner is the arbiter. The debt entry at docs/dev/debt/libSceNet.md carries the caveat, preserving the existing sentence structure. [read-only]
  5. has_local_address treating a getsockname error as "has address" mirrors main's Windows fallback (WSAGetLastError() != WSAEINVAL), so the macOS version is consistent with the semantics it replaces rather than inventing new ones. [verified]

Envelope: current main is 664648356c; the PR's base is 0a0ed72167 (1 commit ahead, 24 behind), and no main commit since the base touches libSceNet or its debt file; git merge-tree --write-tree origin/main pr-head is clean.

Disclosure: Windows host; structure, guards, diff, and test registration all checked locally. The macOS runtime behavior is author-reported, as noted above. Nothing needs to change for merge — flagging only that the validating evidence lives on a macOS runner, not in this review.

@oneandonlydean oneandonlydean self-assigned this Oct 23, 2026
@oneandonlydean
oneandonlydean merged commit 0825fee into boykopovar:main Oct 23, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants