fix(libSceNet): close a libkernel socket through sceNetSocketClose #6140

Merged
milescoviello merged 1 commit from upstream-pr/6140 into main 2026-10-18 15:22:52 +00:00
milescoviello commented 2026-10-14 23:10:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-14T23:10:22Z.


What

MXGP 26: The Official Game (PPSA30839) opens TCP sockets with libkernel's socket(), then closes them with sceNetSocketClose. sceNetSocketClose only looked in libSceNet's own socket table, so it failed every one of those closes with SCE_NET_ERROR_EBADF (0x80410109), and the host socket stayed open. On the console, libSceNet's calls and libkernel's socket calls work on the same kernel sockets.

Now, when sceNetSocketClose gets a descriptor that libSceNet doesn't own but libkernel has open as a socket (GuestSocketIsOpen_nid_no_patch), it closes it through libkernel's close(). Any other descriptor still fails with EBADF, as before.

The fallback runs after g_mutex is released. libkernel's close() first asks libSceNet's registered socket closer, and that closer takes g_mutex.

guest_sce_net gains a check:

  • it opens a TCP socket with libkernel's socket() and closes it with sceNetSocketClose;
  • libkernel no longer reports the socket open;
  • a second sceNetSocketClose fails with EBADF.

Tested

Ubuntu 24.04, GCC 13.3 (CI's compiler), Release with CI's configure flags, on main eea428ca8:

  • guest_sce_net aborts with main's Export.cpp and passes with the change.
  • All 376 guest_* tests pass.
  • tools/check_conventions.py --base upstream/main passes.
  • PPSA30839 relinked with --to-intel, 90 s, every libSceNet socket call traced under gdb:
    • On main, sceNetSocketClose returns 0x80410109 for descriptors 7, 9 and 11. 7 and 9 came from libkernel's socket(2, 1, 0).
    • With this change, all three return 0.
    • Its sceNetSetsockopt(11, IPPROTO_TCP, TCP_NODELAY) still fails with EBADF. Setting options on a libkernel socket through libSceNet is a separate change, and #5565 covers TCP_NODELAY on libkernel sockets.

Not checked on a console.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4865794200 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6140). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-14T23:10:22Z. --- ### What MXGP 26: The Official Game (PPSA30839) opens TCP sockets with libkernel's `socket()`, then closes them with `sceNetSocketClose`. `sceNetSocketClose` only looked in libSceNet's own socket table, so it failed every one of those closes with `SCE_NET_ERROR_EBADF` (`0x80410109`), and the host socket stayed open. On the console, libSceNet's calls and libkernel's socket calls work on the same kernel sockets. Now, when `sceNetSocketClose` gets a descriptor that libSceNet doesn't own but libkernel has open as a socket (`GuestSocketIsOpen_nid_no_patch`), it closes it through libkernel's `close()`. Any other descriptor still fails with `EBADF`, as before. The fallback runs after `g_mutex` is released. libkernel's `close()` first asks libSceNet's registered socket closer, and that closer takes `g_mutex`. `guest_sce_net` gains a check: - it opens a TCP socket with libkernel's `socket()` and closes it with `sceNetSocketClose`; - libkernel no longer reports the socket open; - a second `sceNetSocketClose` fails with `EBADF`. ### Tested Ubuntu 24.04, GCC 13.3 (CI's compiler), Release with CI's configure flags, on `main` eea428ca8: - `guest_sce_net` aborts with `main`'s `Export.cpp` and passes with the change. - All 376 `guest_*` tests pass. - `tools/check_conventions.py --base upstream/main` passes. - PPSA30839 relinked with `--to-intel`, 90 s, every libSceNet socket call traced under gdb: - On `main`, `sceNetSocketClose` returns `0x80410109` for descriptors 7, 9 and 11. 7 and 9 came from libkernel's `socket(2, 1, 0)`. - With this change, all three return 0. - Its `sceNetSetsockopt(11, IPPROTO_TCP, TCP_NODELAY)` still fails with `EBADF`. Setting options on a libkernel socket through libSceNet is a separate change, and #5565 covers `TCP_NODELAY` on libkernel sockets. Not checked on a console. ### Checklist - [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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-14 23:16:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-14T23:16:35Z.


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
core/libs/tests/GuestSceNet.cpp
PR 6044 core/libs/tests/GuestSceNet.cpp
PR 6138 core/libs/tests/GuestSceNet.cpp
PR 7201 core/libs/prx/libSceNet/Export.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6180600077 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6140#issuecomment-6180600077). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-14T23:16:35Z. --- <!-- pr-overlap --> 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](https://www.github.com/boykopovar/AnyPS5/pull/5836) | `core/libs/prx/libSceNet/Export.cpp`<br>`core/libs/tests/GuestSceNet.cpp` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/GuestSceNet.cpp` | | [PR 6138](https://www.github.com/boykopovar/AnyPS5/pull/6138) | `core/libs/tests/GuestSceNet.cpp` | | [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) | `core/libs/prx/libSceNet/Export.cpp` |
adriadam10 commented 2026-10-16 23:53:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-16T23:53:53Z.


Checked build and ctest (guest_sce_net passes; it fails when Export.cpp is reverted), the lock ordering (fallback runs after g_mutex is released) and the EBADF path for descriptors neither library owns; merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6226917689 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6140#issuecomment-6226917689). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-16T23:53:53Z. --- Checked build and ctest (guest_sce_net passes; it fails when Export.cpp is reverted), the lock ordering (fallback runs after `g_mutex` is released) and the EBADF path for descriptors neither library owns; merging.
milescoviello commented 2026-10-17 20:02:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-17T20:02:22Z.


@adriadam10, I rebased this onto main e3838332e earlier today, as part of rebasing my older PRs. The head moved from 1c6505156 to 8b81fa09b and the patch is unchanged (git range-diff shows =). In case your merge didn't go through: after the rebase, guest_sce_net passes 3 of 3 and ^guest_ passes 409 of 409.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6240085202 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6140#issuecomment-6240085202). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-17T20:02:22Z. --- @adriadam10, I rebased this onto `main` e3838332e earlier today, as part of rebasing my older PRs. The head moved from 1c6505156 to 8b81fa09b and the patch is unchanged (`git range-diff` shows `=`). In case your merge didn't go through: after the rebase, `guest_sce_net` passes 3 of 3 and `^guest_` passes 409 of 409. AI-assisted: yes (Claude Code)
oneandonlydean commented 2026-10-18 15:23:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-18T15:23:26Z.


Merged while @adriadam10 is away: the review passed and full CI is green on this head.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6251634101 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6140#issuecomment-6251634101). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-18T15:23:26Z. --- Merged while @adriadam10 is away: the review passed and full CI is green on this head.
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!6140
No description provided.