fix(libkernel): wake timed sleeps for thread cancellation #8916
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8916"
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 Henryisdjfunkyho on 2026-10-21T06:00:01Z.
What
An enabled pthread cancellation request during a long POSIX
usleep,nanosleepor_nanosleeppreviously waited for the full timeout. Waits longer than 1 ms now wake for enabled cancellation; shorter waits keep the existing GPU completion wake path and check cancellation before and after waiting. This follows POSIX.1-2024 section 2.9.5 (https://pubs.opengroup.org/onlinepubs/9799919799/functions/V2_chap02.html).sceKernelUsleep,sceKernelNanosleepandsceKernelSleepretain the prior wait path. Six registered deferred-cancellation tests cover all three SCE APIs in normal and coarse modes. The tests observe entry after the initial cancellation check, then require a canceled result, exactly one cleanup call, no normal return, and cleanup no earlier than the full one-second timeout. Ready, wait-entry and completion handshakes have three-second deadlines; each process retains its 20-second CTest timeout. POSIXnanosleephost waits now update the existing per-thread wait counter, matching the Windows sleep tracking used for this handshake. The POSIX cancellation matrix remains registered.GuestSleepCancel.cppuses the canonical API header declarations from currentmain; its duplicate test-local export declarations have been removed. All test cases and assertions are preserved.The firmware 13.42 disassembly and emulated checks reported by BunnyFl support the SCE cancellation checks before and after sleep. The remaining unmeasured part is whether cancellation arriving mid-sleep interrupts the PS5 kernel sleep syscall. The six SCE deferred tests preserve this tree's current model until that kernel behavior is measured. No title unblock is claimed.
Tested
Fresh validation of
42bbc9823592f50de423d73d743e9bee42b7be5f, withmainthroughcb45ae2ddf6e113a4619e8fed91fe7b9333c5c0fmerged into the branch. The conflict was between adjacent debt entries after #11201 merged; its measured thread-creation details and this PR's cancellation entry are both preserved. Bundled dependencies, Release, full runtime configuration withBUILD_TESTING=ON; only the focused targets and tests below were built and run.6.18.54.1-microsoft-standard-WSL2, GCC 15.2.0, CMake 4.2.3: 49/49 focused cases passed, no failures or skips. The one-case difference is the Windows-onlyguest_nanosleep_timer_self_testregistration.guest_sleep_cancel_*cases, including all six SCE deferred cases. Each SCE case also passed five consecutive executions on each OS, 30 executions per OS.4d8f05891band5d0ddabbde. The earlier contributor's 48-case report testedaa1e8d27e6and is not the validation result for this head.Windows commands, from the worktree root:
Linux commands, from the isolated source checkout:
Public-header checks, Windows and Linux respectively:
Negative control on Linux: in an isolated validation copy, replacing
SleepWake::Sleep(nanos)insideSleepUntilCompletewithCancellableSleep(nanos)made all six SCE cases fail the elapsed-time assertion atGuestSleepCancel.cpp:139. Restoring the exact source, rebuilding, and rerunning the complete focused matrix passed 49/49 again. The mutation is not in this patch.The long-nanosleep overflow clamp already in
mainremains intact;guest_nanosleep_long_requestpasses on both systems.python tools/check_conventions.py --base origin/main --head HEADandgit diff --check origin/main...HEADpass. The earlier header merge received an independent Luna review with no outstanding findings. I reviewed this debt-entry conflict resolution directly, without a subagent; no new independent review is claimed for this merge.PR #11201 has merged into main; its measured thread-create/detach entry is preserved alongside this PR's cancellation entry. The refreshed overlap scan flags adjacent debt edits in #7135, which changes cpuset entries, and #10856, which adds a separate cancellation-wait-registration entry. I inspected these hunks; both entries must be preserved when merging. Neither is a semantic prerequisite for this sleep fix. Older PRs with conflicting base histories still prevent complete automated comparisons; no exhaustive combined-PR validation is claimed.
No full repository test-suite, macOS, console, title, GPU or performance result is claimed. Full GitHub runtime CI requires a collaborator's manual Build workflow run under the contribution policy.
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)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T06:01:41Z.
I checked the shared
CancellableSleeppath and the new test matrix. The POSIX cancellation-point behavior is supported by the cited contract, but the same change also makessceKernelUsleep,sceKernelNanosleepandsceKernelSleepwake on cancellation; the debt entry says those SCE cancellation semantics have not been measured. Could the implementation keep the new interruptible wait on the POSIX aliases and preserve the current SCE behavior until there is console evidence, or point to the project policy/reference that supports applying the POSIX rule to those exports? The test coverage can still exercise the established POSIX paths either way.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T06:02:48Z.
I also checked the exact PR head
722e494d98from a fresh Linux worktree.python3 tools/check_conventions.py --base upstream/main --head HEADandgit diff --check upstream/main...HEADboth pass. I could not configure the full Linux build: the SDL submodule is uninitialized and this host does not have SDL XRandR support. I did not run the focused runtime tests here.Imported from boykopovar/AnyPS5. Originally posted by Henryisdjfunkyho on 2026-10-21T07:30:20Z.
Thanks for flagging this. I’ve limited cancellation-aware behavior to POSIX usleep, nanosleep, and _nanosleep. The sceKernel* calls keep their previous behavior until we can check their cancellation semantics on console. I added tests for that and recorded the open question in the libkernel debt notes. Could you take another look?
Imported from boykopovar/AnyPS5. Originally posted by Filip722 on 2026-10-21T10:00:47Z.
Linux runtime validation of current head
aa1e8d27e6e628ba58366dc41daa7dcca236db0a: all 48 cases registered by the focused selection on Linux passed, with no failures or skips. The six blocked POSIX cases also each passed three consecutive executions.This exercises the updated split from the review follow-up:
usleep,nanosleep, and_nanosleep: blocked cancellation, disabled cancellation, ordinary completion, pending cancellation at zero duration, and pending cancellation before a long wait.sceKernelUsleep,sceKernelNanosleep,sceKernelSleep, both registrations): the thread remains unfinished during the early observation and completes the requested sleep before pending cancellation is delivered.guest_nanosleep, pthread cancellation/recovery, andsleepcontrols remain passing.Negative control: kept the PR tests, replaced only
Time.cppwith merge-baseb77bd58806a5d9a7ecc91ee7290728a4d2ff4d6d, and rebuilt libkernel.guest_sleep_cancel_usleep_blockedfails after the ten-second sleep withsleep stayed blocked after cancellationandGuestSleepCancel.cpp:132: requirement failed. Restoring the exact PR source and rebuilding makes the unchanged case pass again.Environment: Debian 13.7 x86-64, Linux
7.2.9-x64v3-xanmod1, AMD Ryzen 5 7640HS, GCC 15.2.0, CMake 3.31.6. Full Release configuration,BUILD_TESTING=ON,ANYPS5_RELINKER_ONLY=OFF, bundled dependencies. Built libkernel and all four focused executables plus dependencies with two jobs.Tracked source restored to the named head. No new Windows/macOS, console, title, GPU or full repository test-suite run. This adds actual Linux runtime coverage beyond the earlier configure/syntax-only checks; it does not establish SCE cancellation semantics on console hardware.
AI-assisted testing and report.
Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T13:24:46Z.
Review: COMMENTED
Checked against main
6cd36e6517and FreeBSD 11.4 lib/libthr (thr_syscalls.c__thr_nanosleepwith_thr_cancel_enter/_thr_cancel_leave(curthread, 1)):usleep/nanosleep/_nanosleepgo throughCancellableSleep, which only checks before and after the host sleep. The new long-wait loop mirrorssleep_nid_postfixand looks right to me, and making these POSIX calls cancellation points matches libthr.foreach(sleepApi IN ITEMS sce_usleep sce_nanosleep sce_sleep)..._deferredtests, but the current head isn't a descendant of it: atc7e2611the CMake loop only coversusleep nanosleep _nanosleep, and thedeferredmode is gone from GuestSleepCancel.cpp. So the 48-case Linux run above was for an earlier head, and nothing currently pins the "longer SCE waits do not wake on cancellation" debt wording. Could you restore those registrations?sce_*branches inSleep()(GuestSleepCancel.cpp:50-57) and thesceKernelSleepdeclaration are unreachable and could be removed.Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.