Skip to content

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

Open
Jazeupoi wants to merge 1 commit into
boykopovar:mainfrom
Jazeupoi:fix/macos-wait-keep-errno
Open

Jazeupoi wants to merge 1 commit into
boykopovar:mainfrom
Jazeupoi:fix/macos-wait-keep-errno

Conversation

@Jazeupoi

@Jazeupoi Jazeupoi commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

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.

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@coderabbitai

coderabbitai Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Changes: Timed condition waits

Layer / File(s) Summary
Preserve errno in coarse waits
core/libs/prx/libkernel/Time/include/TimedWait.hpp
The header adds <cerrno>. Both coarse wait paths save and restore the caller’s errno; the Windows fine-grained waiter path is unchanged. WaitUntil retains its timeout-result calculation.

Suggested labels: libkernel

Suggested reviewers: devilismyfriend, privacyonion

Change: Bug fix

Merge Risk: Minimal · up to e44c9

Condition waits now leave the caller's errno unchanged. This fixes a timeout check that failed on macOS. I found no concrete risk to other platforms or callers, so the change looks safe to merge.

Security architecture risk: Minimal · up to e44c9

Coarse waits now preserve the caller’s errno on normal return. The change leaves timeout results, cancellation behavior, and access boundaries unchanged in the paths reviewed.

Security review details

Security Blast Radius

  • inferred — The direct effect is limited to the calling thread’s errno on existing coarse-wait paths. The compared code adds no entrypoint, new input, or route to a privileged operation.
Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 08452628-54cd-4ae9-8495-a9c58ab4e749
Commits

Reviewing files that changed from the base of the PR and between b3c7769 and e44c9a2.

Files selected for processing: 1

  • core/libs/prx/libkernel/Time/include/TimedWait.hpp

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

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 12130 core/libs/prx/libkernel/Time/include/TimedWait.hpp
PR 28299 core/libs/prx/libkernel/Time/include/TimedWait.hpp
PR 28808 core/libs/prx/libkernel/Time/include/TimedWait.hpp

@NobleBrains NobleBrains left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

@Jazeupoi

Jazeupoi commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Tested on Windows and Linux too, on main 483b81fb5a with this PR merged (e44c9a2e17). The change applies to every platform; glibc and the Windows runtime leave errno alone, so there it only has to stay harmless.

  • Windows 11, WinLibs GCC, Release: libkernel builds; guest_event_flag_wake, guest_event_flag_clear_all_queue, guest_event_flag_released_waiter, guest_cond_timedwait, guest_pthread_cond, guest_semaphore and guest_pthread_timed_locks pass 5/5.
  • Linux (Ubuntu 24.04 under WSL2), GCC, Release: the same tests except guest_pthread_timed_locks pass 5/5.

AI-assisted: yes (Claude Code)

@ligmaSec ligmaSec left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants