fix(libkernel): cancel before acquiring a ready timed semaphore #6103

Merged
Zinebyte merged 1 commit from upstream-pr/6103 into main 2026-10-17 10:15:32 +00:00
Zinebyte commented 2026-10-14 22:11:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Zinebyte on 2026-10-14T22:11:55Z.


What

A guest thread with cancellation already pending calls sem_timedwait on a semaphore containing one permit. The current fast path returns success and consumes that permit without checking cancellation, even though sem_wait already 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_np ordering: validate the semaphore, test cancellation, then attempt acquisition; _sem_timedwait delegates 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

  • 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; no silent stubs added
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4865274822 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6103). Originally posted by [Zinebyte](https://github.com/Zinebyte) on 2026-10-14T22:11:55Z. --- ### What A guest thread with cancellation already pending calls `sem_timedwait` on a semaphore containing one permit. The current fast path returns success and consumes that permit without checking cancellation, even though `sem_wait` already 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_np` ordering: validate the semaphore, test cancellation, then attempt acquisition; `_sem_timedwait` delegates to it ([source](https://github.com/freebsd/freebsd-src/blob/releng/11.4/lib/libc/gen/sem_new.c#L374-L417)). 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 - [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 - [x] Unimplemented paths throw; no silent stubs added - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
Dextroid17 commented 2026-10-15 02:09:39 +00:00 (Migrated from github.com)

Imported 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.

  • with this PR: 3 of 3 pass.
  • with Pthread/Posix/Sem.cpp put back to main's version (the new test kept): 3 of 3 abort. The Require there prints no message, so I took a backtrace of one more run under gdb: it stops inside the new CheckAvailableTimedSemaphore, called from main.

So the new check catches the missing cancellation point here. I did not check Windows or macOS.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6182791182 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6103#issuecomment-6182791182). Originally posted by [Dextroid17](https://github.com/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. - with this PR: 3 of 3 pass. - with Pthread/Posix/Sem.cpp put back to main's version (the new test kept): 3 of 3 abort. The `Require` there prints no message, so I took a backtrace of one more run under gdb: it stops inside the new `CheckAvailableTimedSemaphore`, called from `main`. So the new check catches the missing cancellation point here. I did not check Windows or macOS. AI-assisted: yes
github-actions[bot] commented 2026-10-15 12:14:45 +00:00 (Migrated from github.com)

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.

Pull request Shared files not compared
PR 6044 core/libs/tests/GuestPthreadCancel.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6191796845 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6103#issuecomment-6191796845). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-15T12:14:45Z. --- <!-- pr-overlap --> 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 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/GuestPthreadCancel.cpp` |
adriadam10 commented 2026-10-16 22:59:33 +00:00 (Migrated from github.com)

Imported 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6226272439 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6103#issuecomment-6226272439). Originally posted by [adriadam10](https://github.com/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.
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!6103
No description provided.