Skip to content

Fix stale GVL comments in Coolio_Loop_process_event - #106

Merged
Watson1978 merged 1 commit into
socketry:mainfrom
Watson1978:fix-stale-gvl-comment
Jul 31, 2026
Merged

Fix stale GVL comments in Coolio_Loop_process_event#106
Watson1978 merged 1 commit into
socketry:mainfrom
Watson1978:fix-stale-gvl-comment

Conversation

@Watson1978

Copy link
Copy Markdown
Collaborator

The comments in Coolio_Loop_process_event() say the Global VM Lock is not held, and build a whole explanation on top of that — the event is stashed rather than dispatched because "we can't rb_funcall() anything" until the GVL comes back.

That stopped being true. cool.io patches ev_run() to release the GVL only around the blocking backend call:

  • ext/libev/ev.c:3777rb_thread_call_without_gvl((void *)ev_backend_poll, ...), inside the COOLIO PATCHERY HO! block, wraps the poll and nothing else.
  • ext/libev/ev.c:3818EV_INVOKE_PENDING runs after that call has returned, i.e. with the GVL reacquired.

So this function runs with the GVL held. The code was never unsafe — the xrealloc() of the event buffer and the watcher lookup both happen under the GVL — but a reader who believes the comment would conclude the opposite, and would be wary of touching anything here.

This replaces the wrong rationale with what the code actually does, rather than guessing at a new one: events are stashed and dispatched by Coolio_Loop_dispatch_events() once ev_loop() returns, and a watcher detached during that dispatch nils its own stashed entries, which the dispatch walk skips (ext/cool.io/watcher.c, ext/cool.io/loop.c). The parts that were already correct — the O(1) explanation, the note about this being a libev callback abstraction — are kept as they were, voice included.

Comments only. rspec — 67 examples, 0 failures.

🤖 Generated with Claude Code

The comments here say the Global VM Lock is not held, and build a whole
explanation on top of that: the event is stashed rather than dispatched
because "we can't rb_funcall() anything" until the GVL comes back.

That stopped being true. cool.io patches ev_run() to release the GVL only
around the blocking backend call (ev.c, rb_thread_call_without_gvl on
ev_backend_poll); libev invokes the events it collected after that call has
returned, so this function runs with the GVL held.

The code was never unsafe -- xrealloc() of the event buffer and the watcher
lookup both happen under the GVL -- but a reader who believes the comment
would conclude the opposite, and would be wary of touching anything here.

Describe what the code actually does instead: events are stashed and
dispatched by Coolio_Loop_dispatch_events() once ev_loop() returns, and a
watcher detached during that dispatch nils its own stashed entries, which the
dispatch walk skips.

Comments only, no behaviour change.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@Watson1978
Watson1978 merged commit f41520f into socketry:main Jul 31, 2026
14 checks passed
@Watson1978
Watson1978 deleted the fix-stale-gvl-comment branch July 31, 2026 09:18
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