Project

General

Profile

Actions

Bug #22724

open

A GC event hook fires for another Ractor's local GCs only if that Ractor enabled some unrelated internal-event hook

Bug #22724: A GC event hook fires for another Ractor's local GCs only if that Ractor enabled some unrelated internal-event hook
1

Added by Eregon (Benoit Daloze) 1 day ago. Updated 1 day ago.

Status:
Open
Assignee:
Target version:
-
ruby -v:
ruby 4.1.0dev (2026-12-04T14:46:31Z master 8395770107) +PRISM [arm64-darwin25]
[ruby-core:127859]

Description

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):

#include <ruby.h>
#include <ruby/debug.h>
#include <pthread.h>
#include <stdatomic.h>

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:

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:

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.

Actions

Also available in: PDF Atom