fix(libkernel): give a sleep nested in an APC its own timer on Windows #1520
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1520"
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 popcoder565 on 2026-10-08T11:01:47Z.
What
On Windows, a
sceKernelRaiseExceptionhandler that sleeps left the thread it interrupted asleep for good, if that thread was itself sleeping.SleepUntil,PollSleepUntilandSleepNanosCoarseeach wait on onethread_localhigh-resolution timer throughTimerWait. When a thread is blocked in an alertableTimedWaitwait, a raise runs the handler as an APC inside that wait (#760). If the thread was sleeping and the handler sleeps too, the handler'sTimerWaitre-arms the same timer. The handler's own wait then consumes the signal, because it is a synchronization timer. When the handler returns, the interrupted sleep waits again on a timer that will not fire, so it never returns.TimerWaitnow counts how deep it is on the thread. A nested call creates its own timer for that wait and closes it afterwards. The outermost call keeps using the thread's timer, so nothing changes outside an APC.I found this while testing #1476 on Windows; #1476 fixes the same reuse for
Conditionwaiters. Unity's GC handler waits insceKernelSyncOnAddressWait, which is #1476's case, so this one only bites a handler that sleeps. If #1476's author would rather fold this in, I'll close this PR.The new test
guest_raise_nested_sleepis Windows only, likeguest_raise_exception. A thread sleeps 1 s insceKernelUsleep, a raise runs a handler that sleeps 20 ms, and the 1 s sleep must return within 1 to 2 s. It has its own file, so it doesn't add to the overlapping changes inGuestRaiseException.cpp.Tested
Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, Intel Core i7-12700H.
guest_raise_nested_sleepAPS5_COARSE_TIMED_WAITS=1main4a14093a, without the fixFull ctest: 437 of 438 pass. The one failure is
agc_driver_srgb8_color_target(#1434).Not run on Linux. The change is inside
#ifdef _WIN32, and the test is registered on Windows only.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by klosixgaming-arch on 2026-10-08T11:23:24Z.
Review: APPROVED
Excellent fix for a subtle Windows-specific concurrency bug. This addresses an important edge case that could cause threads to hang indefinitely when sleeping inside exception handlers.
Strengths:
TimerWaitcalls reusing the same thread-local timer, causing the handler's wait to consume the signal meant for the interrupted sleepNestingstruct) with proper cleanup and depth trackingTechnical notes:
This will improve reliability for titles that use exception handlers with sleep operations. Ready to merge.
Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-08T18:46:58Z.
Verified from the outside, because this one cannot be tested on Linux at all (its CMake target does not exist there —
ninja: unknown target 'guest_raise_nested_sleep_tests'on this machine, since the block is Windows-only), so the Windows CI job is the only place it can run:So the test is registered and executed rather than skipped — which is worth stating explicitly, because a test that exits 77 or is never run still leaves the job green (that is the hole #1028 addresses).
The change itself reads correctly:
Nestingincrementsdepthin its constructor, sodepth > 1is true exactly for a wait nested inside another on the same thread, and the nesting's own timer is closed in the destructor after the wait has returned. Replacing theWaiterlinked-list of nested waiters withthread_localdepth also removes the allocation per nesting level.One small question, the only silent path I can see: if
CreateHighResolutionTimer()fails,ownis null andtimer = nesting.ownpoints the nested wait at no timer, so that level falls back to the thread's shared one — the original behaviour, but now silent. Should that be a hard failure, or is falling back the intended degradation to record in the debt file?Written with AI assistance.
Imported from boykopovar/AnyPS5. Originally posted by popcoder565 on 2026-10-09T02:56:09Z.
@Dextroid17 thanks for checking it.
If the nested timer can't be created: the nested wait doesn't fall back to the shared timer.
timer = nesting.ownsets it to null, andif (!timer) return false;returns beforeSetWaitableTimer. All three callers (SleepNanosCoarse,SleepUntil,PollSleepUntil) then sleep withAlertableSleepin whole milliseconds, the same fallback they already take when the thread's own timer is null.Creating the timer fails only when the process runs out of handles or memory, or on Windows 10 before 1803, which doesn't support
CREATE_WAITABLE_TIMER_HIGH_RESOLUTION. On those versions every sleep already takes the millisecond path. So I'd keep the fallback: it's coarser, but it never hangs.Rechecked after #1476 merged (Windows 10 19045, GCC 15.2.0 WinLibs r7, Release, this PR merged onto
mainf1451396):guest_raise_nested_sleeppasses 3/3 (1.04–1.06 s), andguest_raise_exceptionpasses 3/3.TimedWait.cppfrommain:guest_raise_nested_sleepfails 2/2, because the outer sleep still hasn't returned 5 s after the handler ran.#1476 gives each nested condition wait its own waiter and timer. The sleep paths still share one timer per thread, and that's what this PR covers.
🤖 Addressed by Claude Code
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T01:07:43Z.
Checked the diff (nesting depth per thread, nested wait owns and closes its timer, outermost keeps the thread's timer; failure to create it falls back to the millisecond sleep in all three callers). Local Linux build and ctest are unaffected (the code and the test are Windows-only); the Windows job on this head runs and passes
guest_raise_nested_sleep, as @Dextroid17 confirmed from the log, and the author's before/after runs show it aborting without the change. Merging.