fix(libSceNetCtl): keep the callback argument and deliver the state from sceNetCtlCheckCallback #6138

Merged
fdfawef324 merged 1 commit from upstream-pr/6138 into main 2026-10-18 18:05:57 +00:00
fdfawef324 commented 2026-10-14 23:07:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fdfawef324 on 2026-10-14T23:07:32Z.


What

Stop 2 of the Minecraft Preview bring-up (#1277), NetCtl half: "NetCtl must store the callback argument and deliver the state from sceNetCtlCheckCallback". On main, sceNetCtlRegisterCallback and sceNetCtlRegisterCallbackV6 store only the function and drop arg, and sceNetCtlCheckCallback returns 0 without calling anything, so a title that polls sceNetCtlCheckCallback until its callback reports the connection state never gets it.

  • NetCtlCallback in SceTypes.hpp is now void (APS5_VABI *)(int, void*). Without APS5_VABI, the Windows build would call the guest function with the Microsoft ABI. The captureless lambda in GuestSceNet.cpp no longer converts to it, so it becomes an APS5_VABI function.
  • Both callback tables hold slots {func, arg, lastEvent}. Registration stores func and arg in an empty slot and writes *cid; unregistration clears the slot. Error codes, and the result for an empty slot, are unchanged.
  • With no sceNetCtlRegisterCallback callback registered, sceNetCtlCheckCallback returns 0 without querying the host.
  • Otherwise it takes the event from the predicate sceNetCtlGetState uses: IPOBTAINED (3) while the host has a non-loopback address, DISCONNECTED (1) otherwise. The values come from shadPS4 net_ctl_codes.h L27-29. Under the lock it marks the IPv4 slots whose lastEvent differs. After the lock is released, it calls each of them on the calling thread as func(event, arg), so a callback can unregister itself without deadlocking (tested).
  • Each slot gets an event once per state change, as in KytyPS5 network.cpp @9e41d832 (NetCtlCallbackSlot L3447-3451, NetCtlCheckCallback L3648-3661, NetCtlRegisterCallback L3687-3707). shadPS4 net_ctl_obj.cpp L47-57 delivers the state on every check instead. Neither model is measured on hardware, and the debt entry records the difference.
  • KytyPS5 calls the callbacks under its lock, so its NetCtlUnregisterCallback waits for a running check. Here they are called from a copy of their slots, so an unregister or sceNetCtlTerm on another thread that returns while a check is in flight does not stop that check from calling the callback once more with its old argument. The debt entry records this.
  • sceNetCtlTerm clears both tables, as KytyPS5 NetCtlTerm (L3625-3633) does.
  • V6 slots keep their argument but get no event, because no source says which call delivers them. This is recorded in the debt entry.

A Functional entry in docs/dev/debt/libSceNetCtl.md records that the delivery model comes from KytyPS5 and is not measured on hardware, that the state comes from the host interfaces, the in-flight call after a concurrent unregister, and that V6 callbacks get no event.

Cost: on Windows the host query is two GetAdaptersAddresses calls, the same as one sceNetCtlGetState on main. On this host a check with a callback registered costs about 8.6 ms when the host is idle and 10-23 ms while parallel builds run; sceNetCtlGetState costs the same. With no callback registered, a check returns before the query (under 1 us). A follow-up PR will cache the predicate on Windows (invalidated by NotifyUnicastIpAddressChange/NotifyIpInterfaceChange, or built on GetUnicastIpAddressTable) and retry GetAdaptersAddresses on ERROR_BUFFER_OVERFLOW. Until then, an address added between the size query and the fetch reads as no address, as it already does for sceNetCtlGetState, and here it delivers DISCONNECTED and then IPOBTAINED on the next check.

This covers only the NetCtl half of stop 2. The NpManager half (state callbacks and SIGNED_OUT from sceNpCheckCallback) is a separate change. Minecraft Bedrock (PPSA17221) is playable on main without this change (#5744), so the change was not needed there. The Preview needs it for stop 2 of #1277. No open PR covers these functions; the last change to them was #5427 (RegisterCallbackV6/UnregisterCallbackV6). #6039 also appends to docs/dev/debt/libSceNetCtl.md; whichever merges second resolves a two-line append.

Test: guest_net_ctl (core/libs/tests/GuestNetCtl.cpp) now runs callback cases before its existing exit 77 for a host without an IPv4 address. The expected event comes from sceNetCtlGetState, so the result does not depend on the host's connectivity.

  • A V6 callback is registered before the first check and again after sceNetCtlTerm (cid 0 both times); it is never called.
  • After a register with &probe and a check, the callback has been called exactly once, with arg == &probe and the expected event. A second check calls nothing.
  • A second callback with a different argument: the next check calls only the new one, with its own argument.
  • A callback that is registered and then unregistered before a check is not called.
  • A callback that unregisters its own cid from inside the call: the unregister returns 0, the check returns, the next check does not call it, and the next registration reuses its cid.
  • A callback registered before sceNetCtlTerm is not called by a check after it. Registering again after Term gets cid 0, and the next check delivers the current state again.

Tested

Windows Server 2025 (10.0.26100), WinLibs MinGW-w64 GCC 15.2.0, Ninja, Release, BUILD_TESTING=ON, on main eea428ca8.

  • Red, main with only the new test: GuestNetCtl.cpp does not compile on Windows, because an APS5_VABI function does not convert to main's NetCtlCallback. Red, with the NetCtlCallback type change and main's Export.cpp: guest_net_ctl aborts (0xc0000409) in 2 of 2 runs on eea428ca8, 2 of 2 on 922abe6a4 and 3 of 3 on 153d5d469, because the callback is never called.
  • Red, on 153d5d469: this change with sceNetCtlCheckCallback also walking the V6 table aborts (0xc0000409) in 3 of 3 runs, and this change with the callbacks called under the lock times out at 20 s (deadlock in the self-unregister case).
  • Green, with this change: guest_net_ctl passes in 5 of 5 runs on eea428ca8 and 6 of 6 on 922abe6a4 (0.4-1.3 s on a host running parallel builds), and 8 of 8 on 153d5d469. The host has an address, so the cases ran with event 3 and the test did not skip.
  • ctest -R 'Net|net' on eea428ca8 and on 922abe6a4: 9 of 9 passed (guest_net_ctl, guest_sce_net, guest_netctl_ap_dialog, guest_datagram_*, guest_socket_host_write_*).
  • python tools/check_conventions.py --base upstream/main (the tools from main): passed.
  • git merge-tree against the open PRs that touch the same files (#6044, #6039, #6023, #5995, #5991, #5967, #5966, #5965, #5928, #5836, #5659, #5611, #5473; #5536 has since merged and is in the base): the only conflict this change adds is the #6039 append to the debt file, described above.

Not tested: Linux and macOS (there APS5_VABI is empty and the test fails at runtime instead of at compile time), and any title. I did not run Minecraft Preview.

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
  • Unblocks: PPSA19639 #1277
  • GPUs tested: n/a (no AGC, shader recompiler or VideoOut change)
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4865771044 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6138). Originally posted by [fdfawef324](https://github.com/fdfawef324) on 2026-10-14T23:07:32Z. --- ### What Stop 2 of the Minecraft Preview bring-up (#1277), NetCtl half: "NetCtl must store the callback argument and deliver the state from `sceNetCtlCheckCallback`". On `main`, `sceNetCtlRegisterCallback` and `sceNetCtlRegisterCallbackV6` store only the function and drop `arg`, and `sceNetCtlCheckCallback` returns 0 without calling anything, so a title that polls `sceNetCtlCheckCallback` until its callback reports the connection state never gets it. - `NetCtlCallback` in `SceTypes.hpp` is now `void (APS5_VABI *)(int, void*)`. Without `APS5_VABI`, the Windows build would call the guest function with the Microsoft ABI. The captureless lambda in `GuestSceNet.cpp` no longer converts to it, so it becomes an `APS5_VABI` function. - Both callback tables hold slots `{func, arg, lastEvent}`. Registration stores `func` and `arg` in an empty slot and writes `*cid`; unregistration clears the slot. Error codes, and the result for an empty slot, are unchanged. - With no `sceNetCtlRegisterCallback` callback registered, `sceNetCtlCheckCallback` returns 0 without querying the host. - Otherwise it takes the event from the predicate `sceNetCtlGetState` uses: `IPOBTAINED` (3) while the host has a non-loopback address, `DISCONNECTED` (1) otherwise. The values come from [shadPS4 `net_ctl_codes.h` L27-29](https://github.com/shadps4-emu/shadPS4/blob/15abf68c9b52c4b04d094f2837719fdb871021f3/src/core/libraries/network/net_ctl_codes.h#L27-L29). Under the lock it marks the IPv4 slots whose `lastEvent` differs. After the lock is released, it calls each of them on the calling thread as `func(event, arg)`, so a callback can unregister itself without deadlocking (tested). - Each slot gets an event once per state change, as in KytyPS5 [`network.cpp` @9e41d832](https://github.com/KytyPS5/KytyPS5/blob/9e41d8320a2774b303fbad344c2347e8d0292e83/src/libs/network.cpp) (`NetCtlCallbackSlot` L3447-3451, `NetCtlCheckCallback` L3648-3661, `NetCtlRegisterCallback` L3687-3707). [shadPS4 `net_ctl_obj.cpp` L47-57](https://github.com/shadps4-emu/shadPS4/blob/15abf68c9b52c4b04d094f2837719fdb871021f3/src/core/libraries/network/net_ctl_obj.cpp#L47-L57) delivers the state on every check instead. Neither model is measured on hardware, and the debt entry records the difference. - KytyPS5 calls the callbacks under its lock, so its `NetCtlUnregisterCallback` waits for a running check. Here they are called from a copy of their slots, so an unregister or `sceNetCtlTerm` on another thread that returns while a check is in flight does not stop that check from calling the callback once more with its old argument. The debt entry records this. - `sceNetCtlTerm` clears both tables, as KytyPS5 `NetCtlTerm` (L3625-3633) does. - V6 slots keep their argument but get no event, because no source says which call delivers them. This is recorded in the debt entry. A Functional entry in `docs/dev/debt/libSceNetCtl.md` records that the delivery model comes from KytyPS5 and is not measured on hardware, that the state comes from the host interfaces, the in-flight call after a concurrent unregister, and that V6 callbacks get no event. Cost: on Windows the host query is two `GetAdaptersAddresses` calls, the same as one `sceNetCtlGetState` on `main`. On this host a check with a callback registered costs about 8.6 ms when the host is idle and 10-23 ms while parallel builds run; `sceNetCtlGetState` costs the same. With no callback registered, a check returns before the query (under 1 us). A follow-up PR will cache the predicate on Windows (invalidated by `NotifyUnicastIpAddressChange`/`NotifyIpInterfaceChange`, or built on `GetUnicastIpAddressTable`) and retry `GetAdaptersAddresses` on `ERROR_BUFFER_OVERFLOW`. Until then, an address added between the size query and the fetch reads as no address, as it already does for `sceNetCtlGetState`, and here it delivers `DISCONNECTED` and then `IPOBTAINED` on the next check. This covers only the NetCtl half of stop 2. The NpManager half (state callbacks and SIGNED_OUT from `sceNpCheckCallback`) is a separate change. Minecraft Bedrock (PPSA17221) is playable on `main` without this change (#5744), so the change was not needed there. The Preview needs it for stop 2 of #1277. No open PR covers these functions; the last change to them was #5427 (`RegisterCallbackV6`/`UnregisterCallbackV6`). #6039 also appends to `docs/dev/debt/libSceNetCtl.md`; whichever merges second resolves a two-line append. Test: `guest_net_ctl` (`core/libs/tests/GuestNetCtl.cpp`) now runs callback cases before its existing exit 77 for a host without an IPv4 address. The expected event comes from `sceNetCtlGetState`, so the result does not depend on the host's connectivity. - A V6 callback is registered before the first check and again after `sceNetCtlTerm` (cid 0 both times); it is never called. - After a register with `&probe` and a check, the callback has been called exactly once, with `arg == &probe` and the expected event. A second check calls nothing. - A second callback with a different argument: the next check calls only the new one, with its own argument. - A callback that is registered and then unregistered before a check is not called. - A callback that unregisters its own cid from inside the call: the unregister returns 0, the check returns, the next check does not call it, and the next registration reuses its cid. - A callback registered before `sceNetCtlTerm` is not called by a check after it. Registering again after `Term` gets cid 0, and the next check delivers the current state again. ### Tested Windows Server 2025 (10.0.26100), WinLibs MinGW-w64 GCC 15.2.0, Ninja, Release, `BUILD_TESTING=ON`, on `main` eea428ca8. - Red, `main` with only the new test: `GuestNetCtl.cpp` does not compile on Windows, because an `APS5_VABI` function does not convert to `main`'s `NetCtlCallback`. Red, with the `NetCtlCallback` type change and `main`'s `Export.cpp`: `guest_net_ctl` aborts (`0xc0000409`) in 2 of 2 runs on eea428ca8, 2 of 2 on 922abe6a4 and 3 of 3 on 153d5d469, because the callback is never called. - Red, on 153d5d469: this change with `sceNetCtlCheckCallback` also walking the V6 table aborts (`0xc0000409`) in 3 of 3 runs, and this change with the callbacks called under the lock times out at 20 s (deadlock in the self-unregister case). - Green, with this change: `guest_net_ctl` passes in 5 of 5 runs on eea428ca8 and 6 of 6 on 922abe6a4 (0.4-1.3 s on a host running parallel builds), and 8 of 8 on 153d5d469. The host has an address, so the cases ran with event 3 and the test did not skip. - `ctest -R 'Net|net'` on eea428ca8 and on 922abe6a4: 9 of 9 passed (`guest_net_ctl`, `guest_sce_net`, `guest_netctl_ap_dialog`, `guest_datagram_*`, `guest_socket_host_write_*`). - `python tools/check_conventions.py --base upstream/main` (the tools from `main`): passed. - `git merge-tree` against the open PRs that touch the same files (#6044, #6039, #6023, #5995, #5991, #5967, #5966, #5965, #5928, #5836, #5659, #5611, #5473; #5536 has since merged and is in the base): the only conflict this change adds is the #6039 append to the debt file, described above. Not tested: Linux and macOS (there `APS5_VABI` is empty and the test fails at runtime instead of at compile time), and any title. I did not run Minecraft Preview. ### 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 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] Unblocks: PPSA19639 #1277 - [x] GPUs tested: n/a (no AGC, shader recompiler or VideoOut change) - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-14 23:16:33 +00:00 (Migrated from github.com)

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


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 6685 core/libs/tests/GuestNetCtl.cpp L20-21, L24, L91-92
docs/dev/debt/libSceNetCtl.md L9
PR 6691 core/libs/tests/GuestNetCtl.cpp L24, L92
PR 6720 docs/dev/debt/libSceNetCtl.md L9

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/tests/GuestSceNet.cpp
PR 6039 docs/dev/debt/libSceNetCtl.md
PR 6044 core/libs/tests/GuestNetCtl.cpp
core/libs/tests/GuestSceNet.cpp
PR 6977 core/libs/SceTypes.hpp
PR 7201 core/libs/prx/libSceNetCtl/Export.cpp
PR 7332 core/libs/prx/libSceNetCtl/Export.cpp
core/libs/tests/GuestNetCtl.cpp
docs/dev/debt/libSceNetCtl.md
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6180599754 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6138#issuecomment-6180599754). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-14T23:16:33Z. --- <!-- pr-overlap --> 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 6685](https://www.github.com/boykopovar/AnyPS5/pull/6685) | `core/libs/tests/GuestNetCtl.cpp` [L20-21](https://github.com/boykopovar/AnyPS5/blob/f890e21a81a068ef8bee64a0c319dce23ad239cc/core/libs/tests/GuestNetCtl.cpp#L20-L21), [L24](https://github.com/boykopovar/AnyPS5/blob/f890e21a81a068ef8bee64a0c319dce23ad239cc/core/libs/tests/GuestNetCtl.cpp#L24), [L91-92](https://github.com/boykopovar/AnyPS5/blob/f890e21a81a068ef8bee64a0c319dce23ad239cc/core/libs/tests/GuestNetCtl.cpp#L91-L92)<br>`docs/dev/debt/libSceNetCtl.md` [L9](https://github.com/boykopovar/AnyPS5/blob/f890e21a81a068ef8bee64a0c319dce23ad239cc/docs/dev/debt/libSceNetCtl.md#L9) | | | | [PR 6691](https://www.github.com/boykopovar/AnyPS5/pull/6691) | `core/libs/tests/GuestNetCtl.cpp` [L24](https://github.com/boykopovar/AnyPS5/blob/f890e21a81a068ef8bee64a0c319dce23ad239cc/core/libs/tests/GuestNetCtl.cpp#L24), [L92](https://github.com/boykopovar/AnyPS5/blob/f890e21a81a068ef8bee64a0c319dce23ad239cc/core/libs/tests/GuestNetCtl.cpp#L92) | | | | [PR 6720](https://www.github.com/boykopovar/AnyPS5/pull/6720) | `docs/dev/debt/libSceNetCtl.md` [L9](https://github.com/boykopovar/AnyPS5/blob/f890e21a81a068ef8bee64a0c319dce23ad239cc/docs/dev/debt/libSceNetCtl.md#L9) | | | 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/tests/GuestSceNet.cpp` | | [PR 6039](https://www.github.com/boykopovar/AnyPS5/pull/6039) | `docs/dev/debt/libSceNetCtl.md` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/GuestNetCtl.cpp`<br>`core/libs/tests/GuestSceNet.cpp` | | [PR 6977](https://www.github.com/boykopovar/AnyPS5/pull/6977) | `core/libs/SceTypes.hpp` | | [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) | `core/libs/prx/libSceNetCtl/Export.cpp` | | [PR 7332](https://www.github.com/boykopovar/AnyPS5/pull/7332) | `core/libs/prx/libSceNetCtl/Export.cpp`<br>`core/libs/tests/GuestNetCtl.cpp`<br>`docs/dev/debt/libSceNetCtl.md` |
isnisk commented 2026-10-15 14:50:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by isnisk on 2026-10-15T14:50:19Z.


Checked on Linux: Ubuntu 26.04 (WSL2), GCC 15.2.0, Release, this head (a5e4164ed) merged onto main 489466d3f (clean merge).

  • With the change: guest_net_ctl, guest_sce_net and guest_netctl_ap_dialog pass, 3 rounds; no warnings from the changed files.
  • Without it (main's Export.cpp, the new test and the NetCtlCallback type change kept): guest_net_ctl aborts 3 of 3. As you expected, on Linux it compiles and fails at runtime instead of at compile time.
  • check_conventions.py --base origin/main passes.

Not checked: macOS, the title.

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6194962293 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6138#issuecomment-6194962293). Originally posted by [isnisk](https://github.com/isnisk) on 2026-10-15T14:50:19Z. --- Checked on Linux: Ubuntu 26.04 (WSL2), GCC 15.2.0, Release, this head (`a5e4164ed`) merged onto `main` `489466d3f` (clean merge). - With the change: `guest_net_ctl`, `guest_sce_net` and `guest_netctl_ap_dialog` pass, 3 rounds; no warnings from the changed files. - Without it (`main`'s `Export.cpp`, the new test and the `NetCtlCallback` type change kept): `guest_net_ctl` aborts 3 of 3. As you expected, on Linux it compiles and fails at runtime instead of at compile time. - `check_conventions.py --base origin/main` passes. Not checked: macOS, the title. AI-assisted: yes (Claude Code).
adriadam10 commented 2026-10-17 00:07:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-17T00:07:13Z.


Checked the diff against KytyPS5 NetCtlCheckCallback/RegisterCallback and the debt entry, and built and ran guest_net_ctl + guest_sce_net on the merged tree (35/35 in scope pass; guest_net_ctl fails with the non-test changes reverted, matching isnisk's Linux run). Callbacks run outside the lock, so the self-unregister case is covered; merging once the Windows build is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6227081663 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6138#issuecomment-6227081663). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-17T00:07:13Z. --- Checked the diff against KytyPS5 `NetCtlCheckCallback`/`RegisterCallback` and the debt entry, and built and ran `guest_net_ctl` + `guest_sce_net` on the merged tree (35/35 in scope pass; `guest_net_ctl` fails with the non-test changes reverted, matching isnisk's Linux run). Callbacks run outside the lock, so the self-unregister case is covered; merging once the Windows build is green.
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!6138
No description provided.