Skip to content

Add Runtime.runUntilComplete() convenience method - #19

Merged
lalinsky merged 1 commit into
mainfrom
add-run-until-complete
Oct 3, 2025
Merged

lalinsky merged 1 commit into
mainfrom
add-run-until-complete

Conversation

@lalinsky

@lalinsky lalinsky commented Oct 3, 2025 •

Copy link
Copy Markdown
Owner

Summary

Implements a convenience method that combines spawn() + run() + result() into a single call, similar to Python's asyncio.run() and Rust's tokio::block_on().

This simplifies the common pattern of spawning a single main task and running the event loop until completion.

Changes

  • Add runUntilComplete() method to Runtime (src/runtime.zig)
  • Add ReturnPayload() helper to extract payload type from error unions
  • Update 9 single-task tests to use the simpler API
  • Update examples/sleep.zig to demonstrate the simplified usage

API Design

Before:

var handle = try runtime.spawn(myTask, .{&runtime}, .{});
defer handle.deinit();
try runtime.run();
try handle.result();

After:

try runtime.runUntilComplete(myTask, .{&runtime}, .{});

Error Handling

The method returns !ReturnPayload(func) which properly merges:

  • Errors from spawn() (allocation, etc.)
  • Errors from run() (event loop errors)
  • Errors from the task itself

Multi-task tests (server/client patterns) continue to use the existing spawn() + run() pattern as they require concurrent task execution.

Testing

All 41 tests pass ✓

Summary by CodeRabbit

  • New Features
    • Added a convenience API to run tasks to completion in a single call, simplifying usage and error handling.
  • Refactor
    • Streamlined task orchestration across examples and internal code by replacing manual task management with the new one-call flow.
  • Tests
    • Updated tests to use the new API, reducing boilerplate and improving readability with no behavioral changes to test outcomes.

Implements a convenience method that combines spawn() + run() + result()
into a single call, similar to Python's asyncio.run() and Rust's
tokio::block_on().

This simplifies the common pattern of spawning a single main task and
running the event loop until completion. The method properly handles
error propagation from spawn(), run(), and the task itself.

Changes:
- Add runUntilComplete() method to Runtime
- Add ReturnPayload() helper to extract payload type from error unions
- Update single-task tests to use the simpler API
- Update examples/sleep.zig to demonstrate the simplified usage

Multi-task tests (server/client patterns) continue to use the existing
spawn() + run() pattern as they require concurrent task execution.
@coderabbitai

coderabbitai Bot commented Oct 3, 2025 •

Copy link
Copy Markdown
Contributor

Walkthrough

Consolidated task execution by introducing Runtime.runUntilComplete and updating tests/examples to use it instead of manual spawn/run/result patterns. Added a helper to compute return payload types. No public API changes except the new Runtime method.

Changes

Cohort / File(s) Summary of Changes
Runtime API addition
src/runtime.zig
Added Runtime.runUntilComplete(func, args, options) and helper ReturnPayload(func). Integrates spawn, event-loop run, and result retrieval; propagates errors.
Tests/control-flow refactor
src/file.zig, src/fs.zig, src/net.zig, src/sync.zig
Replaced spawn/deinit/run/result sequences with a single runtime.runUntilComplete(...) call across tests. Removed explicit task lifecycle handling.
Example update
examples/sleep.zig
Simplified example by replacing separate task handle and runtime driving with runUntilComplete(...).

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Caller as Test/Example
  participant RT as Runtime
  participant EL as Event Loop
  participant Task as Spawned Task

  Caller->>RT: runUntilComplete(func, args, options)
  RT->>RT: spawn(func, args)
  alt spawn error
    RT-->>Caller: error
  else spawned
    RT->>EL: run()
    EL->>Task: execute until completion
    alt task error
      Task-->>EL: error
      EL-->>RT: error
      RT-->>Caller: error
    else success
      Task-->>EL: payload
      EL-->>RT: payload
      RT-->>Caller: payload
    end
  end

  note right of RT: Combines spawn + run + result
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Poem

I pressed a single button—hop!—and ran the loop to end,
No juggling little tasks, no handles to befriend.
One thump of paw: complete! the payload hops back home,
The burrow’s tests now simpler, less places for to roam.
Carrot-latency: reduced. Bunny happy. Done. 🥕

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The proposed title succinctly and specifically describes the main change—adding a new convenience method runUntilComplete to the Runtime API—so it clearly informs readers of the primary enhancement without extraneous detail.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch add-run-until-complete

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 73471bd and 44aa26b.

📒 Files selected for processing (6)
  • examples/sleep.zig (1 hunks)
  • src/file.zig (4 hunks)
  • src/fs.zig (1 hunks)
  • src/net.zig (2 hunks)
  • src/runtime.zig (2 hunks)
  • src/sync.zig (4 hunks)
🔇 Additional comments (4)
src/sync.zig (4)

375-375: LGTM! Clean migration to runUntilComplete.

The single-task test correctly uses the new API, eliminating spawn/defer/run boilerplate. Error handling is preserved via try.


449-449: LGTM! Correct single-task migration.

The test properly uses runUntilComplete. Error handling for the timeout is correctly contained within the task function.


584-584: LGTM! Consistent migration pattern.

The test correctly applies runUntilComplete for a single-task scenario with proper error handling.


650-650: LGTM! Straightforward migration.

The test correctly uses runUntilComplete for a single-task scenario. No error handling needed in the task function itself.


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.

@lalinsky
lalinsky merged commit 02de960 into main Oct 3, 2025
@lalinsky
lalinsky deleted the add-run-until-complete branch October 23, 2025 21:01
lalinsky added a commit that referenced this pull request Jan 6, 2026
Make backend waker responsible for draining:
- Move drain() calls from loop.processAsyncHandles() to backend tick() methods
- Backends now own and control their waker lifecycle

Rename async_impl to waker:
- Rename AsyncImpl struct to Waker across all backends (poll, kqueue, epoll)
- Change field from optional async_impl to non-optional waker
- Simplify code by removing unnecessary null checks

Remove redundant has_fds checks:
- Poll and epoll backends no longer need to check if waker exists
- Waker is always initialized and available
lalinsky added a commit that referenced this pull request Jan 6, 2026
* Add loongarch64 architecture support

* Add loongarch64 CI test

* Add sp and fp to loongarch64 clobber list
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