fix(libkernel): give each nested TimedWait its own waiter on Windows #1476
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1476"
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 awake-devel on 2026-10-08T08:49:12Z.
What
On Windows, a
sceKernelRaiseExceptionhandler that waits could lose wakeups for the thread it interrupted and for other threads.Each thread had one
TimedWaitwaiter (an event, a timer and the queue links) and used it for every wait. A raise on a thread that is blocked in aTimedWaitcondition runs the handler as an APC inside that wait (#760), while the waiter is still linked into the condition's queue. If the handler waits too (sceKernelSyncOnAddressWait, a semaphore, a contended mutex),Condition::enqueuereused that waiter and overwrote its links:NotifyAllthere woke only the interrupted thread, and the others slept until their timeout or forever.A wait now takes the thread's first waiter that is not in use, and creates a deeper one for a wait nested inside another. Each waiter keeps its own event and timer; the nested ones are freed with the thread's first waiter. Linux is unchanged: its
TimedWait::Conditionis astd::condition_variable_anyandsceKernelRaiseExceptionthrows there.This PR was investigated and written by Claude Code (AI), and posted by me after review.
Tested
Not built or run on Windows: no Windows machine was available. Every changed line is Windows-only (
TimedWait.cppinside#ifdef _WIN32, andguest_raise_exception, which is registered only on Windows).Linux (Arch Linux, GCC, i7 laptop): the preprocessed
TimedWait.cpp(g++ -std=c++20 -E -P) is byte-identical before and after this change, so the Linux build and test suite are unaffected. The Windows code was not compiled.New phases in
guest_raise_exception(the handler waits insceKernelSyncOnAddressWait), not yet run; onmainboth are expected to fail, the first by a thread that never returns fromsceKernelWaitSema, the second by the interrupted thread never returning:No title tested.
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
https://claude.ai/code/session_01HeC9bA3n74TzJRYv9AgBE8
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-08T09:00:03Z.
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.
main)core/libs/tests/GuestRaiseException.cppL21Imported from boykopovar/AnyPS5. Originally posted by popcoder565 on 2026-10-08T09:43:59Z.
Tested on Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release build, Intel Core i7-12700H.
This PR merged onto
main3e32797cguest_raise_exception, 7 runs: 5 passed, including both new phases, and 2 timed out.mainwithout this PR too; it is the lost-delivery bug #1421 fixes.TimedWait.cppput back tomain, test kept): 2 of 5 runs hung inNestedWaitKeepsLaterWaiters(7 threads started). The other 3 hung in the busy-thread phase.agc_driver_srgb8_color_target(#1434, an NVIDIA import flake).Together with #1421 (merged cleanly)
ctest -R ^guest_raise_exception$ --repeat until-fail:20: 20 of 20 pass, 9.4 to 10.1 s each.NestedWaitKeepsLaterWaiters(9 threads started).NestedWaitKeepsOuterWakehangs in all 3 runs (8 threads started).Requirefailures.ExpectReturnedtries 5000 times withsleep_for(1 ms), and each step takes 15.6 ms on Windows (#1471), so it outlasts the 30 s timeout.The same bug in
TimedWait's sleepsSleepUntil,PollSleepUntilandSleepNanosCoarseeach keep onethread_localtimer and wait on it throughTimerWait.sceKernelUsleep(1000000)and is raised, with a handler that sleeps insceKernelUsleep(20000). With this PR and #1421 it fails on every run. The handler runs once, but the 1 s sleep has not returned several seconds later, and the thread is still in its wait.TimerWaitits own timer. With that, it passed 3 of 3 runs, and the 1 s sleep returned after 1000.0 to 1000.2 ms:The test phase I used
It runs with
SleepingHandlerinstalled, after the nested-wait phases.sceKernelUsleepis declared like the other imports; with the unpatched libraries the test links against, the symbol issceKernelUsleep_nid_postfix.This is the same kind of bug this PR fixes, in a different path, so it could go here. If you'd rather keep this PR as it is, I can send the fix and the phase as a separate PR.
Tested with AI assistance (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by hikazey on 2026-10-08T10:24:25Z.
A second Windows machine, AMD this time: Windows 11 Pro (26200), MinGW-w64 GCC 15.2.0 (WinLibs r7, the CI build), Release, configured like CI, Ryzen 9 5900X (12 cores / 24 threads). This commit merged onto
main73ac9ef7, which merges cleanly, and without #1421.guest_raise_exceptionTimedWait.cppfrommain, new phases keptmainalone: 429/429)The busy-thread hang seen on the i7-12700H (the lost delivery #1421 fixes) didn't happen in any of the 6 runs with this PR here. It may be timing-dependent, so 6 clean runs don't rule it out on this machine. I didn't capture which phase each mutation run hung in, only that all 6 hit the timeout.
AI-assisted: yes (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by popcoder565 on 2026-10-08T11:02:00Z.
I've sent the nested-sleep fix as #1520, with its test in a separate file so it doesn't collide with the phases here. If you'd rather fold it into this PR, say so and I'll close #1520.
Imported from boykopovar/AnyPS5. Originally posted by merwie on 2026-10-08T12:43:08Z.
Tested on Windows 11 Pro 26200, WinLibs GCC 15.2.0 (r7), Release, at
39c3d045:guest_raise_exception: passes 3 of 3 runs (8.4 s). WithTimedWait.cppfrom the merge base and the new test kept, it hits the 30 s ctest timeout 2 of 2 times, so the new phases fail without the change as expected.ctest -j8 -E "agc_driver|graphics": 254/254 passed.sceKernelWaitSemawith no timeout, a raise whose handler loops onsceKernelSyncOnAddressWait32until the main thread wakes it, and the semaphore signaled while the handler waits (20 rounds). Onmainthe outer wait never returns; with this PR it passes 3 of 3.Title: God of War Sons of Sparta (PPSA28997) on Windows. Unity's GC stops threads with
sceKernelRaiseException, and the handlers wait insceKernelSyncOnAddressWait. Without this change the title froze after the intro logos in most runs (the collector thread waiting for a thread whose wakeup had been consumed). With this PR and a separate fix for threads blocked inscePthreadMutexLock(PR to follow), it got past the intro in 6 of 6 runs; with only one of the two it still froze in most runs.I had written the same per-nesting-level waiter fix independently before finding this PR, so I'm not opening a duplicate.
Tested with AI assistance (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T14:48:44Z.
Reviewed the diff: each nested
TimedWaitnow gets its own waiter (own event, timer and queue links),busyis cleared in bothwaitSignalandwaitSignalUntil, and the nested waiters are freed with the thread's first one. Linux is unchanged.Windows evidence in the thread: @hikazey (Win11, Ryzen 9 5900X, 6/6 with the change, 6/6 timeouts with
TimedWait.cppfrommain, full suite 429/429), @merwie (Win11, 3/3, 2/2 timeouts without the change) and @popcoder565 (Win10, mutation check hangs inNestedWaitKeepsLaterWaiters). The Windows CI job is the remaining check; merging once it is green.Heads-up: #1431 also edits
GuestRaiseException.cppand will need a rebase after this lands.