Skip to content

Split futex wait into separate wait() and timedWait() methods - #289

Merged
lalinsky merged 4 commits into
mainfrom
fix-futex-timedwait
Feb 6, 2026
Merged

lalinsky merged 4 commits into
mainfrom
fix-futex-timedwait

Conversation

@lalinsky

@lalinsky lalinsky commented Feb 5, 2026

Copy link
Copy Markdown
Owner

Summary

This PR refactors the futex wait API to provide clearer semantics and better error handling by splitting the unified wait() function into two distinct methods:

  • wait(ptr, current) - Blocks indefinitely without timeout
  • timedWait(ptr, current, timeout) - Blocks with timeout, returns error.Timeout on expiration

Changes

  • API clarity: Removed optional timeout parameter from wait(), making the infinite wait case explicit
  • Error handling: timedWait() now returns error.Timeout instead of silently returning on timeout
  • Platform coverage: Updated all platform implementations:
    • Linux (futex)
    • Windows (RtlWaitOnAddress)
    • Darwin (__ulock_wait)
    • FreeBSD (_umtx_op)
    • OpenBSD (futex)
    • DragonFly (umtx_sleep)
    • NetBSD (___lwp_park60)
  • Event API: Updated EventFutex and EventNetBSD to expose both methods
  • Internal updates: Updated all callers (mutexes, condition variables, waiters)
  • Tests: Added test for successful timedWait() and updated timeout test to expect error

Benefits

  • Type safety: Timeouts are now explicit in the API, not hidden in optional parameters
  • Error propagation: Callers can distinguish between successful wakeup and timeout
  • Clearer intent: Reading code makes it obvious whether an operation can timeout

@coderabbitai

coderabbitai Bot commented Feb 5, 2026 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

Split wait APIs into explicit indefinite and timeout-aware paths: wait(current) now blocks indefinitely, and timedWait(current, timeout) returns error.Timeout on timeout. Call sites and platform-specific futex/event implementations were updated to use the new signatures and error semantics.

Changes

Cohort / File(s) Summary
Common wait call-sites
src/common.zig
Removed passing explicit timeout to event.wait; timed paths now call event.timedWait(current, remaining) and handle/swallow error.Timeout where intended.
OS-specific futex & event implementations
src/os/thread_wait.zig
Introduced timedWait(ptr, current, timeout) -> error{Timeout}!void for multiple platform futex/event types and changed wait(ptr, current) to no-timeout indefinite waits. Updated implementations and tests to call wait(...) for blocking and timedWait(...) for timeout-aware behavior.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Poem

Two waits, now split: one holds the shore,
One watches the clock and says “no more.” ⏳
Code waits in silence, or times out with grace,
Threads find their rhythm, each in its place. 🧵✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title directly and precisely describes the main change: splitting futex wait into separate wait() and timedWait() methods, which matches the core refactoring in the changeset.
Description check ✅ Passed The description provides a clear, detailed explanation of the API refactoring, platform coverage, and benefits, all directly aligned with the changeset modifications.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix-futex-timedwait

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

- wait() now blocks indefinitely without timeout parameter
- timedWait() takes timeout and returns error.Timeout on expiration
- Updated all platform implementations (Linux, Windows, Darwin, FreeBSD, OpenBSD, DragonFly, NetBSD)
- Updated Event API and all internal callers
- Added test for timedWait success case
- Updated timeout test to expect error.Timeout
@lalinsky
lalinsky force-pushed the fix-futex-timedwait branch from 9b692c1 to cd871c5 Compare February 6, 2026 05:44

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@src/os/thread_wait.zig`:
- Around line 544-558: In timedWait, the timeout is passed to sys.umtx_sleep in
nanoseconds (timeout_ns from timeout.toNanoseconds()), but DragonFly's
umtx_sleep expects microseconds; replace the use of timeout.toNanoseconds() with
timeout.toMicroseconds() (rename timeout_ns accordingly), ensure the value is
int-cast the same way before calling sys.umtx_sleep, and keep the rest of
timedWait and the sys.umtx_sleep call unchanged.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 2

🤖 Fix all issues with AI agents
In `@src/os/thread_wait.zig`:
- Around line 544-558: timedWait currently does `@intCast`(timeout_us) which will
panic for Duration values > ~2147s; change the conversion of timeout_us before
calling sys.umtx_sleep by clamping/casting to the target C int safely (use
std.math.cast or equivalent with a maxInt fallback like the Darwin path) so the
value passed to sys.umtx_sleep is capped at the platform C int max and avoids a
safety panic; update the conversion for both the timeout_us argument and any
other `@intCast` usages in timedWait to use the safe cast/clamp and keep the rest
of the sys.umtx_sleep call unchanged.
- Around line 544-558: In timedWait, avoid using `@intCast` on timeout_us (u64)
which can panic for large durations; instead convert with std.math.cast to the C
int width and clamp to the C max value on overflow (use a maxInt fallback
similar to the Darwin implementation) and also ensure zero microseconds does not
become an infinite wait by clamping the converted timeout to at least 1 (i.e.,
set timeout_us_converted = max(1, casted_or_max)); update the call site of
sys.umtx_sleep(&ptr.raw, `@intCast`(current), timeout_us_converted) accordingly so
umtx_sleep always receives a safe i32-range, non-zero microsecond value.

Comment thread src/os/thread_wait.zig Resolved
Use std.math.cast with fallback to maxInt to prevent @intcast panic
on timeouts longer than ~35 minutes. Also clamp to @max(1, ...) to
prevent sub-microsecond durations from becoming 0 (infinite wait).
@lalinsky
lalinsky merged commit 0ba453d into main Feb 6, 2026
14 of 15 checks passed
@lalinsky
lalinsky deleted the fix-futex-timedwait branch February 6, 2026 06:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant