test(libkernel): fix host-mutex handshake race in guest_raise_exception #1375

Merged
gjonn merged 1 commit from upstream-pr/1375 into main 2026-10-08 02:19:01 +00:00
gjonn commented 2026-10-08 00:51:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gjonn on 2026-10-08T00:51:07Z.


What

This fixes the flaky Windows ctest guest_raise_exception. The test sometimes hangs until its 30 s timeout, and the log stops after three RunThread entry=... lines. It has failed CI on several unrelated branches: #1350, #1354, codex/luna-posix-file-errors, feat/videoout-dcc-visible-keys and perf/shader-bda-span-reads (e.g. job 113079295544).

The race is in the test's host-blocked phase. The sceKernelRaiseException delivery path from #760 is not affected.

  1. Main unlocks hostLock. The worker wakes up inside pthread_mutex_lock, and the redirected handler runs before the worker owns the mutex.
  2. ExpectDelivery sees the delivery and returns. Main immediately calls hostLock.lock() again.
  3. If main gets the mutex first, the worker stays blocked in the previous round's lock_guard. It never sets started for the next round, so main spins forever. A spin is not a failed Require, so the test never aborts and runs into the ctest timeout instead. The third phase never finishes, which is why the log ends after three RunThread lines.

The fix: the worker records the round number (hostAcquired) once it has the mutex, and main waits for it before locking again. The timeout is not changed.

Tested

Windows 11, MinGW gcc (posix thread model), Ninja, Release, BUILD_TESTING=ON.

  • ctest -R guest_raise_exception --repeat until-fail:50 passes all 50 runs. The unpatched binary also passed this on an idle machine, so the plain repeat does not hit the race.
  • Stress test with 24 copies of the test running at once on 24 hardware threads, each with a 30 s timeout:
Build Runs Passed Hung (30 s timeout)
Before 144 132 12
After 288 288 0
  • One hung run of the unpatched binary printed exactly three RunThread lines, which matches the CI log.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes. The investigation, fix and stress testing were done with Claude Code and reviewed by me.

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4780284797 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1375). Originally posted by [gjonn](https://github.com/gjonn) on 2026-10-08T00:51:07Z. --- ### What This fixes the flaky Windows ctest `guest_raise_exception`. The test sometimes hangs until its 30 s timeout, and the log stops after three `RunThread entry=...` lines. It has failed CI on several unrelated branches: #1350, #1354, `codex/luna-posix-file-errors`, `feat/videoout-dcc-visible-keys` and `perf/shader-bda-span-reads` (e.g. job 113079295544). The race is in the test's host-blocked phase. The `sceKernelRaiseException` delivery path from #760 is not affected. 1. Main unlocks `hostLock`. The worker wakes up inside `pthread_mutex_lock`, and the redirected handler runs before the worker owns the mutex. 2. `ExpectDelivery` sees the delivery and returns. Main immediately calls `hostLock.lock()` again. 3. If main gets the mutex first, the worker stays blocked in the previous round's `lock_guard`. It never sets `started` for the next round, so main spins forever. A spin is not a failed `Require`, so the test never aborts and runs into the ctest timeout instead. The third phase never finishes, which is why the log ends after three `RunThread` lines. The fix: the worker records the round number (`hostAcquired`) once it has the mutex, and main waits for it before locking again. The timeout is not changed. ### Tested Windows 11, MinGW gcc (posix thread model), Ninja, Release, `BUILD_TESTING=ON`. - `ctest -R guest_raise_exception --repeat until-fail:50` passes all 50 runs. The unpatched binary also passed this on an idle machine, so the plain repeat does not hit the race. - Stress test with 24 copies of the test running at once on 24 hardware threads, each with a 30 s timeout: | Build | Runs | Passed | Hung (30 s timeout) | |---|---|---|---| | Before | 144 | 132 | 12 | | After | 288 | 288 | 0 | - One hung run of the unpatched binary printed exactly three `RunThread` lines, which matches the CI log. ### Checklist - [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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes. The investigation, fix and stress testing were done with Claude Code and reviewed by me. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
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!1375
No description provided.