fix(libkernel): time out timed locks of a lock the caller already holds #9165
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9165"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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 returningEDEADLKat once:scePthreadMutexTimedlockon a non-recursive mutex the caller holds.scePthreadRwlockTimedrdlockandscePthreadRwlockTimedwrlockon a rwlock the caller holds for writing.The untimed
scePthreadMutexLock,scePthreadRwlockRdlockandscePthreadRwlockWrlockstill returnEDEADLK.Where the behaviour comes from:
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.mutex_self_locksleeps until the deadline and returnsETIMEDOUTfor 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_timedrdlockandpthread_rwlock_timedwrlockcall these functions and change with them. So dopthread_rwlock_reltimedrdlock_npandpthread_rwlock_reltimedwrlock_np, which #9265 added tomainafter this branch's base; they forward toscePthreadRwlockTimedrdlockandscePthreadRwlockTimedwrlock. Two tests expectedEDEADLKfrom them, which no measurement or FreeBSD source supports:guest_pthread_rwlock, from the POSIX timed rwlock locks. It now expectsETIMEDOUT, and its 5 s deadline is now 20 ms, since the call really waits.guest_pthread_mutex_errors, frompthread_mutex_reltimedlock_npon a mutex the caller holds (added in #8400, which describes that function asscePthreadMutexTimedlockwith POSIX error numbers). It now expectsETIMEDOUTafter its 1000 us timeout.The adaptive and normal mutex types and the POSIX names were not measured;
docs/dev/debt/libkernel.mdsays so.Tested
BUILD_TESTING=ON, onmain1b5d7021a.guest_kernel_errorspasses 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 returnsETIMEDOUTafter at least 15 ms of a 20 ms timeout, and the lock is taken normally once released. It fails without the changes toMutex.cppandRwlock.cpp.guest_kernel_errors,guest_pthread_rwlock,guest_pthread_mutex_errorsandguest_pthread_timed_lockspass 3 of 3.python tools/select_tests.py ctest build --output-on-failure --timeout 120 --parallel 4: onmain1b5d7021aall 739 selected tests outsideagc_pass. A run of all 1517 selected tests one rebase earlier, onmain47fa6eecf, passed 1514; the 3 failures wereagc_driver_sample_order,agc_driver_sample_order_grid_referenceandagc_driver_sample_order_recipe_depth_locationsfrom #6683, which fail with the same messages on thatmainon this RTX 4090.agc_driver_image_array_window_past_layer_limitis not built: it does not compile with MinGW onmain(#8310).python tools/check_conventions.py --base origin/mainpasses.python tools/pr_overlap.py --branch fix/pthread-timed-self-lockreports no open PR changing the same lines or exports.3959305a27:guest_pthread_mutex_errors,guest_pthread_rwlockandguest_kernel_errorspass 3 of 3, and all three fail withMutex.cppandRwlock.cpprestored to the merge base.main38d463a6cfafter #10706 moved the test declarations into the public headers;GuestKernelErrors.cppnow includesPthread/include/Rwlock.hppfor the rwlock calls. At55ff5484d6,guest_kernel_errors,guest_pthread_mutex_errorsandguest_pthread_rwlockpass 3 of 3, and all three fail 3 of 3 withMutex.cppandRwlock.cppfrommain.public_headersand the conventions check pass.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
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 ontomain; its merge-base is1b5d7021a).ctest -R '^(guest_pthread_mutex_errors|guest_pthread_rwlock|guest_kernel_errors)$'passes 3 of 3 (all three tests).libkernel/Pthread/src/Mutex.cpp and Rwlock.cpprestored to the merge-base (the PR's tests kept): fails:guest_pthread_mutex_errorsandguest_pthread_rwlockabort,guest_kernel_errorsfails too.Not tested: macOS, Windows, a title run.
AI-assisted (Claude Code).
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.cppandRwlock.cpprestored. I added it to the Tested section.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T12:56:23Z.
Review: APPROVED
Checked against main
99feaf0eb8and FreeBSD 11.4 lib/libthr/thread/thr_mutex.c / thr_rwlock.c (8c639945):scePthreadMutexTimedlock, Rwlock.cpp:105/134), so this isn't already done.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.pthread_rwlock_reltimedrdlock_np/pthread_rwlock_reltimedwrlock_nplanded 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.GuestPthreadTimedLocks.cpp:75(EDEADLK) is unaffected.Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.