fix(libkernel): deliver raises at the spin that ends a sleep on Windows #12135

Merged
Oscar45397 merged 1 commit from upstream-pr/12135 into main 2026-10-30 16:05:05 +00:00
Oscar45397 commented 2026-10-27 23:18:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Oscar45397 on 2026-10-27T23:18:35Z.


What

On Windows, sceKernelRaiseException at a thread that loops on short sleeps is lost (#12131). A sleep shorter than TimedWait's 0.5 ms LEAD_NANOS, and the last 0.5 ms of a longer one, ends in a YieldProcessor spin in host code with no wait counted. A raise that finds the target there takes the host-code path (return-address breakpoint or sampling), and for a thread looping on sceKernelUsleep(200) that path never delivered it.

  • SleepUntil and SleepNanosUnlessChanged now run that spin under a DeliveryDeferral and drain APCs afterwards, the pattern BeginPark, Unpark and DeferringDelivery already use. A raise that finds the target spinning is queued as an APC and runs when the sleep ends, at most 0.5 ms later.
  • The callers of these sleeps (sleep exports through SleepWake, a timed mutex or rwlock lock on a lock the caller already owns, the TSC frequency probe) hold nothing across the spin that the alertable part of the same sleep does not already hold.
  • The debt entry on where queued deliveries run lists the new point.

Tested

Windows 11 Home 26200, AMD Ryzen 9 5900X, WinLibs GCC 15.2.0 r7, Release, on main f759e165d9. Since rebased onto main bb04a4f1ea without conflicts (the patch is unchanged). There, guest_raise_short_sleep and guest_raise_short_sleep_without_wake pass 5 of 5, the other guest_raise_* tests pass 3 of 3 (including guest_raise_exception), and with TimedWait.cpp put back to main's version both new tests fail in 2 of 2 runs (raise N at a thread sleeping 100 us in a loop was not delivered within 3 s).

  • The repro from #12131 (a thread looping on sceKernelUsleep(200), 200 raises, 3 s allowed per raise): main loses the first raise in 5 of 5 runs; with this change all 200 arrive in 5 of 5 runs (about 0.12 s). Loops on 100 and 400 µs sleeps behave the same. Loops on 10 µs and 1 ms sleeps already deliver on main and are unchanged.
  • New guest_raise_short_sleep and guest_raise_short_sleep_without_wake: 100 raises each at threads looping on 100, 200 and 400 µs sleeps, with sleep wakes on (SleepNanosUnlessChanged) and with APS5_NO_SLEEP_WAKE=1 (SleepUntil). 30 of 30 runs pass in each mode; with the TimedWait.cpp change reverted, both fail at the first or second raise.
  • Non-GPU tests (ctest --test-dir build -E 'agc|shader|recomp|video_?out|gnm|vulkan|spirv|gpu|pm4' -j 8): all 1524 pass except guest_fopen_errors, skipped as on main, and guest_raise_exception (address waking round N: the handler did not run within 5 s), which fails the same way on main (#11813). The other guest_raise_* tests pass.
  • #12005 merged on main without this change still loses the first raise in 5 of 5 runs: its mailbox is emptied in DrainApcs, which a sleep under 0.5 ms never reaches. The two compose: the drain after the spin also empties that mailbox.
  • #11725 (head 1cf6f482c0) now only lowers LEAD_NANOS to 20 µs, which shortens these spins but does not change how they run. The two merge cleanly in either order; with both on main bb04a4f1ea, guest_raise_short_sleep, guest_raise_short_sleep_without_wake and #11725's guest_timed_wait_cpu pass 3 of 3. Landing order: proposed to its author in #11725 (this PR first, as #11725 is on hold).
  • No title was run.

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
  • Fixes: Fixes #12131
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5042570070 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12135). Originally posted by [Oscar45397](https://github.com/Oscar45397) on 2026-10-27T23:18:35Z. --- ### What On Windows, `sceKernelRaiseException` at a thread that loops on short sleeps is lost (#12131). A sleep shorter than `TimedWait`'s 0.5 ms `LEAD_NANOS`, and the last 0.5 ms of a longer one, ends in a `YieldProcessor` spin in host code with no wait counted. A raise that finds the target there takes the host-code path (return-address breakpoint or sampling), and for a thread looping on `sceKernelUsleep(200)` that path never delivered it. - `SleepUntil` and `SleepNanosUnlessChanged` now run that spin under a `DeliveryDeferral` and drain APCs afterwards, the pattern `BeginPark`, `Unpark` and `DeferringDelivery` already use. A raise that finds the target spinning is queued as an APC and runs when the sleep ends, at most 0.5 ms later. - The callers of these sleeps (sleep exports through `SleepWake`, a timed mutex or rwlock lock on a lock the caller already owns, the TSC frequency probe) hold nothing across the spin that the alertable part of the same sleep does not already hold. - The debt entry on where queued deliveries run lists the new point. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows 11 Home 26200, AMD Ryzen 9 5900X, WinLibs GCC 15.2.0 r7, Release, on `main` f759e165d9. Since rebased onto `main` bb04a4f1ea without conflicts (the patch is unchanged). There, `guest_raise_short_sleep` and `guest_raise_short_sleep_without_wake` pass 5 of 5, the other `guest_raise_*` tests pass 3 of 3 (including `guest_raise_exception`), and with `TimedWait.cpp` put back to `main`'s version both new tests fail in 2 of 2 runs (`raise N at a thread sleeping 100 us in a loop was not delivered within 3 s`). - The repro from #12131 (a thread looping on `sceKernelUsleep(200)`, 200 raises, 3 s allowed per raise): `main` loses the first raise in 5 of 5 runs; with this change all 200 arrive in 5 of 5 runs (about 0.12 s). Loops on 100 and 400 µs sleeps behave the same. Loops on 10 µs and 1 ms sleeps already deliver on `main` and are unchanged. - New `guest_raise_short_sleep` and `guest_raise_short_sleep_without_wake`: 100 raises each at threads looping on 100, 200 and 400 µs sleeps, with sleep wakes on (`SleepNanosUnlessChanged`) and with `APS5_NO_SLEEP_WAKE=1` (`SleepUntil`). 30 of 30 runs pass in each mode; with the `TimedWait.cpp` change reverted, both fail at the first or second raise. - Non-GPU tests (`ctest --test-dir build -E 'agc|shader|recomp|video_?out|gnm|vulkan|spirv|gpu|pm4' -j 8`): all 1524 pass except `guest_fopen_errors`, skipped as on `main`, and `guest_raise_exception` (`address waking round N: the handler did not run within 5 s`), which fails the same way on `main` (#11813). The other `guest_raise_*` tests pass. - #12005 merged on `main` without this change still loses the first raise in 5 of 5 runs: its mailbox is emptied in `DrainApcs`, which a sleep under 0.5 ms never reaches. The two compose: the drain after the spin also empties that mailbox. - #11725 (head `1cf6f482c0`) now only lowers `LEAD_NANOS` to 20 µs, which shortens these spins but does not change how they run. The two merge cleanly in either order; with both on `main` bb04a4f1ea, `guest_raise_short_sleep`, `guest_raise_short_sleep_without_wake` and #11725's `guest_timed_wait_cpu` pass 3 of 3. Landing order: proposed to its author [in #11725](https://github.com/boykopovar/AnyPS5/pull/11725#issuecomment-6504967821) (this PR first, as #11725 is on hold). - No title was run. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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] Fixes: Fixes #12131 - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-28 04:34:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T04:34:40Z.

Review: CHANGES_REQUESTED


The Windows short-sleep repro and registered coverage are strong: the report includes repeated failure on main and repeated success with the fix, including the APS5_NO_SLEEP_WAKE=1 path. The code also keeps APC delivery deferred through the spin and drains afterward. The remaining blocker is that this branch is based on 6e9b9134, while current main is 82d5f01f (314 commits ahead). CONTRIBUTING requires branches to be based on current main; please rebase/update and rerun the focused short-sleep tests and conventions check. Since #11725 remains open and edits the same two spin loops, please also verify the updated result against that current overlap.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5677829145 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12135#pullrequestreview-5677829145). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-28T04:34:40Z. **Review:** CHANGES_REQUESTED --- The Windows short-sleep repro and registered coverage are strong: the report includes repeated failure on main and repeated success with the fix, including the `APS5_NO_SLEEP_WAKE=1` path. The code also keeps APC delivery deferred through the spin and drains afterward. The remaining blocker is that this branch is based on `6e9b9134`, while current `main` is `82d5f01f` (314 commits ahead). CONTRIBUTING requires branches to be based on current `main`; please rebase/update and rerun the focused short-sleep tests and conventions check. Since #11725 remains open and edits the same two spin loops, please also verify the updated result against that current overlap.
MarcZX10 commented 2026-10-29 05:58:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T05:58:41Z.

Review: CHANGES_REQUESTED


Re-checked — changes requested

What I checked

  • Reviewed exact head 5ef2cc48d03c4ae2b8c02e7115cc823c3c939e96 against fetched main 2960e9a6e7.
  • The change defers APC delivery across both Windows spin loops and drains afterwards; the new test exercises 100, 200, and 400 microsecond sleeps with sleep wakes enabled and disabled.
  • The current-main merge preview is clean and git diff --check passes. Hosted Windows, conventions, and eligibility checks pass; Linux and specialized jobs were skipped.

Test evidence

The author reports repeated Windows repro/control runs and focused tests on the earlier rebased head 106fa85477, with the failure reproduced on main and fixed by the change. I did not run the Windows runtime tests locally. The current head's report is based on main 319621ba2e, which is 176 commits behind fetched main 2960e9a6e7; please refresh and rerun the focused test and control against the current integration head.

Blocking repository coordination

Open #11725 also changes the same SleepUntil and SleepNanosUnlessChanged loops. The PR body says whichever lands second needs a rebase, but neither PR records an agreed order or Depends on relationship. CONTRIBUTING.md requires the authors to agree which goes first and record that order. Please coordinate with #11725’s author and update the checklist before merge.

I am requesting changes for current-head validation and the unresolved overlap order, not for a confirmed defect in the APC deferral logic.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5693482538 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12135#pullrequestreview-5693482538). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-29T05:58:41Z. **Review:** CHANGES_REQUESTED --- # Re-checked — changes requested ## What I checked - Reviewed exact head `5ef2cc48d03c4ae2b8c02e7115cc823c3c939e96` against fetched `main` `2960e9a6e7`. - The change defers APC delivery across both Windows spin loops and drains afterwards; the new test exercises 100, 200, and 400 microsecond sleeps with sleep wakes enabled and disabled. - The current-main merge preview is clean and `git diff --check` passes. Hosted Windows, conventions, and eligibility checks pass; Linux and specialized jobs were skipped. ## Test evidence The author reports repeated Windows repro/control runs and focused tests on the earlier rebased head `106fa85477`, with the failure reproduced on main and fixed by the change. I did not run the Windows runtime tests locally. The current head's report is based on main `319621ba2e`, which is 176 commits behind fetched `main` `2960e9a6e7`; please refresh and rerun the focused test and control against the current integration head. ## Blocking repository coordination Open #11725 also changes the same `SleepUntil` and `SleepNanosUnlessChanged` loops. The PR body says whichever lands second needs a rebase, but neither PR records an agreed order or `Depends on` relationship. `CONTRIBUTING.md` requires the authors to agree which goes first and record that order. Please coordinate with #11725’s author and update the checklist before merge. I am requesting changes for current-head validation and the unresolved overlap order, not for a confirmed defect in the APC deferral logic.
MarcZX10 commented 2026-10-30 01:57:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-30T01:57:38Z.

Review: APPROVED


Re-checked — new evidence, looks good

The authors have now recorded the agreed landing order: #12135 remains independent and #11725 depends on it.

What I ran

Reviewed exact head 3d3fba812c77c083fb422085cf12fcec44a2b750 against recorded PR base bb04a4f1ea0175e9e4f149f0359a764d62ff3296 and current main 99b571f879cd1ef3de7b8af21261675826d75943. The current-main merge preview is clean (f1f4f3c153974b24b37513079a72d449b2bc291c). The changed TimedWait.cpp and GuestRaiseShortSleep.cpp are unchanged on main since the reported test base; current-main changes to shared CMake/test/debt files merge cleanly. Environment: Linux workspace; no local build or test was run.

git merge-tree --write-tree refs/review/session-main refs/review/session-pr-12135
git diff --check refs/review/session-main...refs/review/session-pr-12135
python3 tools/check_conventions.py --base refs/review/session-main --head refs/review/session-pr-12135
gh pr checks 12135 --repo boykopovar/AnyPS5

Results

Check Result
Current-main merge preview Clean
Diff check Passed; no output
Conventions Passed
Hosted Windows Passed
Hosted conventions, labels, eligibility, report Passed
Hosted Linux and specialized jobs Skipped
Focused Windows tests and main-source negative controls Reported by the PR author and an independent tester; not rerun locally
Unedited command output
$ git merge-tree --write-tree refs/review/session-main refs/review/session-pr-12135
f1f4f3c153974b24b37513079a72d449b2bc291c
$ git diff --check refs/review/session-main...refs/review/session-pr-12135
[no output; exit status 0]
$ python3 tools/check_conventions.py --base refs/review/session-main --head refs/review/session-pr-12135
All conventions checks passed.

$ gh pr checks 12135 --repo boykopovar/AnyPS5
conventions	pass	16s	https://github.com/boykopovar/AnyPS5/actions/runs/40930658297/job/123647882626
labels	pass	6s	https://github.com/boykopovar/AnyPS5/actions/runs/40930658216/job/123647883181
windows	pass	9m44s	https://github.com/boykopovar/AnyPS5/actions/runs/40930583615/job/123647708141
linux	skipping	0	https://github.com/boykopovar/AnyPS5/actions/runs/40930583615/job/123647711738
register-context	skipping	0	https://github.com/boykopovar/AnyPS5/actions/runs/40930583615/job/123647711465
relinker	skipping	0	https://github.com/boykopovar/AnyPS5/actions/runs/40930583615/job/123647712116
relinker-asan	skipping	0	https://github.com/boykopovar/AnyPS5/actions/runs/40930583615/job/123647712497
write-watch	skipping	0	https://github.com/boykopovar/AnyPS5/actions/runs/40930583615/job/123647711015
eligibility	pass	6s	https://github.com/boykopovar/AnyPS5/actions/runs/40930583615/job/123647623069
report	pass	40s	https://github.com/boykopovar/AnyPS5/actions/runs/40930583243/job/123647621429

Sources

Not verified

I did not run the focused tests locally. The PR's test reports are on the recorded base and are not physical-console tests.

To get this to a good state

Proceed with #12135 before #11725 as documented. I will re-check the same source and checks if the head changes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5707232424 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12135#pullrequestreview-5707232424). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-30T01:57:38Z. **Review:** APPROVED --- # Re-checked — new evidence, looks good The authors have now recorded the agreed landing order: #12135 remains independent and #11725 depends on it. ## What I ran Reviewed exact head `3d3fba812c77c083fb422085cf12fcec44a2b750` against recorded PR base `bb04a4f1ea0175e9e4f149f0359a764d62ff3296` and current `main` `99b571f879cd1ef3de7b8af21261675826d75943`. The current-main merge preview is clean (`f1f4f3c153974b24b37513079a72d449b2bc291c`). The changed `TimedWait.cpp` and `GuestRaiseShortSleep.cpp` are unchanged on `main` since the reported test base; current-main changes to shared CMake/test/debt files merge cleanly. Environment: Linux workspace; no local build or test was run. ```sh git merge-tree --write-tree refs/review/session-main refs/review/session-pr-12135 git diff --check refs/review/session-main...refs/review/session-pr-12135 python3 tools/check_conventions.py --base refs/review/session-main --head refs/review/session-pr-12135 gh pr checks 12135 --repo boykopovar/AnyPS5 ``` ## Results | Check | Result | |---|---| | Current-main merge preview | Clean | | Diff check | Passed; no output | | Conventions | Passed | | Hosted Windows | Passed | | Hosted conventions, labels, eligibility, report | Passed | | Hosted Linux and specialized jobs | Skipped | | Focused Windows tests and main-source negative controls | Reported by the PR author and an independent tester; not rerun locally | <details> <summary>Unedited command output</summary> ```text $ git merge-tree --write-tree refs/review/session-main refs/review/session-pr-12135 f1f4f3c153974b24b37513079a72d449b2bc291c $ git diff --check refs/review/session-main...refs/review/session-pr-12135 [no output; exit status 0] $ python3 tools/check_conventions.py --base refs/review/session-main --head refs/review/session-pr-12135 All conventions checks passed. $ gh pr checks 12135 --repo boykopovar/AnyPS5 conventions pass 16s https://github.com/boykopovar/AnyPS5/actions/runs/40930658297/job/123647882626 labels pass 6s https://github.com/boykopovar/AnyPS5/actions/runs/40930658216/job/123647883181 windows pass 9m44s https://github.com/boykopovar/AnyPS5/actions/runs/40930583615/job/123647708141 linux skipping 0 https://github.com/boykopovar/AnyPS5/actions/runs/40930583615/job/123647711738 register-context skipping 0 https://github.com/boykopovar/AnyPS5/actions/runs/40930583615/job/123647711465 relinker skipping 0 https://github.com/boykopovar/AnyPS5/actions/runs/40930583615/job/123647712116 relinker-asan skipping 0 https://github.com/boykopovar/AnyPS5/actions/runs/40930583615/job/123647712497 write-watch skipping 0 https://github.com/boykopovar/AnyPS5/actions/runs/40930583615/job/123647711015 eligibility pass 6s https://github.com/boykopovar/AnyPS5/actions/runs/40930583615/job/123647623069 report pass 40s https://github.com/boykopovar/AnyPS5/actions/runs/40930583243/job/123647621429 ``` </details> ## Sources - [Timed-wait implementation at this head](https://github.com/boykopovar/AnyPS5/blob/3d3fba812c77c083fb422085cf12fcec44a2b750/core/libs/prx/libkernel/Time/TimedWait.cpp) - [Short-sleep regression tests at this head](https://github.com/boykopovar/AnyPS5/blob/3d3fba812c77c083fb422085cf12fcec44a2b750/core/libs/tests/GuestRaiseShortSleep.cpp) - [Recorded order and dependency update](https://github.com/boykopovar/AnyPS5/pull/12135#issuecomment-6512939629) ## Not verified I did not run the focused tests locally. The PR's test reports are on the recorded base and are not physical-console tests. ## To get this to a good state Proceed with #12135 before #11725 as documented. I will re-check the same source and checks if the head changes.
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!12135
No description provided.