fix(libkernel): treat a null deadline as an untimed lock in the timed pthread locks #7035

Merged
milescoviello merged 2 commits from upstream-pr/7035 into main 2026-10-17 15:48:21 +00:00
milescoviello commented 2026-10-17 06:46:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-17T06:46:33Z.


What

pthread_rwlock_timedrdlock, pthread_rwlock_timedwrlock and pthread_mutex_timedlock threw std::runtime_error (null abstime) for a null deadline. In FreeBSD 11's libthr, a null abstime reaches the untimed kernel lock:

The three calls now do what pthread_rwlock_rdlock, pthread_rwlock_wrlock and pthread_mutex_lock do when abstime is null.

I don't know of a title that hits this. I found it while comparing the pthread wrappers with libthr.

Tested

Ubuntu 24.04, GCC 13.3, Release, on main 6a32550fd1.

  • guest_pthread_timed_locks (new): with a null deadline, both rwlock calls take a free lock, and each waits for a writer that holds the lock for 50 ms, then takes it. pthread_mutex_timedlock takes a free mutex, returns EDEADLK when the caller already holds it, and waits for another holder the same way. With main's Rwlock.cpp, and separately with main's Mutex.cpp, the test aborts at the first null deadline in that file (the throw). With this change it passes 3 of 3.
  • guest_pthread_rwlock: its check that a null deadline throws now expects the lock to be taken and released. With main's Rwlock.cpp that check fails.
  • ctest -R '^guest_|libc': 403 of 403 pass, with this and one other small libkernel fix applied together.
  • c7849f8495, after a review: the waiter now requires a flag that the holder sets just before it unlocks, in place of the old check that it waited at least half the hold time. That check aborted in 15 of 20 runs on a heavily loaded Windows host, because the waiter saw the holder's lock 40–90 ms late (run by @screamm). Here, under 24 CPU burners, the new test passes 20 of 20; the old check also passed 20 of 20, so I could not reproduce the abort on Linux. Both lock tests pass 3 of 3 with ctest.
  • Windows 11 Pro, Ryzen 7 5700X, WinLibs GCC 15.2.0 r7, Release, at 676ee71c3: both tests pass 3 of 3, and with this PR's source reverted both throw null abstime (series run by @screamm).
  • tools/check_conventions.py passes. tools/pr_overlap.py finds no conflicting lines, and the new test is registered away from where #6711 adds one (git merge-tree merges this branch with #6711, #4563, #6893 and #6244 cleanly). It couldn't compare against seven PRs that conflict with main and touch CMakeLists.txt (#6044 also moves GuestPthreadRwlock.cpp).

@screamm rechecked c7849f849 on Windows: guest_pthread_timed_locks passes 20 of 20 direct runs on a loaded host (where it had aborted 15 of 20) and 20 of 20 next to an 8-job build; both tests abort with main's Mutex.cpp and Rwlock.cpp.

Not checked: macOS, a console.

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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4898027864 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7035). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-17T06:46:33Z. --- ### What `pthread_rwlock_timedrdlock`, `pthread_rwlock_timedwrlock` and `pthread_mutex_timedlock` threw `std::runtime_error` (`null abstime`) for a null deadline. In FreeBSD 11's libthr, a null `abstime` reaches the untimed kernel lock: - [`rwlock_rdlock_common`](https://github.com/freebsd/freebsd-src/blob/releng/11.0/lib/libthr/thread/thr_rwlock.c#L144) and [`rwlock_wrlock_common`](https://github.com/freebsd/freebsd-src/blob/releng/11.0/lib/libthr/thread/thr_rwlock.c#L265) check the deadline only when it isn't null and pass it to `__thr_rwlock_rdlock`/`__thr_rwlock_wrlock`, where null means no timeout. - [`mutex_lock_sleep`](https://github.com/freebsd/freebsd-src/blob/releng/11.0/lib/libthr/thread/thr_mutex.c#L641) calls [`__thr_umutex_lock`](https://github.com/freebsd/freebsd-src/blob/releng/11.0/lib/libthr/thread/thr_mutex.c#L691-L698) when `abstime == NULL`, and `mutex_self_lock` returns `EDEADLK` for an error-checking mutex the caller already holds, as `pthread_mutex_lock` does. The three calls now do what `pthread_rwlock_rdlock`, `pthread_rwlock_wrlock` and `pthread_mutex_lock` do when `abstime` is null. I don't know of a title that hits this. I found it while comparing the pthread wrappers with libthr. ### Tested Ubuntu 24.04, GCC 13.3, Release, on `main` 6a32550fd1. - `guest_pthread_timed_locks` (new): with a null deadline, both rwlock calls take a free lock, and each waits for a writer that holds the lock for 50 ms, then takes it. `pthread_mutex_timedlock` takes a free mutex, returns `EDEADLK` when the caller already holds it, and waits for another holder the same way. With `main`'s `Rwlock.cpp`, and separately with `main`'s `Mutex.cpp`, the test aborts at the first null deadline in that file (the throw). With this change it passes 3 of 3. - `guest_pthread_rwlock`: its check that a null deadline throws now expects the lock to be taken and released. With `main`'s `Rwlock.cpp` that check fails. - `ctest -R '^guest_|libc'`: 403 of 403 pass, with this and one other small libkernel fix applied together. - **c7849f8495, after a review:** the waiter now requires a flag that the holder sets just before it unlocks, in place of the old check that it waited at least half the hold time. That check aborted in 15 of 20 runs on a heavily loaded Windows host, because the waiter saw the holder's lock 40–90 ms late ([run by @screamm](https://github.com/boykopovar/AnyPS5/pull/7023#issuecomment-6232617712)). Here, under 24 CPU burners, the new test passes 20 of 20; the old check also passed 20 of 20, so I could not reproduce the abort on Linux. Both lock tests pass 3 of 3 with ctest. - Windows 11 Pro, Ryzen 7 5700X, WinLibs GCC 15.2.0 r7, Release, at 676ee71c3: both tests pass 3 of 3, and with this PR's source reverted both throw `null abstime` ([series run by @screamm](https://github.com/boykopovar/AnyPS5/pull/7023#issuecomment-6232617712)). - `tools/check_conventions.py` passes. `tools/pr_overlap.py` finds no conflicting lines, and the new test is registered away from where #6711 adds one (`git merge-tree` merges this branch with #6711, #4563, #6893 and #6244 cleanly). It couldn't compare against seven PRs that conflict with `main` and touch `CMakeLists.txt` (#6044 also moves `GuestPthreadRwlock.cpp`). @screamm [rechecked c7849f849 on Windows](https://github.com/boykopovar/AnyPS5/pull/7035#issuecomment-6233396739): `guest_pthread_timed_locks` passes 20 of 20 direct runs on a loaded host (where it had aborted 15 of 20) and 20 of 20 next to an 8-job build; both tests abort with `main`'s `Mutex.cpp` and `Rwlock.cpp`. Not checked: macOS, a console. ### 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 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] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-17 06:56:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-17T06:56:48Z.


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.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 6241 core/libs/prx/libkernel/CMakeLists.txt L671

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 4972 core/libs/prx/libkernel/CMakeLists.txt
PR 5021 core/libs/prx/libkernel/CMakeLists.txt
PR 6044 core/libs/prx/libkernel/CMakeLists.txt
core/libs/tests/GuestPthreadRwlock.cpp
PR 6222 core/libs/prx/libkernel/CMakeLists.txt
PR 6287 core/libs/prx/libkernel/CMakeLists.txt
PR 6487 core/libs/prx/libkernel/CMakeLists.txt
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6230934803 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7035#issuecomment-6230934803). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-17T06:56:48Z. --- <!-- pr-overlap --> 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**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | [PR 6241](https://www.github.com/boykopovar/AnyPS5/pull/6241) | `core/libs/prx/libkernel/CMakeLists.txt` [L671](https://github.com/boykopovar/AnyPS5/blob/82f0c9aec9fc963a61d13c62084ce08f3cd093ab/core/libs/prx/libkernel/CMakeLists.txt#L671) | | | Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | [PR 4972](https://www.github.com/boykopovar/AnyPS5/pull/4972) | `core/libs/prx/libkernel/CMakeLists.txt` | | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/prx/libkernel/CMakeLists.txt` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/prx/libkernel/CMakeLists.txt`<br>`core/libs/tests/GuestPthreadRwlock.cpp` | | [PR 6222](https://www.github.com/boykopovar/AnyPS5/pull/6222) | `core/libs/prx/libkernel/CMakeLists.txt` | | [PR 6287](https://www.github.com/boykopovar/AnyPS5/pull/6287) | `core/libs/prx/libkernel/CMakeLists.txt` | | [PR 6487](https://www.github.com/boykopovar/AnyPS5/pull/6487) | `core/libs/prx/libkernel/CMakeLists.txt` |
screamm commented 2026-10-17 10:05:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-17T10:05:27Z.


Windows run in https://github.com/boykopovar/AnyPS5/pull/7023#issuecomment-6232617712: both tests pass 3/3, and with the source reverted both throw "null abstime". guest_pthread_timed_locks aborted in 15 of 20 runs on a heavily loaded host and passed 20 of 20 on a quieter one. Every abort was the elapsed >= HoldTime / 2 check, after the main thread saw held 40-90 ms late; the lock was always taken after the holder unlocked. A flag the holder sets just before unlocking, checked when acquire() returns, would make the test deterministic.

Written with Claude Code assistance (validation by a subagent, reviewed in this session).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6232620883 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7035#issuecomment-6232620883). Originally posted by [screamm](https://github.com/screamm) on 2026-10-17T10:05:27Z. --- Windows run in https://github.com/boykopovar/AnyPS5/pull/7023#issuecomment-6232617712: both tests pass 3/3, and with the source reverted both throw "null abstime". guest_pthread_timed_locks aborted in 15 of 20 runs on a heavily loaded host and passed 20 of 20 on a quieter one. Every abort was the elapsed >= HoldTime / 2 check, after the main thread saw held 40-90 ms late; the lock was always taken after the holder unlocked. A flag the holder sets just before unlocking, checked when acquire() returns, would make the test deterministic. Written with Claude Code assistance (validation by a subagent, reviewed in this session).
milescoviello commented 2026-10-17 10:09:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-17T10:09:52Z.


Thanks @screamm, that diagnosis was right. The lock was taken only after the holder unlocked, so the half-hold-time check was measuring scheduler delay. c7849f8495 does what you suggested: the holder sets releasing just before it unlocks, and the waiter requires that flag once acquire() returns. Here, under 24 CPU burners, the new test passes 20 of 20, and both lock tests pass 3 of 3 with ctest. (The old check also passed 20 of 20 under that load on Linux, so I couldn't reproduce the abort here.) I added your Windows run to the description.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6232661960 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7035#issuecomment-6232661960). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-17T10:09:52Z. --- Thanks @screamm, that diagnosis was right. The lock was taken only after the holder unlocked, so the half-hold-time check was measuring scheduler delay. c7849f8495 does what you suggested: the holder sets `releasing` just before it unlocks, and the waiter requires that flag once `acquire()` returns. Here, under 24 CPU burners, the new test passes 20 of 20, and both lock tests pass 3 of 3 with ctest. (The old check also passed 20 of 20 under that load on Linux, so I couldn't reproduce the abort here.) I added your Windows run to the description. AI-assisted: yes (Claude Code)
screamm commented 2026-10-17 11:33:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-17T11:33:33Z.


Rechecked on head c7849f849 (scratch-merged onto main 673f7406c, merge is clean). Windows 11 Pro 26220, Ryzen 7 5700X, WinLibs GCC 15.2. Targeted build only (libs, guest_pthread_timed_locks_tests, guest_pthread_rwlock_tests), not a full build.

  • guest_pthread_timed_locks: 20 of 20 direct runs pass on a loaded host (other builds were running; this is the condition under which it aborted 15 of 20 before), and 20 of 20 again while I ran my own 8-job build next to them. Each run takes 0.5 to 3 s including process start. ctest --repeat until-fail:3: 3 of 3.
  • guest_pthread_rwlock: 3 of 3 with ctest --repeat until-fail:3 and 3 of 3 direct.
  • Negative control: with Mutex.cpp and Rwlock.cpp reverted to main (tests kept), both tests abort with what(): pthread_rwlock_timedrdlock: null abstime; with the files restored both pass.
  • tools/check_conventions.py --base origin/main passes and git diff --check is clean.

The releasing flag is set before the unlock and the lock can only be taken after it, so the check no longer depends on when the main thread is scheduled. That resolves the flake I reported.

Not checked: full build, the non-GPU suite, Linux, title runs.

Written with Claude Code assistance (validation by a subagent, reviewed in this session).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6233396739 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7035#issuecomment-6233396739). Originally posted by [screamm](https://github.com/screamm) on 2026-10-17T11:33:33Z. --- Rechecked on head `c7849f849` (scratch-merged onto main `673f7406c`, merge is clean). Windows 11 Pro 26220, Ryzen 7 5700X, WinLibs GCC 15.2. Targeted build only (`libs`, `guest_pthread_timed_locks_tests`, `guest_pthread_rwlock_tests`), not a full build. - `guest_pthread_timed_locks`: 20 of 20 direct runs pass on a loaded host (other builds were running; this is the condition under which it aborted 15 of 20 before), and 20 of 20 again while I ran my own 8-job build next to them. Each run takes 0.5 to 3 s including process start. ctest `--repeat until-fail:3`: 3 of 3. - `guest_pthread_rwlock`: 3 of 3 with ctest `--repeat until-fail:3` and 3 of 3 direct. - Negative control: with `Mutex.cpp` and `Rwlock.cpp` reverted to main (tests kept), both tests abort with `what(): pthread_rwlock_timedrdlock: null abstime`; with the files restored both pass. - `tools/check_conventions.py --base origin/main` passes and `git diff --check` is clean. The `releasing` flag is set before the unlock and the lock can only be taken after it, so the check no longer depends on when the main thread is scheduled. That resolves the flake I reported. Not checked: full build, the non-GPU suite, Linux, title runs. Written with Claude Code assistance (validation by a subagent, reviewed in this session).
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!7035
No description provided.