Skip to content

ev: add do_not_call_callbacks loop option for embedders - #593

Merged
lalinsky merged 1 commit into
mainfrom
feat/do-not-call-callbacks
Jul 19, 2026
Merged

lalinsky merged 1 commit into
mainfrom
feat/do-not-call-callbacks

Conversation

@lalinsky

Copy link
Copy Markdown
Owner

What

Adds a Loop option do_not_call_callbacks. When set, tick() does the backend poll and all bookkeeping but does not dispatch finished standalone completions to their callbacks — it queues them on a new state.dispatched queue, which the driver drains itself via a new Loop.nextDispatched().

Why

The motivating consumer is a CPython asyncio event loop built on ev. Its callbacks call into CPython and must hold the GIL, but the loop wants to release the GIL around the blocking poll so other Python threads (thread-pool executors, cross-thread wakeups) can run. With this option the driver can:

  1. release the GIL,
  2. tick() — now Python-free (no callbacks fire),
  3. reacquire the GIL,
  4. drain nextDispatched() and call the callbacks itself.

This is simpler and lower-overhead than acquiring the GIL per callback from inside tick() (one release/acquire per loop iteration instead of per callback).

Scope

  • Confined to finishCompletion: a standalone (non-rearm, no group owner) completion is pushed to state.dispatched instead of called. Rearm handles and group-owner callbacks are unaffected and still run inline — their machinery must stay synchronous.
  • Default false, so existing behavior is unchanged.

Tests

zig build test — 542/542 pass.

Add a Loop option that makes tick() collect finished standalone
completions into a queue (drained via Loop.nextDispatched) instead of
invoking their callbacks. This lets a driver run the blocking poll and
then invoke callbacks itself at a controlled point.

The motivating case is a CPython asyncio event loop built on ev: it can
release the GIL around the blocking tick() (which now touches no user
callbacks) and reacquire it before draining nextDispatched and calling
the callbacks, instead of having callbacks fire from inside tick() where
the GIL state is fixed. This is simpler and lower-overhead than
acquiring the GIL per callback.

The change is confined to finishCompletion: for a standalone (non-rearm,
no group owner) completion, push it onto a new state.dispatched queue
rather than calling it. Rearm handles and group-owner callbacks are
unaffected and still run inline. Default is false, so existing behavior
is unchanged.
@coderabbitai

coderabbitai Bot commented Jul 19, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: dcab0c06-d76f-42b5-ba32-823d4952315b

📥 Commits

Reviewing files that changed from the base of the PR and between ec7360f and ab78be5.

📒 Files selected for processing (1)
  • src/ev/loop.zig

📝 Walkthrough

Walkthrough

Loop can suppress standalone completion callbacks, queue those completions, and expose them through nextDispatched. Rearmed completions and group/owner callbacks continue to dispatch inline.

Changes

Deferred completion dispatch

Layer / File(s) Summary
Loop callback configuration
src/ev/loop.zig
Adds do_not_call_callbacks to Loop.Options and Loop, then copies the option during initialization.
Completion queue and retrieval
src/ev/loop.zig
Queues eligible standalone completions during finalization and adds nextDispatched to retrieve them.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: notxorand

Poem

Callbacks wait in a tidy queue,
Until the embedder calls them through.
Rearms still run without delay,
Owners keep their usual way.
One small pop, and off they play.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed It clearly summarizes the main change: a new Loop option that changes callback dispatch behavior for embedders.
Description check ✅ Passed It matches the implemented option, queued dispatch flow, and embedder use case.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/do-not-call-callbacks

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.

@lalinsky
lalinsky merged commit c412046 into main Jul 19, 2026
27 checks passed
@lalinsky
lalinsky deleted the feat/do-not-call-callbacks branch July 19, 2026 09:53
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