Skip to content

fix(libkernel): wait on absolute condition deadlines beyond 71 minutes - #17286

Merged
adriadam10 merged 1 commit into
boykopovar:mainfrom
amazedturnip:fix/libkernel-abs-timedwait-deadline
Nov 14, 2026
Merged

adriadam10 merged 1 commit into
boykopovar:mainfrom
amazedturnip:fix/libkernel-abs-timedwait-deadline

Conversation

@amazedturnip

Copy link
Copy Markdown
Contributor

What

pthread_cond_timedwait (and scePthreadCondTimedwait through the same path) converted an absolute deadline to a remaining time in 32-bit microseconds, so a deadline more than 4294.967 s (71.6 minutes) away was waited as exactly 4294.967 s and then returned ETIMEDOUT. A guest condition_variable::wait_until with a far or "forever" time point that stays parked longer than that got a spurious timeout instead of waiting for the signal.

CondOperations::AbsoluteTimedwait already hands a 64-bit nanosecond deadline to waitUntil; only the conversion narrowed it. The new PosixThread::AbsoluteDeadlineNanos (Pthread/Posix/Common.hpp) builds that deadline directly from the guest timespec with the same validation, the same expired-deadline handling and the same microsecond rounding as RelativeMicroseconds. Deadlines further than one year out are capped at one year, which keeps the host condition variable's duration arithmetic away from overflow (the same reason #14228 clamps per park for ksem). POSIX defines abstime as a 64-bit timespec and ends the wait at that time or on a signal.

Out of scope, so follow-ups stay separate: pthread_mutex_timedlock, sem_timedwait and pthread_rwlock_timedrdlock/wrlock still convert through RelativeMicroseconds because their sce entry points take 32-bit microseconds. Posix/Sem.cpp and Posix/Rwlock.cpp are also being changed by open #14103 and #14108, so they are not touched here. RelativeMicroseconds stays for them.

Depends on: none
Fixes: none
Unblocks: none

Tested

  • Linux (WSL2, Release, Ninja), on e0af82d (current upstream/main).
  • guest_cond_timedwait gains DistantAbsoluteDeadline: the new helper's deadline for now + 7200 s and now + 86400 s is within one second of that distance, a deadline at INT64_MAX seconds is more than 30 days out, a past deadline is already expired, and a pthread_cond_timedwait on now + 2 h returns 0 after a signal about 50 ms later.
    • with this change: 3 of 3 pass
    • negative control (the one-year cap replaced with 4294 s, which is what the old conversion allowed): 3 of 3 fail at the 7200 s distance check
    • restored and rebuilt: 3 of 3 pass
  • Regression: the 96 pthread, cond, mutex, rwlock, semaphore, ksem and libc _Cnd suites (all guest_* tests for the libkernel and libc timed-wait paths that tools/select_tests.py selects for the changed files), 3 runs, all passed.
  • The 71-minute timeout itself cannot be run in CI; the helper arithmetic is the deterministic check.
  • No title was involved: this is the POSIX deadline contract, not a measured game failure. Windows and macOS: not run.

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 (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

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@amazedturnip

Copy link
Copy Markdown
Contributor Author

The windows failure is unrelated to this change. guest_avplayer_http and avplayer_http_transport hit the 120 s ctest timeout, and the new guest_cond_timedwait passed in the same job (run 42504215014). Neither test touches the pthread condition code. I can't rerun the job from a fork; could someone rerun it?

@JJenkx

JJenkx commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

Windows check of head 4cee988c86, merged onto main 69078e84a1 (fcfaae2dc5, clean merge).

Setup: Windows 11 Enterprise 22621, WinLibs GCC 15.2 UCRT, Release. The hosted Windows check does not run these tests on pull requests.

  • guest_cond_timedwait passes in 3 of 3 runs, none skipped.

AI-assisted: yes.

@PrivacyOnion PrivacyOnion 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.

Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 4cee988c86:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_cond_timedwait_tests built cleanly.
  • CTest (guest_cond_timedwait):
    1/1 Test #4011: guest_cond_timedwait .............   Passed    0.41 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libkernel/Pthread/Posix/Common.hpp, core/libs/prx/libkernel/Pthread/src/Cond.cpp reverted to main, the new tests do not build/link without the change; restoring it makes them build and pass.
  • Code reading:
    • Verified pointer safety and input validation in PosixThread::AbsoluteDeadlineNanos (core/libs/prx/libkernel/Pthread/Posix/Common.hpp): confirmed !abstime || abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000 rejects null pointers and invalid nanoseconds with false, triggering throw std::invalid_argument in CondOperations::AbsoluteTimedwait (Cond.cpp) which maps to POSIX_EINVAL.
    • Verified expired deadline handling in PosixThread::AbsoluteDeadlineNanos (Common.hpp): past deadlines where abstime->tv_sec < now.tv_sec - 1 or remaining <= 0 correctly set *deadline = TimedWait::DeadlineNanos(0), ensuring immediate timeout (sceTimedOut / POSIX_ETIMEDOUT) without blocking.
    • Verified overflow protection and horizon clamping in PosixThread::AbsoluteDeadlineNanos (Common.hpp): deadlines beyond horizonSeconds (365 days) are clamped to 1 year via TimedWait::DeadlineNanos(static_caststd::uint64_t(horizonSeconds) * 1000000ULL), preventing host std::chrono duration overflow on extreme values like INT64_MAX.
    • Verified the fix for the 71.6-minute truncation in CondOperations::AbsoluteTimedwait (core/libs/prx/libkernel/Pthread/src/Cond.cpp): confirmed deadline calculation preserves 64-bit nanoseconds (std::uint64_t deadlineNanos) instead of narrowing through 32-bit KernelUseconds, correctly passing full 64-bit deadlines to waitUntil.
    • Verified strict compliance with repository rules in AGENTS.md: confirmed zero comments were added to production or test code (Common.hpp, Cond.cpp, GuestCondTimedwait.cpp), no silent stubs were introduced, no untracked files were added, and conventional commit naming is respected.
    • Verified test rigor and resource cleanup in core/libs/tests/GuestCondTimedwait.cpp: DistantAbsoluteDeadline thoroughly exercises 2-hour (7200 s), 1-day (86400 s), INT64_MAX, and past (-5 s) deadlines, alongside an end-to-end threaded cond wait/signal with proper mutex lock/unlock, scePthreadJoin, and cond/mutex destruction.
  • Minor: please drop the Generated with footer from the description; the AI-assisted checkbox can stay.

Looks Good To Me

@github-actions

github-actions Bot commented Nov 11, 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 17638 core/libs/prx/libkernel/Pthread/src/Cond.cpp

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@amazedturnip The deadline change looks right to me. On this head, Windows CI failed only on guest_avplayer_http and avplayer_http_transport, both timing out. I don't see them fail on other recent runs, and I can't rule out that their HTTP code waits through pthread_cond_timedwait. Could you rerun the Windows job, or run those two tests locally on this head? If they pass, I'll treat it as a flake.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@amazedturnip Update on my earlier ask: the same two AvPlayer HTTP timeouts showed up on an unrelated test-only PR (#17433), so they look like a CI flake and not your change. No need to run them locally.

@adriadam10 adriadam10 self-assigned this Nov 12, 2026
@adriadam10
adriadam10 merged commit 418e5c4 into boykopovar:main Nov 14, 2026
13 of 14 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Checked the 64-bit deadline arithmetic (one-year cap, expired and far-future rows), that the new DistantAbsoluteDeadline test pins the 7200 s/86400 s distances and fails with the old 4294 s limit, and the third-party Windows and Linux runs. The windows failure is the known AvPlayer HTTP timeout flake; merged.

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.

5 participants