feat(libSceSaveData): deliver backup completions to a registered event callback #13551
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/13551"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-30T18:03:50Z.
What
sceSaveDataRegisterEventCallbackandsceSaveDataUnregisterEventCallbackwere parameterless declarations that threw throughNotImplemented_nid_no_patch. They now take shadPS4's signatures and follow its #5226 ("save_data: deliver backup completion callbacks"), as on itsmain1d8004a22f. That covers registration, unregistration, and delivering each backup completion to the registered callback from a thread the library starts.This PR is stacked on #12748, which adds the type-1 event of
sceSaveDataUmountWithBackup.sceSaveDataRegisterEventCallbacksavedata.cppL1345-L1358SAVE_DATA_ERROR_NOT_INITIALIZED. A null callback:SAVE_DATA_ERROR_PARAMETER. Otherwise it stores the callback and userdata. A second registration replaces both.sceSaveDataUnregisterEventCallbacksavedata.cppL1763-L1768Delivery, as in
DispatchBackupEvent(L315-L339) and the backup thread (save_backup.cppL140-L205).sceSaveDataGetEventResultalso delivers the same event to the callback registered at that point. That's type 2 fromsceSaveDataBackupand type 1 from #12748'ssceSaveDataUmountWithBackup.sceSaveDataConvert's type-5 event isn't a backup completion, and shadPS4 doesn't deliver it either.The thread: shadPS4 runs the callback on an emulated kernel thread, so that it has a guest TLS and stack (L82).
scePthreadCreateat the first successfulsceSaveDataRegisterEventCallbackof an initialization, aslibSceUsbStoragedoes for its monitor callbacks. A title that never registers gets no thread, andsceSaveDataInitialize3only counts, as onmain.Registration against the last terminate: Register takes
callbacks.calling, theng_slots_mutex, before it checks the initialization count and stores the callback. The last terminate holdsg_slots_mutexfrom its check through the detach. So a registration racing the last terminate either completes first and is cleared by it, or getsNOT_INITIALIZED. Callback reentrancy is kept, and the join stays outside the lock.Terminate:
SAVE_DATA_ERROR_BUSY, before any other check, as in shadPS4'ssceSaveDataTerminate(L1704-L1726). It never joins the delivery thread from that thread.Unregister also drops completions not yet delivered, so no callback runs after it returns.
Not measured on a console. The debt entry lists what is unknown there:
It also says PS5's signatures are assumed to be PS4's.
Tested
guest_savedata_event_callback(new) runs in its own process. Every wait is bounded at 5 s on a condition variable, with no sleeps.sceSaveDataInitialize3givesNOT_INITIALIZED;PARAMETER.sceSaveDataBackup: the callback gets exactly one event:NOT_FOUND.sceSaveDataUmountWithBackup: exactly one type-1 event, with the same checks.BUSY;NOT_INITIALIZED;NotImplemented_nid_no_patchand the rest of this change kept (run):guest_savedata_event_callbackfails on both runners, aborting at its first case with "sceSaveDataRegisterEventCallback not implemented".guest_savedata*tests, 1 failed, 0 skipped.guest_savedata*tests each, all 10 runs passed, 0 skipped.--repeat until-fail:20: 20 of 20 passed;ctest -R 'savedata|save_data'once: 14 tests, 14 passed. At629477b800everyguest_savedata*test started and stopped the delivery thread with its initializations; since0d0abe09c1only a registration starts it.check_test_registration.pypasses.check_conventions.pypasses with one warning,[debt-length]on the pending-declarations line, which #12748 already has at a longer length (5,607 characters there, 5,415 here).-fsyntax-only: no new diagnostic.bc6b4d76f9, the registration race (MarcZX10's review, oneandonlydean's point 2):CheckRegisterRacingFinalTerminate: 200 iterations of a registration against the final terminate on two guest threads.calling, so the registration waits there while the last terminate runs. The other 100 are a plain race.Fencecallback, and asserts that the raced callback ran 0 times.--repeat until-fail:50on Linux, and ×10 on GitHub's runners (run): 13guest_savedata*tests each on Linux and Windows, all passing, 0 skipped.0d0abe09c1, the delivery thread at the first registration (oneandonlydean's point 1):CheckSessionsWithoutRegistration: two initialize/terminate sessions with no registration, each call returning 0.CheckDeliveryThreadStartsAtRegistration: it counts the process's threads in/proc/self/task, asGuestPthreadStack.cppdoes. Initialize3 and a refused registration add none, the first registration adds one, later ones reuse it, and the final terminate joins it.bc6b4d76f9's code the new cases fail 10 of 10 runs, since the thread count there is 2 where the change gives 1.guest_savedata*tests each on Linux and Windows, all passing, 0 skipped.APS5_SKIP_TIMING_TESTS=1, which relaxesprecise_wait's median bounds on GitHub's hosted runners.36b5e96eb1, a merge of #12748's842f75d7e0, which brings #12748's merge ofmain7fcab08f71and with it JJenkx's #13324 (run).isMountedDirectorycalls, in Check and Restore, take #13324's newstd::error_codeparameter and returnSAVE_DATA_ERROR_INTERNALon a host filesystem error instead of throwing, consistent with #13324.CheckBackupsnow writes param.json at the resolved/app0path, since #13324's test fixes the guest root in a subdirectory first.^guest_savedata_and^savedata_withguest_savedata_event_callback,guest_savedata_mount_statusamong them.APS5_SKIP_TIMING_TESTS=1, which relaxesprecise_wait's median bounds on GitHub's hosted runners.Claim check (rerun 2026-10-30 17:49-17:53Z):
Overlap:
git merge-treeof this head:main, clean;62ab6243cb, a merge of #12748's9b9e850d90, oneandonlydean's merge ofmaind8b60b46edinto #12748. It brings #12748's resolution of the one conflict withmain: inlibSceSaveData.native/Export.cpp's include block, #8285's<string_view>sits beside this branch's includes, and both are kept. It merged without conflicts, and this PR's own diff is unchanged.guest_savedata_*tests,guest_savedata_event_callbackamong them. Linux: 9 run, 9 pass, 0 skip, 0 fail. Windows: 9 run, 9 pass, 0 skip, 0 fail. The libc and libkernel suites run 208 and 309 tests on Linux and 235 and 330 on Windows, with 0 failed. The suites ran withAPS5_SKIP_TIMING_TESTS=1, which relaxesprecise_wait's median bounds on GitHub's hosted runners.df0b6d335c, a merge ofmain3c3b3cda73(run). The one conflict was the pending-declarations list indocs/dev/debt/libSceSaveData.native.md: MarcZX10's #10674 movessceSaveDataGetProgressout of it into its own entry, and the merge keeps that with this branch's removal of the five calls it and #12748 implement. This PR's diff over #12748 is unchanged. The^guest_savetests, withguest_savedata_event_callbackandguest_savedata_mount_status, pass 10 runs in a row. Linux: 9 run, 9 pass, 0 skip, 0 fail. Windows: 9 run, 9 pass, 0 skip, 0 fail. Suites: libc and libkernel pass on Linux (214 and 316) and on Windows (241 and 337). The suites ran withAPS5_SKIP_TIMING_TESTS=1, which relaxesprecise_wait's median bounds on GitHub's hosted runners.4de3c5975a, a merge of #12748's02af078a35, oneandonlydean's merge ofmainb28bb77217into #12748 (run). The one conflict was the pending-declarations list indocs/dev/debt/libSceSaveData.native.md, next tomain'ssceSaveDataGetProgressentry, which xecedemon's #15077 extends. The merge keeps that entry with this branch's removal of the five calls it and #12748 implement. This PR's diff over #12748 is unchanged. The^guest_savetests, withguest_savedata_event_callbackandguest_savedata_mount_status, pass 10 runs in a row. Linux: 9 run, 9 pass, 0 skip, 0 fail. Windows: 9 run, 9 pass, 0 skip, 0 fail. Suites: libc and libkernel pass on Linux (217 and 319) and on Windows (244 and 340). The suites ran withAPS5_SKIP_TIMING_TESTS=1, which relaxesprecise_wait's median bounds on GitHub's hosted runners.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)convert(see #12748), it conflicts insceSaveDataBackup, where this PR builds the backup event and callsDeliverEventafter the scopedg_state_mutexlock. Whichever lands second keeps #15042's copied-intitle_idand directory name, with this PR's scoped lock and theDeliverEventcall after it.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-30T18:19:25Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
The event callback registration can race with final termination, leaving a callback registered after shutdown and allowing it to be reused after reinitialization without a new registration.
What I ran
Reviewed head
629477b800f75f29aac25fb104616d189e8a265bagainst PR base4bc9e779aa7abb2109c65ee3bca19e1b85df3be8; the individual merge preview against that base was clean. The declared order-only dependency chain is #10674 → #12748 → #13551; #12748 and #13551 preview cleanly against currentmain, while #10674 currently conflicts with it. Environment: Linux x86-64, Python 3.11.2, GCC 12.2.0.Results
main; #10674 has a conflict and is declared order-only by #12748. The precheck could not invoke its isolated stack helper, so I reran the stack check directly.passed_with_warnings: 15 heuristic findings. Seven refer to save-data exports from the current base absent from the PR branch tree, but the merge preview is clean; one is the new internal worker function in an anonymous namespace. The audit is not a C++ parser or binary ABI test.possible-unregistered-cpp-testwarnings are false positives: CMake explicitly builds both files and registers CTest entries.Unedited output from focused checks
The convention check emitted this warning and exit summary:
The ABI-audit summary command emitted:
The diff checks exited 0 and emitted no output. The precheck's isolated checkout did not contain the workspace-local ABI, stack, or test-quality helper scripts; I ran those helpers from the workspace against the exact fetched head.
Findings
Blocking
sceSaveDataRegisterEventCallback, theg_initializationscheck happens before taking the callback locks and is not synchronized with the finalsceSaveDataTerminatetransition. If registration reads a nonzero count, then final termination sets it to zero andDetachEventDeliveryclears the callback, registration can subsequently store the callback and return success with no worker. A laterInitialize3starts a worker without clearing that callback, so events may call it after reinitialization even though it was unregistered by termination. Serialize the initialized-state check and callback installation with the final-termination transition, and add a regression test that races registration with final termination and verifies no callback survives a subsequent initialization without registering again.Non-blocking
The declared order-only dependency chain includes #10674, whose branch currently conflicts with
main; this is an integration-order issue, not a request to rebase the dependent PR before its dependency is resolved.Sources
Not verified
I did not compile the change or run its C++ tests locally. Current hosted checks were queued at the final refresh; PR-description test results are author-reported. I did not verify console behavior or the dependency's eventual merge order.
To get this to a good state
Synchronize registration with the final termination lifecycle and add the concurrent regression case. I will rerun the same no-build checks on the new head; build-dependent CI evidence should also be checked before approval.