fix(libkernel): keep errno across timed condition waits on macOS #28369

Open
Jazeupoi wants to merge 1 commit from upstream-pr/28369 into main
Jazeupoi commented 2026-11-30 10:00:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Jazeupoi on 2026-11-30T10:00:20Z.


What

On macOS guest_event_flag_wake aborts on every run. Its read-only timeout case (added in #22055) expects sceKernelWaitEventFlag to return SCE_KERNEL_ERROR_EFAULT with errno unchanged, and the call returns EFAULT with errno at 316. TimedWait::Condition waits through std::condition_variable_any; when that wait times out on macOS, libpthread leaves the raw __psynch_cvwait status in errno: 316 is ETIMEDOUT (60) with ECVCLEARED (0x100). glibc's condition wait does not touch errno, which is why the test passes on Linux.

Condition::Wait and Condition::WaitUntil now save errno before the wait and restore it afterwards, so a guest call that waits keeps the errno it was called with on every host.

Tested

macOS 27.0.1, Apple M4, x86_64 Release under Rosetta 2, Apple clang 21.0.0. The tree was main 1b8f79a5b5 plus #26739, #27739 and #17425, which macOS needs to build; none of them touches this code, and TimedWait.hpp, EventFlag.cpp and the test are unchanged up to b3c7769bc5.

  • guest_event_flag_wake: 50/50. Without the change: 0/3, aborting on the read-only timeout check (rc=0x8002000e errno=316 pattern=0x5 timeout=1000).
  • The 408 tests whose working directory is libkernel: no new failures compared with main. The 8 that still fail on this Mac also fail without the change, and each already has an open PR or issue.
  • The 111 wait-related libkernel tests (semaphores, condition variables, mutexes, event flags, sleeps): 5 rounds each, all pass.
  • Linux and Windows: not run yet; I will add them here.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)

Summary by CodeRabbit

  • Bug Fixes
    • Coarse condition-variable waits now preserve the caller’s errno value after waiting.
    • Timed waits continue to report whether they timed out.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5563024490 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/28369). Originally posted by [Jazeupoi](https://github.com/Jazeupoi) on 2026-11-30T10:00:20Z. --- ### What On macOS `guest_event_flag_wake` aborts on every run. Its read-only timeout case (added in #22055) expects `sceKernelWaitEventFlag` to return `SCE_KERNEL_ERROR_EFAULT` with `errno` unchanged, and the call returns `EFAULT` with `errno` at 316. `TimedWait::Condition` waits through `std::condition_variable_any`; when that wait times out on macOS, libpthread leaves the raw `__psynch_cvwait` status in `errno`: 316 is `ETIMEDOUT` (60) with `ECVCLEARED` (0x100). glibc's condition wait does not touch `errno`, which is why the test passes on Linux. `Condition::Wait` and `Condition::WaitUntil` now save `errno` before the wait and restore it afterwards, so a guest call that waits keeps the `errno` it was called with on every host. ### Tested macOS 27.0.1, Apple M4, x86_64 Release under Rosetta 2, Apple clang 21.0.0. The tree was `main` `1b8f79a5b5` plus #26739, #27739 and #17425, which macOS needs to build; none of them touches this code, and `TimedWait.hpp`, `EventFlag.cpp` and the test are unchanged up to `b3c7769bc5`. - `guest_event_flag_wake`: 50/50. Without the change: 0/3, aborting on the read-only timeout check (`rc=0x8002000e errno=316 pattern=0x5 timeout=1000`). - The 408 tests whose working directory is libkernel: no new failures compared with `main`. The 8 that still fail on this Mac also fail without the change, and each already has an open PR or issue. - The 111 wait-related libkernel tests (semaphores, condition variables, mutexes, event flags, sleeps): 5 rounds each, all pass. - Linux and Windows: not run yet; I will add them here. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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) and the licence notice of code adapted from upstream (see AGENTS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Code) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Bug Fixes** * Coarse condition-variable waits now preserve the caller’s `errno` value after waiting. * Timed waits continue to report whether they timed out. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
MarcZX10 commented 2026-11-30 10:01:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-30T10:01:34Z.

Review: COMMENTED


Review not completed — comments

I could not inspect the prepared review or repository files because this session exposes no reader or filesystem tools, so no verdict on the change is supported by evidence.

What I ran

No commands. The pinned head was provided as e44c9a2e173ffb2b8c41b7de985549203dc944d9.

Results

Check Result Note
Diff and changed files not run Review inputs unavailable
Targeted source inspection not run repo_read.py unavailable

Findings

Blocking

None verified.

Non-blocking

None verified.

Not verified

description.md, commits.md, diff.patch, manifest.json, and files.json were not read. No changed files were read; the files listed for review could not be determined.

To get this to a good state

Provide access to the prepared review folder and the pinned repository reader so the review can be completed.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6110099856 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/28369#pullrequestreview-6110099856). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-30T10:01:34Z. **Review:** COMMENTED --- # Review not completed — comments I could not inspect the prepared review or repository files because this session exposes no reader or filesystem tools, so no verdict on the change is supported by evidence. ## What I ran No commands. The pinned head was provided as `e44c9a2e173ffb2b8c41b7de985549203dc944d9`. ## Results | Check | Result | Note | |---|---|---| | Diff and changed files | not run | Review inputs unavailable | | Targeted source inspection | not run | `repo_read.py` unavailable | ## Findings ### Blocking None verified. ### Non-blocking None verified. ## Not verified `description.md`, `commits.md`, `diff.patch`, `manifest.json`, and `files.json` were not read. No changed files were read; the files listed for review could not be determined. ## To get this to a good state Provide access to the prepared review folder and the pinned repository reader so the review can be completed.
NobleBrains commented 2026-11-30 23:36:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by NobleBrains on 2026-11-30T23:36:47Z.

Review: APPROVED


Confirmed on macOS 15, the OS of the macos-15 runner, in addition to the macOS 27 run in the description.

Setup: Apple M4, macOS 15.7.9, Apple clang 17.0.0 (clang-1700.6.3.2), x86_64 Release under Rosetta, BUILD_TESTING=ON.
Tree: main 93d64f2d1e + the open macOS build fixes #27201, #27739, #27843, #17425, #27827, #27767 (main does not build on macOS without them, see #28168), + #22091, then this PR at e44c9a2e17. It merges cleanly.

guest_event_flag_wake, 30 sequential runs failures
without this PR 30/30
with this PR 0/30

I also had an earlier, independent patch for the timed wait only (save and restore errno around coarse.wait_for). That version made the same test pass on this Mac too. This PR also covers the untimed coarse.wait, so I'm dropping mine in favour of this one.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6122480838 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/28369#pullrequestreview-6122480838). Originally posted by [NobleBrains](https://github.com/NobleBrains) on 2026-11-30T23:36:47Z. **Review:** APPROVED --- Confirmed on macOS 15, the OS of the `macos-15` runner, in addition to the macOS 27 run in the description. **Setup:** Apple M4, macOS 15.7.9, Apple clang 17.0.0 (clang-1700.6.3.2), x86_64 Release under Rosetta, `BUILD_TESTING=ON`. **Tree:** `main` `93d64f2d1e` + the open macOS build fixes #27201, #27739, #27843, #17425, #27827, #27767 (`main` does not build on macOS without them, see #28168), + #22091, then this PR at `e44c9a2e17`. It merges cleanly. | `guest_event_flag_wake`, 30 sequential runs | failures | |---|---| | without this PR | 30/30 | | with this PR | 0/30 | I also had an earlier, independent patch for the timed wait only (save and restore `errno` around `coarse.wait_for`). That version made the same test pass on this Mac too. This PR also covers the untimed `coarse.wait`, so I'm dropping mine in favour of this one. AI-assisted: yes (Claude Code)
ligmaSec commented 2026-12-03 06:37:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T06:37:03Z.

Review: COMMENTED


Tested the before and after TimedWait::Condition headers on Linux with a lock that deliberately changes errno when condition_variable_any unlocks and reacquires it. The pre-change header returned EAGAIN from both the notified wait and the timed-out wait. The PR preserved the caller’s EBUSY in both cases. This exercise used the real kernel library for notification and deadline calculation.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6164805581 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/28369#pullrequestreview-6164805581). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T06:37:03Z. **Review:** COMMENTED --- Tested the before and after `TimedWait::Condition` headers on Linux with a lock that deliberately changes `errno` when `condition_variable_any` unlocks and reacquires it. The pre-change header returned `EAGAIN` from both the notified wait and the timed-out wait. The PR preserved the caller’s `EBUSY` in both cases. This exercise used the real kernel library for notification and deadline calculation. AI-assisted: yes.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/28369:upstream-pr/28369'
git switch 'upstream-pr/28369'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/28369'
git switch 'upstream-pr/28369'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/28369'
git switch 'upstream-pr/28369'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/28369'
git switch 'main'
git merge --squash 'upstream-pr/28369'
git switch 'main'
git merge --ff-only 'upstream-pr/28369'
git switch 'main'
git merge 'upstream-pr/28369'
git push origin 'main'
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!28369
No description provided.