fix(libkernel): time out timed locks of a lock the caller already holds #9165

Merged
MaxLikesCode merged 1 commit from upstream-pr/9165 into main 2026-10-25 01:01:46 +00:00
MaxLikesCode commented 2026-10-21 14:08:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MaxLikesCode on 2026-10-21T14:08:00Z.


What

A timed lock of a lock the caller already holds now waits out its timeout and returns ETIMEDOUT, instead of returning EDEADLK at once:

  • scePthreadMutexTimedlock on a non-recursive mutex the caller holds.
  • scePthreadRwlockTimedrdlock and scePthreadRwlockTimedwrlock on a rwlock the caller holds for writing.

The untimed scePthreadMutexLock, scePthreadRwlockRdlock and scePthreadRwlockWrlock still return EDEADLK.

Where the behaviour comes from:

  • Console measurement. The prosper console oracle measured ETIMEDOUT (0x8002003c) after a 1000 us timeout for a default mutex and for both rwlock calls (mutex, rwlock). Only the result was measured, not whether the console waited.
  • FreeBSD 11.4 libthr. mutex_self_lock sleeps until the deadline and returns ETIMEDOUT for error-checking, adaptive and normal mutexes (thr_mutex.c L878-L928). The rwlock locks have no deadlock check and wait in the kernel (thr_rwlock.c L159-L329). So the calls here sleep until the timeout too.

The POSIX pthread_mutex_timedlock, pthread_mutex_reltimedlock_np, pthread_rwlock_timedrdlock and pthread_rwlock_timedwrlock call these functions and change with them. So do pthread_rwlock_reltimedrdlock_np and pthread_rwlock_reltimedwrlock_np, which #9265 added to main after this branch's base; they forward to scePthreadRwlockTimedrdlock and scePthreadRwlockTimedwrlock. Two tests expected EDEADLK from them, which no measurement or FreeBSD source supports:

  • guest_pthread_rwlock, from the POSIX timed rwlock locks. It now expects ETIMEDOUT, and its 5 s deadline is now 20 ms, since the call really waits.
  • guest_pthread_mutex_errors, from pthread_mutex_reltimedlock_np on a mutex the caller holds (added in #8400, which describes that function as scePthreadMutexTimedlock with POSIX error numbers). It now expects ETIMEDOUT after its 1000 us timeout.

The adaptive and normal mutex types and the POSIX names were not measured; docs/dev/debt/libkernel.md says so.

Tested

  • Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, full Release build with BUILD_TESTING=ON, on main 1b5d7021a.
  • guest_kernel_errors passes with new cases: a timed lock of an error-checking, adaptive, default and normal mutex the caller holds, and both timed rwlock locks while the caller holds the write lock. Each returns ETIMEDOUT after at least 15 ms of a 20 ms timeout, and the lock is taken normally once released. It fails without the changes to Mutex.cpp and Rwlock.cpp. guest_kernel_errors, guest_pthread_rwlock, guest_pthread_mutex_errors and guest_pthread_timed_locks pass 3 of 3.
  • python tools/select_tests.py ctest build --output-on-failure --timeout 120 --parallel 4: on main 1b5d7021a all 739 selected tests outside agc_ pass. A run of all 1517 selected tests one rebase earlier, on main 47fa6eecf, passed 1514; the 3 failures were agc_driver_sample_order, agc_driver_sample_order_grid_reference and agc_driver_sample_order_recipe_depth_locations from #6683, which fail with the same messages on that main on this RTX 4090. agc_driver_image_array_window_past_layer_limit is not built: it does not compile with MinGW on main (#8310).
  • python tools/check_conventions.py --base origin/main passes. python tools/pr_overlap.py --branch fix/pthread-timed-self-lock reports no open PR changing the same lines or exports.
  • Linux: @xXPBSXx ran it on WSL2 Ubuntu 24.04 (GCC 13.3.0, Release) at 3959305a27: guest_pthread_mutex_errors, guest_pthread_rwlock and guest_kernel_errors pass 3 of 3, and all three fail with Mutex.cpp and Rwlock.cpp restored to the merge base.
  • Rebased onto main 38d463a6cf after #10706 moved the test declarations into the public headers; GuestKernelErrors.cpp now includes Pthread/include/Rwlock.hpp for the rwlock calls. At 55ff5484d6, guest_kernel_errors, guest_pthread_mutex_errors and guest_pthread_rwlock pass 3 of 3, and all three fail 3 of 3 with Mutex.cpp and Rwlock.cpp from main. public_headers and the conventions check pass.
  • Not tested: a console, a title or homebrew.

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
  • GPUs tested: n/a
  • AI-assisted: yes. Written with Claude Code (Claude Opus 5.5).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4952950789 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9165). Originally posted by [MaxLikesCode](https://github.com/MaxLikesCode) on 2026-10-21T14:08:00Z. --- ### What A timed lock of a lock the caller already holds now waits out its timeout and returns `ETIMEDOUT`, instead of returning `EDEADLK` at once: - `scePthreadMutexTimedlock` on a non-recursive mutex the caller holds. - `scePthreadRwlockTimedrdlock` and `scePthreadRwlockTimedwrlock` on a rwlock the caller holds for writing. The untimed `scePthreadMutexLock`, `scePthreadRwlockRdlock` and `scePthreadRwlockWrlock` still return `EDEADLK`. Where the behaviour comes from: - **Console measurement.** The prosper console oracle measured `ETIMEDOUT` (`0x8002003c`) after a 1000 us timeout for a default mutex and for both rwlock calls ([mutex](https://github.com/mattias800/prosper/blob/3ea5cb9a846b6dcd00f5073360462bbda4f72288/prosper/tests/data/console_oracle/kernelx.golden.tsv#L16), [rwlock](https://github.com/mattias800/prosper/blob/3ea5cb9a846b6dcd00f5073360462bbda4f72288/prosper/tests/data/console_oracle/kernelx.golden.tsv#L46-L47)). Only the result was measured, not whether the console waited. - **FreeBSD 11.4 libthr.** `mutex_self_lock` sleeps until the deadline and returns `ETIMEDOUT` for error-checking, adaptive and normal mutexes ([thr_mutex.c L878-L928](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/lib/libthr/thread/thr_mutex.c#L878-L928)). The rwlock locks have no deadlock check and wait in the kernel ([thr_rwlock.c L159-L329](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/lib/libthr/thread/thr_rwlock.c#L159-L329)). So the calls here sleep until the timeout too. The POSIX `pthread_mutex_timedlock`, `pthread_mutex_reltimedlock_np`, `pthread_rwlock_timedrdlock` and `pthread_rwlock_timedwrlock` call these functions and change with them. So do `pthread_rwlock_reltimedrdlock_np` and `pthread_rwlock_reltimedwrlock_np`, which #9265 added to `main` after this branch's base; they forward to `scePthreadRwlockTimedrdlock` and `scePthreadRwlockTimedwrlock`. Two tests expected `EDEADLK` from them, which no measurement or FreeBSD source supports: - `guest_pthread_rwlock`, from the POSIX timed rwlock locks. It now expects `ETIMEDOUT`, and its 5 s deadline is now 20 ms, since the call really waits. - `guest_pthread_mutex_errors`, from `pthread_mutex_reltimedlock_np` on a mutex the caller holds (added in #8400, which describes that function as `scePthreadMutexTimedlock` with POSIX error numbers). It now expects `ETIMEDOUT` after its 1000 us timeout. The adaptive and normal mutex types and the POSIX names were not measured; `docs/dev/debt/libkernel.md` says so. ### Tested - Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, full Release build with `BUILD_TESTING=ON`, on `main` 1b5d7021a. - `guest_kernel_errors` passes with new cases: a timed lock of an error-checking, adaptive, default and normal mutex the caller holds, and both timed rwlock locks while the caller holds the write lock. Each returns `ETIMEDOUT` after at least 15 ms of a 20 ms timeout, and the lock is taken normally once released. It fails without the changes to `Mutex.cpp` and `Rwlock.cpp`. `guest_kernel_errors`, `guest_pthread_rwlock`, `guest_pthread_mutex_errors` and `guest_pthread_timed_locks` pass 3 of 3. - `python tools/select_tests.py ctest build --output-on-failure --timeout 120 --parallel 4`: on `main` 1b5d7021a all 739 selected tests outside `agc_` pass. A run of all 1517 selected tests one rebase earlier, on `main` 47fa6eecf, passed 1514; the 3 failures were `agc_driver_sample_order`, `agc_driver_sample_order_grid_reference` and `agc_driver_sample_order_recipe_depth_locations` from #6683, which fail with the same messages on that `main` on this RTX 4090. `agc_driver_image_array_window_past_layer_limit` is not built: it does not compile with MinGW on `main` (#8310). - `python tools/check_conventions.py --base origin/main` passes. `python tools/pr_overlap.py --branch fix/pthread-timed-self-lock` reports no open PR changing the same lines or exports. - Linux: @xXPBSXx [ran it](https://github.com/boykopovar/AnyPS5/pull/9165#issuecomment-6321375016) on WSL2 Ubuntu 24.04 (GCC 13.3.0, Release) at `3959305a27`: `guest_pthread_mutex_errors`, `guest_pthread_rwlock` and `guest_kernel_errors` pass 3 of 3, and all three fail with `Mutex.cpp` and `Rwlock.cpp` restored to the merge base. - Rebased onto `main` 38d463a6cf after #10706 moved the test declarations into the public headers; `GuestKernelErrors.cpp` now includes `Pthread/include/Rwlock.hpp` for the rwlock calls. At 55ff5484d6, `guest_kernel_errors`, `guest_pthread_mutex_errors` and `guest_pthread_rwlock` pass 3 of 3, and all three fail 3 of 3 with `Mutex.cpp` and `Rwlock.cpp` from `main`. `public_headers` and the conventions check pass. - Not tested: a console, a title or homebrew. ### 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 - [x] GPUs tested: n/a - [x] AI-assisted: yes. Written with Claude Code (Claude Opus 5.5). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
xXPBSXx commented 2026-10-21 16:44:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T16:44:22Z.


Linux data point for 3959305a27: WSL2 Ubuntu 24.04 (virtualised kernel, not bare-metal Linux), GCC 13.3.0, Release, BUILD_TESTING=ON, PR head alone (not merged onto main; its merge-base is 1b5d7021a).

  • With the PR: ctest -R '^(guest_pthread_mutex_errors|guest_pthread_rwlock|guest_kernel_errors)$' passes 3 of 3 (all three tests).
  • With libkernel/Pthread/src/Mutex.cpp and Rwlock.cpp restored to the merge-base (the PR's tests kept): fails: guest_pthread_mutex_errors and guest_pthread_rwlock abort, guest_kernel_errors fails too.
    Not tested: macOS, Windows, a title run.
    AI-assisted (Claude Code).
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6321375016 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9165#issuecomment-6321375016). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-21T16:44:22Z. --- Linux data point for `3959305a27`: WSL2 Ubuntu 24.04 (virtualised kernel, not bare-metal Linux), GCC 13.3.0, Release, `BUILD_TESTING=ON`, PR head alone (not merged onto `main`; its merge-base is `1b5d7021a`). - With the PR: `ctest -R '^(guest_pthread_mutex_errors|guest_pthread_rwlock|guest_kernel_errors)$'` passes 3 of 3 (all three tests). - With `libkernel/Pthread/src/Mutex.cpp and Rwlock.cpp` restored to the merge-base (the PR's tests kept): fails: `guest_pthread_mutex_errors` and `guest_pthread_rwlock` abort, `guest_kernel_errors` fails too. Not tested: macOS, Windows, a title run. AI-assisted (Claude Code).
MaxLikesCode commented 2026-10-21 16:54:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MaxLikesCode on 2026-10-21T16:54:19Z.


Thanks @xXPBSXx for the Linux run and the check with Mutex.cpp and Rwlock.cpp restored. I added it to the Tested section.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6321591771 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9165#issuecomment-6321591771). Originally posted by [MaxLikesCode](https://github.com/MaxLikesCode) on 2026-10-21T16:54:19Z. --- Thanks @xXPBSXx for the Linux run and the check with `Mutex.cpp` and `Rwlock.cpp` restored. I added it to the Tested section. AI-assisted: yes (Claude Code)
harisibrar1 commented 2026-10-24 12:56:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T12:56:23Z.

Review: APPROVED


Checked against main 99feaf0eb8 and FreeBSD 11.4 lib/libthr/thread/thr_mutex.c / thr_rwlock.c (8c639945):

  • It still merges cleanly onto main, and main still returns EDEADLK here (Mutex.cpp:63-67 via scePthreadMutexTimedlock, Rwlock.cpp:105/134), so this isn't already done.
  • The FreeBSD reference matches: mutex_lock_sleep → mutex_self_lock (thr_mutex.c:651, 879-928) sleeps to the deadline and returns ETIMEDOUT for error-checking, adaptive and normal mutexes when an abstime is given, and thr_rwlock.c has no deadlock check before the umtx wait.
  • FYI: pthread_rwlock_reltimedrdlock_np / pthread_rwlock_reltimedwrlock_np landed on main after your base (8f13fe70b9). They forward to the sce timed rwlock calls, so they now also return ETIMEDOUT on a self-held write lock. Your debt sentence about the POSIX timed locks covers them, and main's GuestPthreadRwlock.cpp has no self-held reltimed case that would break; you might mention them in the description.
  • Null-abstime POSIX calls still go to the untimed lock, so GuestPthreadTimedLocks.cpp:75 (EDEADLK) is unaffected.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638318519 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9165#pullrequestreview-5638318519). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T12:56:23Z. **Review:** APPROVED --- Checked against main 99feaf0eb8 and FreeBSD 11.4 lib/libthr/thread/thr_mutex.c / thr_rwlock.c (8c639945): - It still merges cleanly onto main, and main still returns EDEADLK here (Mutex.cpp:63-67 via `scePthreadMutexTimedlock`, Rwlock.cpp:105/134), so this isn't already done. - The FreeBSD reference matches: `mutex_lock_sleep` → `mutex_self_lock` (thr_mutex.c:651, 879-928) sleeps to the deadline and returns ETIMEDOUT for error-checking, adaptive and normal mutexes when an abstime is given, and thr_rwlock.c has no deadlock check before the umtx wait. - FYI: `pthread_rwlock_reltimedrdlock_np` / `pthread_rwlock_reltimedwrlock_np` landed on main after your base (8f13fe70b9). They forward to the sce timed rwlock calls, so they now also return ETIMEDOUT on a self-held write lock. Your debt sentence about the POSIX timed locks covers them, and main's GuestPthreadRwlock.cpp has no self-held reltimed case that would break; you might mention them in the description. - Null-abstime POSIX calls still go to the untimed lock, so `GuestPthreadTimedLocks.cpp:75` (EDEADLK) is unaffected. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
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!9165
No description provided.