Repository navigation
Fix selector teardown leak from libev size-zero allocator calls - #336
Merged
ioquatix merged 1 commit intoOct 9, 2026
Merged
Conversation
ioquatix
approved these changes
Oct 9, 2026
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.
This is AI-assisted. Model: gpt-6.1-sol
Summary
Fixes #335.
libev's allocator contract treats
realloc(ptr, 0)as "free this and return NULL", and libev discards the return value of those size-zero calls during teardown. nio4r currently passes Ruby'sxreallocdirectly toev_set_allocator, but it does not implement that contract: bothxrealloc(NULL, 0)andxrealloc(ptr, 0)return a new one-byte allocation (allocating the replacement before freeing the original pointer when non-NULL). libev throws that pointer away, so every selector teardown leaks a little memory.This PR adds a small adapter in
ext/nio4r/nio4r_ext.c:size == 0:xfree(pointer)and returnNULL, matching libev's expectation.xrealloc(pointer, size), so allocations still go through Ruby's allocator and GC malloc accounting.The vendored libev sources are unchanged.
Reproduction: https://github.com/nateberkopec/nio4r-leak
Validation
Linux aarch64, Ruby 3.3.10, Valgrind 3.19, using the existing repro:
macOS, Ruby 4.0.7: native extension builds. 1000 cycles each of register IO / select / deregister / double-close passed for
:poll,:kqueueand:select.Apart from this change, the
extandlibgem trees match upstream HEAD exactly.Full upstream RSpec suite, against this PR's native extension with RSpec 3.13 (within the declared
~> 3.7requirement):No new tests were added.