test(libkernel): keep the raise-in-system-call writer in guest code until each raise lands #10630

Merged
LeandroLP23 merged 3 commits from upstream-pr/10630 into main 2026-10-26 04:36:27 +00:00
LeandroLP23 commented 2026-10-24 13:37:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by LeandroLP23 on 2026-10-24T13:37:34Z.


What

guest_raise_in_system_call (#8637) timed out on the Windows job of 20 different pull requests between 2026-10-21 and 2026-10-24, and once on a main push build. In the failed runs' logs where it passed, it took 0.25–0.78 s, and every failure is a 30.00 s ctest timeout with no output, so the test hangs rather than runs slowly. Six of the timeouts came after #10076 merged.

Why it hangs. I printed, in a local build only, the point where RaiseOn decides to deliver. With the test process limited to one core, the first raise is delivered after exactly 1000.0 ms with the writer still inside NtWriteFile (InSystemCall true). That is the 1 s RetryLimit. The handler then runs with the stream lock held, and the main thread's fputc waits for it: the #7652 deadlock that the test is meant to rule out. Run normally, the same raise is delivered in guest code after 252 ms.

The writer could stay in the call that long for two reasons:

  • Each 64 KiB block needed 16 reader wake-ups (4 KiB per 10 ms sleep) before NtWriteFile returned.
  • When the call returned, the writer spent only a fraction of a millisecond in its own code before the next fwrite. When few cores are free, it goes straight on into the next block within the same time slice. So the sampler only ever saw it inside a system call, until the 1 s limit ran out.

The retry limit is the library's design, so this changes the test, not RaiseOn:

  • One write per block: the stream is unbuffered, so each block is one 16 KiB WriteFile into the 4 KiB pipe. The main thread's fputc still waits for the stream lock that fwrite holds.
  • Raise overlapping the blocked call: the reader counts the bytes it reads. When the first byte of a block arrives, the writer is inside WriteFile with more than a pipe's worth still to go. The reader then stops reading and waits until the writer thread's suspend count (NtQueryInformationThread, ThreadSuspendCount) is above zero. Only RaiseOn suspends it, and only while the write is blocked. The reader then holds 100 ms more, twice the 50 ms host-code window, and checks that the write has not returned.
  • Delivery in the test's own code: the handler reads the interrupted rip from the context and requires it to lie in the test executable's image, so a delivery anywhere after fwrite returned passes. The writer then spins in its own code until the raise has been delivered.
  • Controlled delays: guest_raise_in_system_call_delay_raise sleeps 200 ms before the raise, and guest_raise_in_system_call_delay_return spins in guest code between fwrite returning and its completion flag.
  • Watchdog: it now uses a steady_clock deadline. It used to count 25,000 sleep_for(1ms) calls, and each lasts about 15.6 ms at the default Windows timer resolution, so it never fired before ctest's 30 s timeout. A hang now aborts at 25 s.

Tested

Windows 11 26300, AMD Ryzen AI 7 350, WinLibs GCC 15.2, Release, main 6cd36e6517 for the main column, this branch on da6c581a02. "Load" means busy-looping processes pinned to the same cores.

guest_raise_in_system_call main this PR
all cores 5 of 5 pass, 0.51–0.54 s 3 of 3 ctest rounds pass, 1.11–1.26 s
1 core hang in 5 of 5 (30 s) 6 of 6 pass
1 core, 2 load processes hang in 8 of 8 6 of 6 pass
2 cores, 4 load processes 5 of 5 pass, 1.72–2.42 s 6 of 6 pass

The two delay variants pass the same three rounds (_delay_raise 3.14–3.18 s, _delay_return 1.45–1.46 s) and the same 18 loaded runs each.

Controls (local changes, not part of this PR):

  • #8637's extension removed from RaiseOn: all three tests fail in 3 of 3 runs, at 0.27–0.51 s.
  • The previous handler check (written == delivery) put back: _delay_return fails in 3 of 3 runs, the false failure from review.
  • The previous fixed 100 ms hold put back, together with #8637's extension removed: _delay_raise passes in 3 of 3 runs, the false pass from review.

Other checks:

  • ctest -R 'guest_(mutex_)?raise' -j 4 passes 7 of 7.
  • tools/check_conventions.py --base origin/main passes.

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: none
  • Unblocks: none (Windows CI flake)
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4995851509 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10630). Originally posted by [LeandroLP23](https://github.com/LeandroLP23) on 2026-10-24T13:37:34Z. --- ### What `guest_raise_in_system_call` (#8637) timed out on the Windows job of 20 different pull requests between 2026-10-21 and 2026-10-24, and once on a `main` push build. In the failed runs' logs where it passed, it took 0.25–0.78 s, and every failure is a 30.00 s ctest timeout with no output, so the test hangs rather than runs slowly. Six of the timeouts came after #10076 merged. **Why it hangs.** I printed, in a local build only, the point where `RaiseOn` decides to deliver. With the test process limited to one core, the first raise is delivered after exactly 1000.0 ms with the writer still inside `NtWriteFile` (`InSystemCall` true). That is the 1 s `RetryLimit`. The handler then runs with the stream lock held, and the main thread's `fputc` waits for it: the #7652 deadlock that the test is meant to rule out. Run normally, the same raise is delivered in guest code after 252 ms. The writer could stay in the call that long for two reasons: - Each 64 KiB block needed 16 reader wake-ups (4 KiB per 10 ms sleep) before `NtWriteFile` returned. - When the call returned, the writer spent only a fraction of a millisecond in its own code before the next `fwrite`. When few cores are free, it goes straight on into the next block within the same time slice. So the sampler only ever saw it inside a system call, until the 1 s limit ran out. The retry limit is the library's design, so this changes the test, not `RaiseOn`: - **One write per block:** the stream is unbuffered, so each block is one 16 KiB `WriteFile` into the 4 KiB pipe. The main thread's `fputc` still waits for the stream lock that `fwrite` holds. - **Raise overlapping the blocked call:** the reader counts the bytes it reads. When the first byte of a block arrives, the writer is inside `WriteFile` with more than a pipe's worth still to go. The reader then stops reading and waits until the writer thread's suspend count (`NtQueryInformationThread`, `ThreadSuspendCount`) is above zero. Only `RaiseOn` suspends it, and only while the write is blocked. The reader then holds 100 ms more, twice the 50 ms host-code window, and checks that the write has not returned. - **Delivery in the test's own code:** the handler reads the interrupted `rip` from the context and requires it to lie in the test executable's image, so a delivery anywhere after `fwrite` returned passes. The writer then spins in its own code until the raise has been delivered. - **Controlled delays:** `guest_raise_in_system_call_delay_raise` sleeps 200 ms before the raise, and `guest_raise_in_system_call_delay_return` spins in guest code between `fwrite` returning and its completion flag. - **Watchdog:** it now uses a `steady_clock` deadline. It used to count 25,000 `sleep_for(1ms)` calls, and each lasts about 15.6 ms at the default Windows timer resolution, so it never fired before ctest's 30 s timeout. A hang now aborts at 25 s. ### Tested Windows 11 26300, AMD Ryzen AI 7 350, WinLibs GCC 15.2, Release, `main` `6cd36e6517` for the `main` column, this branch on `da6c581a02`. "Load" means busy-looping processes pinned to the same cores. | `guest_raise_in_system_call` | `main` | this PR | |---|---|---| | all cores | 5 of 5 pass, 0.51–0.54 s | 3 of 3 ctest rounds pass, 1.11–1.26 s | | 1 core | **hang in 5 of 5** (30 s) | 6 of 6 pass | | 1 core, 2 load processes | **hang in 8 of 8** | 6 of 6 pass | | 2 cores, 4 load processes | 5 of 5 pass, 1.72–2.42 s | 6 of 6 pass | The two delay variants pass the same three rounds (`_delay_raise` 3.14–3.18 s, `_delay_return` 1.45–1.46 s) and the same 18 loaded runs each. Controls (local changes, not part of this PR): - **#8637's extension removed from `RaiseOn`:** all three tests fail in 3 of 3 runs, at 0.27–0.51 s. - **The previous handler check (`written == delivery`) put back:** `_delay_return` fails in 3 of 3 runs, the false failure from review. - **The previous fixed 100 ms hold put back, together with #8637's extension removed:** `_delay_raise` passes in 3 of 3 runs, the false pass from review. Other checks: - `ctest -R 'guest_(mutex_)?raise' -j 4` passes 7 of 7. - `tools/check_conventions.py --base origin/main` passes. ### 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: none - [x] Unblocks: none (Windows CI flake) - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Code)
MarcZX10 commented 2026-10-24 13:38:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T13:38:28Z.

Review: APPROVED


Reviewed exact head a604ce5effe96b2179ceb2e1d94000f4c73c2ea7. The single-file test change keeps the handler blocked while the main thread calls fputc, while it repeatedly creates a sustained fwrite system-call window for each raise. The explicit writing/deliveries handshake prevents a raise from being sent before the corresponding write starts, and the reader's bounded 100 ms drain interval reduces scheduler-sensitive repeated system calls. The steady_clock watchdog is correctly below the registered 30 s CTest timeout; on Windows abort exits without flushing stream buffers, so it can terminate even if the test is stuck behind the stream lock.

git diff --check upstream/main...HEAD and python3 tools/check_conventions.py --base upstream/main --head HEAD pass on the exact PR head. The body reports useful repeated Windows affinity/load runs, including a no-fix control that fails via the watchdog; I have not independently reproduced those Windows runs. Hosted eligibility, conventions, intake, and labels pass. Hosted product test jobs were skipped, so there is no hosted runtime result to claim. I found no blocking issue in the test change.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638439862 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10630#pullrequestreview-5638439862). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T13:38:28Z. **Review:** APPROVED --- Reviewed exact head `a604ce5effe96b2179ceb2e1d94000f4c73c2ea7`. The single-file test change keeps the handler blocked while the main thread calls `fputc`, while it repeatedly creates a sustained `fwrite` system-call window for each raise. The explicit `writing`/`deliveries` handshake prevents a raise from being sent before the corresponding write starts, and the reader's bounded 100 ms drain interval reduces scheduler-sensitive repeated system calls. The `steady_clock` watchdog is correctly below the registered 30 s CTest timeout; on Windows `abort` exits without flushing stream buffers, so it can terminate even if the test is stuck behind the stream lock. `git diff --check upstream/main...HEAD` and `python3 tools/check_conventions.py --base upstream/main --head HEAD` pass on the exact PR head. The body reports useful repeated Windows affinity/load runs, including a no-fix control that fails via the watchdog; I have not independently reproduced those Windows runs. Hosted eligibility, conventions, intake, and labels pass. Hosted product test jobs were skipped, so there is no hosted runtime result to claim. I found no blocking issue in the test change.
MarcZX10 commented 2026-10-24 14:20:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T14:20:04Z.

Review: CHANGES_REQUESTED


I rechecked the changed synchronization in GuestRaiseInSystemCall.cpp against the test’s stated purpose. The writer stores writing = raise immediately before fwrite, and the main thread raises as soon as it observes that value. This only proves the writer reached the line before fwrite; it does not prove the thread is inside the blocking NtWriteFile call. Under scheduling delay the exception can be delivered before the call, so the new test can pass without exercising the system-call path it is meant to guard. Please arrange the pipe/reader coordination so the writer is observably blocked in the write before raising (and retain the watchdog), or otherwise add a deterministic assertion that the target is in the system call at delivery. The reported stress runs are useful, but this ordering gap limits what they prove.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638573004 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10630#pullrequestreview-5638573004). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T14:20:04Z. **Review:** CHANGES_REQUESTED --- I rechecked the changed synchronization in `GuestRaiseInSystemCall.cpp` against the test’s stated purpose. The writer stores `writing = raise` immediately before `fwrite`, and the main thread raises as soon as it observes that value. This only proves the writer reached the line before `fwrite`; it does not prove the thread is inside the blocking `NtWriteFile` call. Under scheduling delay the exception can be delivered before the call, so the new test can pass without exercising the system-call path it is meant to guard. Please arrange the pipe/reader coordination so the writer is observably blocked in the write before raising (and retain the watchdog), or otherwise add a deterministic assertion that the target is in the system call at delivery. The reported stress runs are useful, but this ordering gap limits what they prove.
MarcZX10 commented 2026-10-24 14:34:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T14:34:09Z.

Review: APPROVED


Re-reviewed exact head 7fd6a7d7d3466deae7ed175d627e48d8f22af6df after the author’s follow-up. The test now uses unbuffered 16 KiB writes into a 4 KiB pipe; the reader observes bytes from the active block, stops draining, and the main thread raises only after that handshake. The 100 ms hold checks that the write has not returned before the exception is delivered, and the handler verifies the corresponding write has returned. This closes the prior ordering gap in the test’s claim. git diff --check upstream/main...HEAD and repository conventions pass on the updated head. Hosted conventions/eligibility/labels pass; hosted product jobs remain skipped. The author reports repeated Windows runs and a no-fix control, which I have not independently reproduced.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638611350 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10630#pullrequestreview-5638611350). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T14:34:09Z. **Review:** APPROVED --- Re-reviewed exact head `7fd6a7d7d3466deae7ed175d627e48d8f22af6df` after the author’s follow-up. The test now uses unbuffered 16 KiB writes into a 4 KiB pipe; the reader observes bytes from the active block, stops draining, and the main thread raises only after that handshake. The 100 ms hold checks that the write has not returned before the exception is delivered, and the handler verifies the corresponding write has returned. This closes the prior ordering gap in the test’s claim. `git diff --check upstream/main...HEAD` and repository conventions pass on the updated head. Hosted conventions/eligibility/labels pass; hosted product jobs remain skipped. The author reports repeated Windows runs and a no-fix control, which I have not independently reproduced.
THEMANnt commented 2026-10-25 23:11:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by THEMANnt on 2026-10-25T23:11:54Z.

Review: COMMENTED


Re-reviewed 86709434faae43bb020effbdcd509ce5aec33ee5. Final approval remains pending.

Reviewed the revised blocked-write handshake and the new --delay-raise/--delay-return CTest modes. The test now captures the writer handle, observes its suspension, checks the signal context's instruction pointer against the test image, and coordinates draining, delivery and release across ten iterations. These are timing/runtime-sensitive assertions that need actual target execution.

Validation: The exact current test passes GCC 15.2 C++23 syntax compilation. Retrying the established optimized compatibility build still fails inside GCC's choose_baseaddr backend pass, before an executable is produced. The prior baseline build had the same compiler failure; this does not establish an author-introduced compile defect. No current runtime pass is claimed.

Pending: Run the complete linked test and both delay modes with a working compiler and matching runtime. Approval remains pending that execution; no verified code defect justifies a request for changes here.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5645824168 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10630#pullrequestreview-5645824168). Originally posted by [THEMANnt](https://github.com/THEMANnt) on 2026-10-25T23:11:54Z. **Review:** COMMENTED --- Re-reviewed `86709434faae43bb020effbdcd509ce5aec33ee5`. Final approval remains pending. Reviewed the revised blocked-write handshake and the new --delay-raise/--delay-return CTest modes. The test now captures the writer handle, observes its suspension, checks the signal context's instruction pointer against the test image, and coordinates draining, delivery and release across ten iterations. These are timing/runtime-sensitive assertions that need actual target execution. **Validation:** The exact current test passes GCC 15.2 C++23 syntax compilation. Retrying the established optimized compatibility build still fails inside GCC's choose_baseaddr backend pass, before an executable is produced. The prior baseline build had the same compiler failure; this does not establish an author-introduced compile defect. No current runtime pass is claimed. **Pending:** Run the complete linked test and both delay modes with a working compiler and matching runtime. Approval remains pending that execution; no verified code defect justifies a request for changes here.
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!10630
No description provided.