fix(libkernel): return EBUSY in scePthreadRwlockTryrdlock when writer holds lock #1200

Closed
emirplus wants to merge 1 commit from upstream-pr/1200 into main
emirplus commented 2026-10-07 17:25:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by emirplus on 2026-10-07T17:25:17Z.


What

scePthreadRwlockTryrdlock previously checked OwnsWrite(lock), which only verified whether the calling thread held the write lock. When an external thread held the write lock, foreign readers bypassed this check and attempted try_lock_shared(), misreporting contention or behaving inconsistently.

This change:

  • Adds HasWriter(lock) to check whether any thread holds the write lock (_writer != std::thread::id{}).
  • Returns SCE_KERNEL_ERROR_EBUSY immediately under write contention.

Tested

  • Windows 11 x64, MinGW-w64 GCC 15.2.0, CMake + Ninja.
  • ctest -R guest_pthread_rwlock --output-on-failure: 100% passed (0.19s).

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4776098634 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1200). Originally posted by [emirplus](https://github.com/emirplus) on 2026-10-07T17:25:17Z. --- ### What `scePthreadRwlockTryrdlock` previously checked `OwnsWrite(lock)`, which only verified whether the calling thread held the write lock. When an external thread held the write lock, foreign readers bypassed this check and attempted `try_lock_shared()`, misreporting contention or behaving inconsistently. This change: - Adds `HasWriter(lock)` to check whether any thread holds the write lock (`_writer != std::thread::id{}`). - Returns `SCE_KERNEL_ERROR_EBUSY` immediately under write contention. ### Tested - Windows 11 x64, MinGW-w64 GCC 15.2.0, CMake + Ninja. - `ctest -R guest_pthread_rwlock --output-on-failure`: 100% passed (0.19s). ### 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
Dextroid17 commented 2026-10-08 02:23:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-08T02:23:36Z.

Review: COMMENTED


Built and ran it, then tried to observe the difference — and could not, which I think is the useful result here.

Linux (CachyOS, GCC, Release):

cmake --build build --target guest_pthread_rwlock_tests --parallel 16
ctest -R ^guest_pthread_rwlock$    →  1/1 Passed

The existing test already covers this case, and it passes on both sides. core/libs/tests/GuestPthreadRwlock.cpp:114-116 starts a second thread, lets it take the write lock, and then requires:

Holder writer{&rwlock, true};
Start(writer);
Require(pthread_rwlock_tryrdlock_nid_postfix(&rwlock) == GUEST_EBUSY);

That is precisely "a writer other than the caller holds the lock". Swapping only Rwlock.cpp back to pr-1200^ and keeping the test:

pr-1200   (HasWriter):  100% passed
pr-1200^  (OwnsWrite):  100% passed

Why the parent was already correct on this path. _lock is a std::shared_timed_mutex, so try_lock_shared() fails whenever any writer holds it — the parent's OwnsWrite pre-check only changed the caller-owns-it case, and HasWriter covers that case too. So for both encodings:

writer is another thread → try_lock_shared() returns false → EBUSY   (both)
writer is the caller     → pre-check returns EBUSY                    (both)
no writer                → try_lock_shared() succeeds → 0             (both)

And the broader pre-check cannot misfire, because of the ordering elsewhere. _writer is stored after the mutex is acquired (Wrlock/Trywrlock/Timedwrlock) and cleared before it is released (Unlock clears then unlocks). That makes HasWriter mean exactly "the write lock is currently held", never "a writer once held it" — so it can never return EBUSY while the lock is actually free. My first instinct was that a wider pre-check would introduce a window where a try-lock reports a spurious EBUSY; it does not, and that is due to the ordering rather than the check itself.

So what does this change do? It makes the condition state the POSIX rule — held for writing by any thread — instead of the caller's own state, which is the clearer thing to read and the right thing to intend. But it is not a behavioural fix on this implementation: nothing currently returns 0 that should return EBUSY. The body reads as though a failure is being fixed, so a reviewer will go looking for the case that failed; if there is one (a title, a log line), a test would pin it, and if there is not, I would describe it as matching POSIX rather than fixing a result.

One suggestion that would add real value here. The safety of the wider check rests entirely on that store/clear ordering, and nothing enforces it. A test that hammers Tryrdlock from a second thread while another repeatedly takes and releases the write lock, asserting it never returns 0 while HasWriter is true, would both pin the intended semantics and fail loudly if the ordering were ever reversed. That is the invariant I would want covered rather than the individual outcomes.

This is a comment, not an approval — I have no merge rights here.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5450657802 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1200#pullrequestreview-5450657802). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-08T02:23:36Z. **Review:** COMMENTED --- Built and ran it, then tried to observe the difference — and could not, which I think is the useful result here. **Linux (CachyOS, GCC, Release):** ``` cmake --build build --target guest_pthread_rwlock_tests --parallel 16 ctest -R ^guest_pthread_rwlock$ → 1/1 Passed ``` **The existing test already covers this case, and it passes on both sides.** `core/libs/tests/GuestPthreadRwlock.cpp:114-116` starts a *second thread*, lets it take the write lock, and then requires: ```cpp Holder writer{&rwlock, true}; Start(writer); Require(pthread_rwlock_tryrdlock_nid_postfix(&rwlock) == GUEST_EBUSY); ``` That is precisely "a writer other than the caller holds the lock". Swapping only `Rwlock.cpp` back to `pr-1200^` and keeping the test: ``` pr-1200 (HasWriter): 100% passed pr-1200^ (OwnsWrite): 100% passed ``` **Why the parent was already correct on this path.** `_lock` is a `std::shared_timed_mutex`, so `try_lock_shared()` fails whenever any writer holds it — the parent's `OwnsWrite` pre-check only changed the *caller-owns-it* case, and `HasWriter` covers that case too. So for both encodings: ``` writer is another thread → try_lock_shared() returns false → EBUSY (both) writer is the caller → pre-check returns EBUSY (both) no writer → try_lock_shared() succeeds → 0 (both) ``` **And the broader pre-check cannot misfire, because of the ordering elsewhere.** `_writer` is stored *after* the mutex is acquired (`Wrlock`/`Trywrlock`/`Timedwrlock`) and cleared *before* it is released (`Unlock` clears then unlocks). That makes `HasWriter` mean exactly "the write lock is currently held", never "a writer once held it" — so it can never return EBUSY while the lock is actually free. My first instinct was that a wider pre-check would introduce a window where a try-lock reports a spurious EBUSY; it does not, and that is due to the ordering rather than the check itself. **So what does this change do?** It makes the condition state the POSIX rule — *held for writing by any thread* — instead of the caller's own state, which is the clearer thing to read and the right thing to intend. But it is not a behavioural fix on this implementation: nothing currently returns 0 that should return `EBUSY`. The body reads as though a failure is being fixed, so a reviewer will go looking for the case that failed; if there is one (a title, a log line), a test would pin it, and if there is not, I would describe it as matching POSIX rather than fixing a result. **One suggestion that would add real value here.** The safety of the wider check rests entirely on that store/clear ordering, and nothing enforces it. A test that hammers `Tryrdlock` from a second thread while another repeatedly takes and releases the write lock, asserting it never returns `0` while `HasWriter` is true, would both pin the intended semantics and fail loudly if the ordering were ever reversed. That is the invariant I would want covered rather than the individual outcomes. This is a comment, not an approval — I have no merge rights here. Written with AI assistance.
oneandonlydean commented 2026-10-08 02:44:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-08T02:44:27Z.


Closing: _lock is a std::shared_timed_mutex, so try_lock_shared() already fails while another thread holds the write lock and main returns EBUSY there; the existing test at GuestPthreadRwlock.cpp:114 passes with and without this. If you have a case where main returns 0 under a writer, comment here with it.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6051104507 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1200#issuecomment-6051104507). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-08T02:44:27Z. --- Closing: `_lock` is a `std::shared_timed_mutex`, so `try_lock_shared()` already fails while another thread holds the write lock and main returns `EBUSY` there; the existing test at `GuestPthreadRwlock.cpp:114` passes with and without this. If you have a case where main returns 0 under a writer, comment here with it.

Pull request closed

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!1200
No description provided.