Skip to content

Move coroutines.zig to external package - #140

Merged
lalinsky merged 1 commit into
mainfrom
external-coro-zig
Nov 12, 2025
Merged

lalinsky merged 1 commit into
mainfrom
external-coro-zig

Conversation

@lalinsky

Copy link
Copy Markdown
Owner

No description provided.

@coderabbitai

coderabbitai Bot commented Nov 12, 2025 •

Copy link
Copy Markdown
Contributor

Walkthrough

This PR replaces the internal coroutine runtime (src/coroutines.zig) with an external "coro" dependency. Build configuration is updated to wire the new dependency, imports are migrated across the codebase, and coroutine-related exports are removed from the public zio API.

Changes

Cohort / File(s) Summary
Build Configuration
build.zig, build.zig.zon
Added "coro" dependency with git URL and hash. Wired coro into the build graph with target and optimize options; registered as an import on the zio module.
Coroutine Implementation Removal
src/coroutines.zig
Entire file deleted, eliminating arch/platform-specific stack management, context switching, entry point logic, Coroutine struct, and associated tests.
Import Migration to External Coro
src/core/task.zig, src/runtime.zig, src/stack_pool.zig
Updated import sources: Coroutine, Context, and Stack-related symbols now sourced from "coro" instead of internal coroutines module.
Unused Import Cleanup
src/sync/Condition.zig, src/sync/ResetEvent.zig
Removed unused coroutines imports; no functional logic changes.
Public API Surface
src/zio.zig
Removed re-exports of coroutines module alias and Coroutine type alias; other module exports unchanged.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Areas requiring attention:

  • src/coroutines.zig removal: Verify the external "coro" module provides equivalent arch-specific context switching behavior (x86_64, aarch64, riscv64) and stack management.
  • Type boundary changes: Confirm Context and Coroutine types from external coro are compatible with existing usage in src/runtime.zig (Executor.main_context field type).
  • DEFAULT_STACK_SIZE migration: Ensure the constant imported from coro matches expected semantics in task.zig.
  • Public API breakage: The removal of coroutines and Coroutine re-exports from zio.zig is a breaking change—verify this is intentional and documented.

Possibly related PRs

  • Refactor task state #89: Modifies coroutine implementation in src/coroutines.zig, src/core/task.zig, and src/runtime.zig—directly overlaps with the same files being refactored here.
  • Add ResetEvent synchronization primitive #10: Removes coroutine module and affects sync primitives (ResetEvent, Condition) that depend on coroutine runtime, matching the cleanup pattern in this PR.
  • Implement direct context switching between coroutines #39: Changes coroutine runtime surface (Coroutine, Context, yield semantics); this PR replaces the internal implementation with external "coro," so both touch core coroutine types and scheduling.

Poem

Out with the old assembly dance,
In rides the coro library's chance—
Stack frames and contexts, now external,
Our runtime sheds its internal—
Lighter, cleaner, trust the chain. 🔗

Pre-merge checks and finishing touches

❌ Failed checks (1 inconclusive)
Check name Status Explanation Resolution
Description check ❓ Inconclusive No description was provided, making it impossible to assess alignment with the changeset. Add a description explaining why coroutines were moved to an external package and any migration notes for users.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: extracting the internal coroutines module into an external dependency (coro package).
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch external-coro-zig

📜 Recent review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bee10b9 and 884caeb.

📒 Files selected for processing (9)
  • build.zig (1 hunks)
  • build.zig.zon (1 hunks)
  • src/core/task.zig (2 hunks)
  • src/coroutines.zig (0 hunks)
  • src/runtime.zig (2 hunks)
  • src/stack_pool.zig (1 hunks)
  • src/sync/Condition.zig (0 hunks)
  • src/sync/ResetEvent.zig (0 hunks)
  • src/zio.zig (0 hunks)
💤 Files with no reviewable changes (4)
  • src/sync/Condition.zig
  • src/sync/ResetEvent.zig
  • src/zio.zig
  • src/coroutines.zig
🧰 Additional context used
🧠 Learnings (3)
📚 Learning: 2025-11-04T10:29:56.949Z
Learnt from: lalinsky
Repo: lalinsky/zio PR: 118
File: src/core/task.zig:33-115
Timestamp: 2025-11-04T10:29:56.949Z
Learning: In zio's task implementation (src/core/task.zig), the Closure struct intentionally caps result_len and context_len at 4096 bytes (using u12 fields) and alignment at 16 bytes (using u4 padding fields). This is a deliberate design decision to avoid bloating task structures. Users needing larger result or context storage should heap-allocate and pass pointers instead. These limits are more generous than std lib's fixed storage (64 bytes for results, 1024 bytes for args).
<!-- [add_learning]
The task storage limits in zio (4 KiB for result/context, 16-byte alignment) are intentional design constraints to prevent task bloat, not correctness issues. The maintainer's position is that larger data should be heap-allocated.

Applied to files:

  • src/stack_pool.zig
  • src/core/task.zig
  • src/runtime.zig
📚 Learning: 2025-11-06T07:27:07.947Z
Learnt from: lalinsky
Repo: lalinsky/zio PR: 128
File: src/event/tests.zig:723-732
Timestamp: 2025-11-06T07:27:07.947Z
Learning: In Zig 0.15+, the default ArrayList uses an unmanaged API. Initialize with `.empty` and pass the allocator to methods that need allocation (append, appendSlice, deinit, etc.). The old managed API that stored the allocator internally is deprecated.

Applied to files:

  • src/stack_pool.zig
📚 Learning: 2025-10-10T05:59:17.087Z
Learnt from: lalinsky
Repo: lalinsky/zio PR: 41
File: src/udp.zig:79-82
Timestamp: 2025-10-10T05:59:17.087Z
Learning: In UDP socket operations (src/udp.zig), error.Canceled from result_data.result represents OS/IO-level cancellation (e.g., io_uring), not user-requested cancellation. User cancellation is handled by waitForXevCompletion() via try. OS-level error.Canceled should be mapped to error.Unexpected as it represents an unexpected system event.

Applied to files:

  • src/runtime.zig
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
  • GitHub Check: test (ubuntu-latest, x86_64-linux)
  • GitHub Check: test (ubuntu-latest, riscv64-linux, qemu-riscv64-static, epoll)
  • GitHub Check: test (windows-latest, native)
  • GitHub Check: test (ubuntu-latest, aarch64-linux, qemu-aarch64-static, epoll)
🔇 Additional comments (7)
build.zig (1)

22-34: LGTM - clean dependency wiring.

The coro dependency follows the same pattern as libxev (target/optimize options passed through, then wired into the zio module). Build configuration looks correct.

src/stack_pool.zig (1)

7-10: LGTM - straightforward module swap.

The import path change from coroutines to coro is clean, and all the stack-related symbols remain the same.

src/core/task.zig (2)

10-11: LGTM - clean import refactor.

Importing Coroutine and DEFAULT_STACK_SIZE directly from the coro module removes the namespace indirection.


326-326: LGTM - cleaner constant usage.

Using DEFAULT_STACK_SIZE directly (imported at line 11) instead of the qualified coroutines.DEFAULT_STACK_SIZE is more concise.

src/runtime.zig (2)

15-16: LGTM - consistent with the module migration.

Both Coroutine and Context are now imported directly from the coro module, removing the dependency on the old coroutines module.


332-332: Fix unresolved coroutines references in stack_pool.zig before merging.

The removal of src/coroutines.zig leaves dangling references in the codebase. The file src/stack_pool.zig still imports from the old module on lines 8-10:

const Stack = coroutines.Stack;
const StackPtr = coroutines.StackPtr;
const stack_alignment = coroutines.stack_alignment;

These will fail once the coroutines module is removed. You need to update src/stack_pool.zig to get these types and constants from their new location (likely where Context moved to in your refactor).

⛔ Skipped due to learnings
Learnt from: lalinsky
Repo: lalinsky/zio PR: 48
File: src/core/ConcurrentAwaitableList.zig:254-269
Timestamp: 2025-10-16T14:33:19.718Z
Learning: In the zio repository's `src/core/ConcurrentAwaitableList.zig` and `src/core/Awaitable.zig`, the `in_list` field should remain debug-only (conditional compilation with `builtin.mode == .Debug`). Double-removal detection in `ConcurrentAwaitableList.remove()` uses pointer-based checks (comparing `prev`/`next` against `head`/`tail`) rather than relying on a regular `in_list` field.
Learnt from: lalinsky
Repo: lalinsky/zio PR: 118
File: src/core/task.zig:33-115
Timestamp: 2025-11-04T10:29:56.949Z
Learning: In zio's task implementation (src/core/task.zig), the Closure struct intentionally caps result_len and context_len at 4096 bytes (using u12 fields) and alignment at 16 bytes (using u4 padding fields). This is a deliberate design decision to avoid bloating task structures. Users needing larger result or context storage should heap-allocate and pass pointers instead. These limits are more generous than std lib's fixed storage (64 bytes for results, 1024 bytes for args).
<!-- [add_learning]
The task storage limits in zio (4 KiB for result/context, 16-byte alignment) are intentional design constraints to prevent task bloat, not correctness issues. The maintainer's position is that larger data should be heap-allocated.
build.zig.zon (1)

17-20: All required symbols are properly exported from the coro package.

The verification confirms that the dependency configuration is correct. All six required symbols (Coroutine, Context, Stack, StackPtr, stack_alignment, DEFAULT_STACK_SIZE) are found and exported as public constants in the coro module at the pinned commit. The hash verification provides additional assurance against unintended changes.


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 4ddd296 into main Nov 12, 2025
@lalinsky
lalinsky deleted the external-coro-zig branch November 12, 2025 23:23
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