fix(libkernel): cancel before acquiring a ready timed semaphore #6103
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6103"
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 Zinebyte on 2026-10-14T22:11:55Z.
What
A guest thread with cancellation already pending calls
sem_timedwaiton a semaphore containing one permit. The current fast path returns success and consumes that permit without checking cancellation, even thoughsem_waitalready cancels before acquiring it.Check cancellation after validating the semaphore and before the timed wait's ready-count fast path. Add four deterministic cases to the existing guest pthread cancellation test: enabled cancellation with future and expired deadlines preserves the permit, disabled cancellation consumes it normally, and an uncanceled wait with an expired deadline still acquires an available permit.
This follows FreeBSD 11.4's
_sem_clockwait_npordering: validate the semaphore, test cancellation, then attempt acquisition;_sem_timedwaitdelegates to it (source). Invalid semaphore handling still precedes cancellation. For an empty valid semaphore with pending cancellation and an invalid deadline, cancellation now takes precedence over deadline validation, as in that source implementation.Tested
Linux x86-64: configured the normal build with pinned submodules and built the actual libc/libkernel libraries and relevant runtime test executables. The new integrated cancellation test fails against unchanged production code; the one-line fix passes all seven relevant semaphore and cancellation CTests (0.23 s total after rebuilding against upstream
922abe6a4). An independent probe linked to the actual libraries reproduces the same failure and confirms cancellation preserves the permit after the fix. No mocked cancellation or retail-title claim.Independent adversarial review found no blocking issues. Conventions and whitespace checks pass. Hosted full runtime CI requires a maintainer's manual run under the repository's current workflow.
Manual review of the partial-overlap notice against main
fa9365ca74: #6044 relocates this test into its threads directory, and Git preserves every cancellation case through that rename. No conflicting implementation or dependency was found.Checklist
main; no other open PR implements the same functions.mdfiles or images added to the repositoryImported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-15T02:09:39Z.
I ran this PR's test on Linux.
Setup: this PR's diff applied to main at
8cd937e29(the PR's base is 44 commits behind it), default CMake build (CMAKE_BUILD_TYPE empty, unoptimized), clean environment.ctest -R '^guest_pthread_cancel$', 3 runs each. The test did not open a GPU device, so this is a CPU-only run.Requirethere prints no message, so I took a backtrace of one more run under gdb: it stops inside the newCheckAvailableTimedSemaphore, called frommain.So the new check catches the missing cancellation point here. I did not check Windows or macOS.
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T12:14:45Z.
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.
core/libs/tests/GuestPthreadCancel.cppImported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-16T22:59:33Z.
Checked the cancel-before-acquire ordering against sem_wait/WaitFor, the new cases fail without the change and pass with it, scoped ctest green; merging.