fix(libkernel): return EBUSY in scePthreadRwlockTryrdlock when writer holds lock #1200
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1200"
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 emirplus on 2026-10-07T17:25:17Z.
What
scePthreadRwlockTryrdlockpreviously checkedOwnsWrite(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 attemptedtry_lock_shared(), misreporting contention or behaving inconsistently.This change:
HasWriter(lock)to check whether any thread holds the write lock (_writer != std::thread::id{}).SCE_KERNEL_ERROR_EBUSYimmediately under write contention.Tested
ctest -R guest_pthread_rwlock --output-on-failure: 100% passed (0.19s).Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)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):
The existing test already covers this case, and it passes on both sides.
core/libs/tests/GuestPthreadRwlock.cpp:114-116starts a second thread, lets it take the write lock, and then requires:That is precisely "a writer other than the caller holds the lock". Swapping only
Rwlock.cppback topr-1200^and keeping the test:Why the parent was already correct on this path.
_lockis astd::shared_timed_mutex, sotry_lock_shared()fails whenever any writer holds it — the parent'sOwnsWritepre-check only changed the caller-owns-it case, andHasWritercovers that case too. So for both encodings:And the broader pre-check cannot misfire, because of the ordering elsewhere.
_writeris stored after the mutex is acquired (Wrlock/Trywrlock/Timedwrlock) and cleared before it is released (Unlockclears then unlocks). That makesHasWritermean 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
Tryrdlockfrom a second thread while another repeatedly takes and releases the write lock, asserting it never returns0whileHasWriteris 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.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-08T02:44:27Z.
Closing:
_lockis astd::shared_timed_mutex, sotry_lock_shared()already fails while another thread holds the write lock and main returnsEBUSYthere; the existing test atGuestPthreadRwlock.cpp:114passes with and without this. If you have a case where main returns 0 under a writer, comment here with it.Pull request closed