Fix memory leak by ensuring rb_call_super is called in Watcher_Detach#91
Merged
ioquatix merged 5 commits intosocketry:mainfrom Mar 20, 2026
Merged
Fix memory leak by ensuring rb_call_super is called in Watcher_Detach#91ioquatix merged 5 commits intosocketry:mainfrom
ioquatix merged 5 commits intosocketry:mainfrom
Conversation
Instead of manually calling `ev_stop` within the C macro, delegate the disablement process to the existing Ruby-level `disable` method via `rb_funcall`. This refactoring ensures that when a watcher is detached while still enabled, all internal states are safely and correctly updated. Specifically, it guarantees that the `@active_watchers` counter is decremented (preventing potential event loop hangs) and the `enabled` flag is properly set to 0.
326102e to
7a45665
Compare
On macOS (kqueue), calling `ev_stop` multiple times on the same watcher or after it has been partially decommissioned can trigger a Segmentation Fault. By checking `watcher_data->enabled` before invoking the C-level stop function, we prevent these race conditions and reentrancy issues.
Collaborator
Author
|
@ioquatix Could you please review this? Thanks. |
ioquatix
reviewed
Mar 20, 2026
| @@ -26,20 +26,15 @@ | |||
|
|
|||
| #define Watcher_Detach(watcher_type, watcher) \ | |||
Member
There was a problem hiding this comment.
Do you mind making a follow up PR to to make this an inline function? These macros are getting a bit out of control.
Collaborator
Author
There was a problem hiding this comment.
I completely agree about the macros—they made debugging quite challenging this time!
ioquatix
approved these changes
Mar 20, 2026
Member
|
Do you want me to make a patch release? |
Collaborator
Author
Member
|
Fixed in v1.9.4 |
Collaborator
Author
|
Thanks |
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.
Fix #89
Overview
This PR fixes a memory leak where detached watchers are not garbage collected because they remain in the
@watchersarray.Root Cause
In the C extension (watcher.h), the Watcher_Detach and Watcher_Disable macros were not properly calling rb_call_super(0, 0) in all code paths.
Because super was bypassed, the Ruby-level cleanup logic (which removes the watcher from the loop's @watchers array) was never executed, causing the memory leak.
Important Note on Thread Safety and detach_race_condition_spec.rb
During the investigation of this fix,
we conducted a 10,000-iteration stress test on detach_race_condition_spec.rb and deeply analyzed the intermittent SEGV on macOS occurring inside ev_io_stop sometimes.
The SEGV is an architectural limitation, not a memory management bug in this patch. cool.io releases the Ruby GVL (via rb_nogvl) to run the libev event loop (ev_run). However, libev is strictly not thread-safe. When a separate Ruby thread calls detach (and subsequently ev_io_stop) while the event loop thread is concurrently modifying its internal arrays (e.g., anfds), it causes memory corruption inside libev.
Fixing this specific thread-safety limitation would require a major architectural rewrite (e.g., using ev_async for cross-thread message passing instead of calling ev_io_stop directly from another thread). Therefore, this PR strictly focuses on resolving the memory leak (#89), which is fully fixed by these changes.
Types of Changes
Contribution