Skip to content

[event-loop] Add epoll backend - #131

Merged
lalinsky merged 7 commits into
event-loopfrom
event-loop-epoll
Nov 6, 2025
Merged

lalinsky merged 7 commits into
event-loopfrom
event-loop-epoll

Conversation

@lalinsky

@lalinsky lalinsky commented Nov 6, 2025

Copy link
Copy Markdown
Owner

No description provided.

@coderabbitai

coderabbitai Bot commented Nov 6, 2025 •

Copy link
Copy Markdown
Contributor

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

Walkthrough

The PR adds a Linux-specific epoll backend and refactors the existing poll backend. Backend selection now routes Linux to epoll, with a new module implementing full asynchronous I/O orchestration. The poll backend receives event mask calculation optimizations and improved poll queue cleanup.

Changes

Cohort / File(s) Change Summary
Backend Selection
src/event/backend.zig
Added explicit Linux case to Backend switch that imports the dedicated epoll backend instead of falling back to the default poll path.
Epoll Backend Implementation
src/event/backends/epoll.zig
New module introducing NetHandle alias, error/shutdown types, and complete epoll-based event loop with lifecycle (init/deinit), submission processing, completion routing, epoll event translation, poll queue management, and synchronous/asynchronous operation handling.
Poll Backend Optimization
src/event/backends/poll.zig
Refactored getEvents return type, introduced computed event mask reuse, updated poll entry initialization and merge logic, improved removeFromPollQueue with conditional cleanup and event recalculation, and adjusted idle detection to check poll_queue count.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

  • epoll.zig: Substantial new module with intricate async I/O state management, completion queueing per fd, and event dispatch logic. Verify epoll event handling, would-block scenarios, error translation, and requeue mechanics.
  • poll.zig: Event mask refactoring changes multiple code paths. Confirm event aggregation logic in removeFromPollQueue is correct when handling remaining completions, and poll_queue count check properly replaces the prior idleness condition.
  • backend.zig: Straightforward, but confirm OS detection routing doesn't break existing platforms.

Poem

🐧 Epoll lands on Linux, fast and lean,
Poll gets its act together, events foreseen,
Async loops tick without the wait,
Socket completions at a better rate,
The backend routes it all just right! 🚀

Pre-merge checks and finishing touches

❌ Failed checks (1 warning, 1 inconclusive)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
Description check ❓ Inconclusive No pull request description was provided by the author, making it impossible to assess relevance or detail. Add a description explaining why epoll is being introduced, what problems it solves, and any performance or compatibility notes relevant to the change.
✅ Passed checks (1 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Add epoll backend' directly reflects the main change: introducing a new epoll-based event loop backend, which aligns with the substantial additions to src/event/backends/epoll.zig and the routing change in src/event/backend.zig.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch event-loop-epoll

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 commented Nov 6, 2025

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 6, 2025

Copy link
Copy Markdown
Contributor
✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@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

📜 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 ed8ee7a and 83f61fb.

📒 Files selected for processing (3)
  • src/event/backend.zig (1 hunks)
  • src/event/backends/epoll.zig (1 hunks)
  • src/event/backends/poll.zig (4 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 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/event/backends/epoll.zig
🔇 Additional comments (1)
src/event/backends/poll.zig (1)

97-114: Caching the op mask here is a good call.

Precomputing op_events keeps the pollfd updates tidy and avoids repeating the switch every time we touch the same completion.

Comment thread src/event/backends/epoll.zig Resolved
github-actions Bot and others added 2 commits November 6, 2025 10:07
Check return values from epoll_ctl(CTL_DEL) and epoll_ctl(CTL_MOD)
to prevent internal state from diverging from kernel state.

- CTL_DEL: Only remove from poll_queue if epoll_ctl succeeds
- CTL_MOD: Only update entry.events if epoll_ctl succeeds
- Log errors when epoll_ctl fails

Co-authored-by: Lukáš Lalinský <[email protected]>
Instead of logging errors and returning early, propagate epoll_ctl failures
up the call stack so they can be handled properly. This ensures internal
bookkeeping stays in sync with the kernel's epoll state.

Co-authored-by: Lukáš Lalinský <[email protected]>
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