Use Ruby's x* allocators uniformly for internal selector allocations#176
Merged
Conversation
The selector internals (queue entries in `IO_Event_Selector_ready_push`, and the backing array + per-element slots in `IO_Event_Array`) were using raw `malloc` / `calloc` / `realloc` / `free`. Two were unchecked-with-`assert(...)`, which in release builds (`NDEBUG`) silently compiled to a NULL pointer dereference on out-of-memory. The rest checked `NULL` and propagated `-1` through `IO_Event_Array_initialize` / `_resize` / `_lookup` to their callers in `epoll.c` / `kqueue.c` / `uring.c`. Switch the lot to Ruby's `xmalloc` / `xcalloc` / `xrealloc2` / `xfree`: - Each allocator triggers a GC sweep on memory pressure before failing, increasing the chance of success. - Failures raise `NoMemoryError` (or `RangeError` for the array-size-exceeds-maximum branch) instead of returning `NULL`, so the `assert` is no longer needed and the `-1` returns become dead. - Ruby's allocation accounting stays in sync with the actual heap state. With the `-1` paths gone, `IO_Event_Array_initialize` and `IO_Event_Array_resize` now return `void`, `IO_Event_Array_lookup` is guaranteed to return a non-NULL pointer, and the corresponding `if (result < 0) rb_sys_fail(...)` / `if (!descriptor) rb_sys_fail(...)` checks in each selector are removed. Audit of GC/state-handling risk: each allocator call site has the invariant "allocate → in-place initialise (no allocation) → publish into a GC-traceable structure", so no half-initialised state is ever visible to a GC sweep, no locks are held across the allocation, and no `rb_ensure` is required. Supersedes #175. Co-authored-by: Cursor <cursoragent@cursor.com>
b91adb8 to
6fd1bff
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The selector internals (queue entries in
IO_Event_Selector_ready_push, and the backing array + per-element slots inIO_Event_Array) were using rawmalloc/calloc/realloc/free. Two of those were unchecked-with-assert(...), which in release builds (NDEBUG) silently compiles to aNULLpointer dereference on out-of-memory. The rest checkedNULLand propagated-1throughIO_Event_Array_initialize/_resize/_lookupto their callers inepoll.c/kqueue.c/uring.c.This PR switches the lot to Ruby's
xmalloc/xcalloc/xrealloc2/xfree:NoMemoryError(orRangeErrorfor the array-size-exceeds-maximum branch) instead of returningNULL, so theassertis no longer needed and the-1returns become dead.With the
-1paths gone:IO_Event_Array_initializeandIO_Event_Array_resizenow returnvoid.IO_Event_Array_lookupis guaranteed to return a non-NULLpointer.if (result < 0) rb_sys_fail(...)/if (!descriptor) rb_sys_fail(...)checks in each selector are removed.GC / state-handling audit
Each allocator call site has the invariant:
At step 1 the new object doesn't exist yet, so there's nothing to roll back. Between 1 and 3 no allocation can happen, so GC can't see half-initialised state. No mutex is ever held across an allocation. No call site requires
rb_ensure.Files changed
ext/io/event/array.h—calloc/realloc/malloc/free→xcalloc/xrealloc2/xmalloc/xfree._initializeand_resizelose theirintreturn.ext/io/event/selector/selector.c—IO_Event_Selector_ready_pushallocation pair →xmalloc/xfree.ext/io/event/selector/epoll.c,kqueue.c,uring.c— drop dead< 0/ NULL checks at the call sites of_initializeand_lookup.releases.md— bullet under## Unreleased.Diff is net −35 LOC.
Relationship to #175
Supersedes #175 (which only fixed half of the
selector.callocation pair and didn't toucharray.h). Close #175 once this lands.Verification
make -C extbuilds clean (modulo a pre-existing warning fromruby/internal/core/rstring.h).