From: "ivoanjo (Ivo Anjo) via ruby-core" Date: 2026-12-04T16:42:43+00:00 Subject: [ruby-core:127860] [Ruby Bug#22724] A GC event hook fires for another Ractor's local GCs only if that Ractor enabled some unrelated internal-event hook Issue #22724 has been updated by ivoanjo (Ivo Anjo). > Either GC event hooks always fire for local GCs of all Ractors, or never for other Ractors' local GCs. If the fix ends up being "hooks are always local", it would be kinda nice to have an alternative to ask to get events on all ractors, to enable, e.g. a profiler to still have a global view of all GCs (without having to create a new tracepoint per ractor). ---------------------------------------- Bug #22724: A GC event hook fires for another Ractor's local GCs only if that Ractor enabled some unrelated internal-event hook https://bugs.ruby-lang.org/issues/22724#change-120455 * Author: Eregon (Benoit Daloze) * Status: Open * Assignee: ractor * ruby -v: ruby 4.1.0dev (2026-12-04T14:46:31Z master 8395770107) +PRISM [arm64-darwin25] * Backport: 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN ---------------------------------------- Since Ractor-local GC, a hook on `RUBY_INTERNAL_EVENT_GC_ENTER` (and `GC_EXIT`, `GC_START`, `GC_END_MARK`, `GC_END_SWEEP`) normally only fires for collections of the objspace of the Ractor which enabled it. Local GCs in other Ractors are not reported. However, as soon as another Ractor enables *any* internal-event hook, even an unrelated one, the first hook starts firing for every local GC of that other Ractor, running on that Ractor's thread. Reproducer, a small C extension (`gc_hook_leak.c`): ```c #include #include #include #include static pthread_t main_thread; static atomic_long on_main, on_other; static void count_gc_enter(VALUE tpval, void *data) { atomic_fetch_add(pthread_equal(pthread_self(), main_thread) ? &on_main : &on_other, 1); } static void noop(VALUE tpval, void *data) { } static VALUE enable(VALUE events, void (*func)(VALUE, void *)) { VALUE tp = rb_tracepoint_new(0, NUM2UINT(events), func, NULL); rb_gc_register_mark_object(tp); rb_tracepoint_enable(tp); return Qnil; } /* GC_ENTER hook that counts on which native thread it fired */ static VALUE enable_gc_enter_hook(VALUE self) { return enable(UINT2NUM(RUBY_INTERNAL_EVENT_GC_ENTER), count_gc_enter); } /* An unrelated internal-event hook which does nothing */ static VALUE enable_gc_end_mark_hook(VALUE self) { return enable(UINT2NUM(RUBY_INTERNAL_EVENT_GC_END_MARK), noop); } static VALUE counts(VALUE self) { return rb_ary_new_from_args(2, LONG2NUM(on_main), LONG2NUM(on_other)); } void Init_gc_hook_leak(void) { rb_ext_ractor_safe(true); main_thread = pthread_self(); rb_define_global_function("enable_gc_enter_hook", enable_gc_enter_hook, 0); rb_define_global_function("enable_gc_end_mark_hook", enable_gc_end_mark_hook, 0); rb_define_global_function("gc_enter_counts", counts, 0); } ``` and `repro.rb`: ```ruby require_relative "gc_hook_leak" enable_gc_enter_hook # in the main Ractor only r = Ractor.new(ARGV[0] == "other_hook") do |other_hook| enable_gc_end_mark_hook if other_hook # unrelated hook in this Ractor before = GC.count 200_000.times { "x" * 100 } GC.count - before end gcs_in_ractor = r.value on_main, on_other = gc_enter_counts puts "GCs in the Ractor: #{gcs_in_ractor}, GC_ENTER events on the main thread: #{on_main}, on the Ractor thread: #{on_other}" ``` `extconf.rb`: ```ruby require "mkmf" create_makefile("gc_hook_leak") ``` ``` $ ruby extconf.rb && make $ ruby -v -W0 repro.rb ruby 4.1.0dev (2026-12-04T14:46:31Z master 8395770107) +PRISM [arm64-darwin25] GCs in the Ractor: 392, GC_ENTER events on the main thread: 0, on the Ractor thread: 0 $ ruby -v -W0 repro.rb other_hook ruby 4.1.0dev (2026-12-04T14:46:31Z master 8395770107) +PRISM [arm64-darwin25] GCs in the Ractor: 392, GC_ENTER events on the main thread: 0, on the Ractor thread: 393 ``` The only difference between the two runs is that the Ractor enabled a no-op `GC_END_MARK` hook. Yet the main Ractor's `GC_ENTER` hook goes from receiving no events from that Ractor to receiving one per GC. ## Cause Internal objspace event hooks are kept in the VM-wide `vm->global_hooks` list (`connect_non_targeted_event_hook()` in `vm_trace.c`). Whether a GC event actually fires is gated per objspace, by `objspace->hook_events` (`gc_event_hook()` in `gc/default/default.c`). `update_global_event_hook()` in `vm_trace.c` calls `rb_objspace_set_event_hook(new_events_global)`, where `new_events_global` is the union of the internal events enabled anywhere in the VM, not just the events of the hook being enabled. `rb_objspace_set_event_hook()` then stores that into the *current Ractor's* objspace only (`rb_gc_impl_set_event_hook(rb_gc_get_objspace(), e)` in `gc.c`). So: * The Ractor which enables a GC hook gets the GC events in its objspace's `hook_events`, other Ractors do not, which is why the hook doesn't see other Ractors' local GCs by default. * When another Ractor enables any internal-event hook, its objspace gets all globally-enabled internal events, including `GC_ENTER` from the first hook. Its local GCs then dispatch to the VM-wide hook list, so the first hook fires there. ## Expected behavior Whether a GC event hook sees the local GCs of another Ractor should not depend on which unrelated hooks that Ractor enabled. Either GC event hooks always fire for local GCs of all Ractors, or never for other Ractors' local GCs. Note that when it does fire for another Ractor, the C hook (registered from the main Ractor) runs on that Ractor's thread, concurrently with the main Ractor, which the C extension might not expect. -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/