Project

General

Profile

Actions

Bug #22553

open

Segfault when several Ractors call Kernel#class or #frozen? on objects of many classes

Bug #22553: Segfault when several Ractors call Kernel#class or #frozen? on objects of many classes

Added by jamescook83 (James Cook) 1 day ago.

Status:
Open
Assignee:
-
Target version:
-
ruby -v:
ruby 4.1.0dev (2026-11-15T03:48:53Z master 3be2b028a7) +PRISM [arm64-darwin25]
[ruby-core:127495]

Description

When several Ractors call object.class on objects of many different classes, one of them
sometimes crashes with [BUG] Segmentation fault at 0x0000000000000000. object.frozen? does
the same. object.hash does not.

How often it crashes seems to depend on the number of distinct classes, not on the number of calls.
With 8 Ractors each making 2000 calls:

distinct classes runs crashed
1, 2, 10 or 50 60 0
100 60 1
250 60 2
500 60 7
1000 60 22
2000 60 33

The repro uses 2000 classes because that crashes in about half of the runs. There is no
threshold.

Repro

#   ruby ractor_repro.rb [mode] [classes]
#
# mode:
#   class        object.class
#   frozen?      object.frozen?
#   hash         object.hash
#   own_objects  object.class, on objects each Ractor creates
#   bind         Kernel#class through bind_call
#   warm         object.class, after the main Ractor calls it once per class
#
# classes: number of distinct classes (default 2000). Each Ractor makes 2000 calls either way.
Warning[:experimental] = false

MODE = (ARGV[0] || "class").to_sym
COUNT = Integer(ARGV[1] || 2000)
CLASSES = Ractor.make_shareable(Array.new(COUNT) { Class.new })
OBJECTS = Ractor.make_shareable(CLASSES.map(&:new) * (2000 / COUNT))

def call_class = OBJECTS.each { |object| object.class }
def call_frozen = OBJECTS.each { |object| object.frozen? }
def call_hash = OBJECTS.each { |object| object.hash }
def call_class_on_own_objects = (CLASSES.map(&:new) * (2000 / COUNT)).each { |object| object.class }
def call_class_with_bind_call
  kernel_class = Kernel.instance_method(:class)
  OBJECTS.each { |object| kernel_class.bind_call(object) }
end

OBJECTS.each { |object| object.class } if MODE == :warm

ractors = Array.new(8) do
  Ractor.new do
    case MODE
    when :class, :warm then call_class
    when :frozen? then call_frozen
    when :hash then call_hash
    when :own_objects then call_class_on_own_objects
    when :bind then call_class_with_bind_call
    end
  end
end
ractors.each(&:value)
puts "ok"

A single run may pass, so run it several times. A crashed run may hang (see below), so use a
timeout. The attached ractor_sweep.sh runs every mode and class count 60 times.

Results per mode, 2000 classes:

mode runs crashed
class 60 33
frozen? 60 34
own_objects 60 24
hash 60 0
bind 60 0
warm 60 0

Notes:

  • own_objects crashes, so the objects do not need to be shared between Ractors.
  • warm does not crash, even though the main Ractor calls class from a different line than
    the Ractors do.
  • A variant where each Ractor calls object.class from its own method also crashed (30 of 40
    runs), so the Ractors do not need to share a call site.
  • On this build Kernel#class and Kernel#frozen? are leaf builtins defined in
    <internal:kernel>. Kernel#hash is a C method.
  • The crash rate changes with machine load. In separate batches the class mode crashed in as
    few as 2 of 30 runs and as many as 35 of 40. The tables above come from one run of
    ractor_sweep.sh, which takes the conditions in turn so that they share the same load.

Crash output

ractor_repro.rb:19: [BUG] Segmentation fault at 0x0000000000000000
ruby 4.1.0dev (2026-11-15T03:48:53Z master 3be2b028a7) +PRISM [arm64-darwin25]

-- Control frame information -----------------------------------------------
c:0005 p:0004 s:0018 e:000017 l:n b:---- r:0x0 BLOCK  ractor_repro.rb:19 [FINISH]
c:0004 p:---- s:0014 e:000013 l:y b:0001 r:0x0 CFUNC  :each
c:0003 p:0005 s:0010 e:000009 l:y b:0001 r:0x0 METHOD ractor_repro.rb:19
c:0002 p:0061 s:0006 e:000005 l:n b:---- r:0x0 BLOCK  ractor_repro.rb:33 [FINISH]
c:0001 p:---- s:0003 e:000002 l:y b:---- r:0x0 DUMMY  [FINISH]

-- Ruby level backtrace information ----------------------------------------
ractor_repro.rb:33:in 'block (2 levels) in <main>'
ractor_repro.rb:19:in 'call_class'
ractor_repro.rb:19:in 'each'
ractor_repro.rb:19:in 'block in call_class'

-- Threading information ---------------------------------------------------
Total ractor count: 9
Ruby thread count for this ractor: 1

-- Machine register context ------------------------------------------------
  x0: 0x000000089f14f4d8  x1: 0x0000000134707ee8  x2: 0x000000013480b530
  x3: 0x0000000000000000  x4: 0x00000000000030a0  x5: 0x000000013480b390
  x6: 0x0000000000000000  x7: 0xfffff0003ffff800 x18: 0x0000000000000000
 x19: 0x000000089f14f4d8 x20: 0x0000000134707ee8 x21: 0x000000089f169e80
 x22: 0x0000000100cda080 x23: 0x00000001207a0aa0 x24: 0x00000000ffffffff
 x25: 0x0000000100cda080 x26: 0x000000013480b530 x27: 0x000000089f169e60
 x28: 0x0000000134608048  lr: 0x0000000101a3b384  fp: 0x000000013480b5d0
  sp: 0x000000013480b450

-- C level backtrace information -------------------------------------------
SEGV received in SEGV handler
ABRT received in SEGV handler

The C backtrace is never printed. Sometimes the crash handler crashes again, as above. Other
times the output stops at "C level backtrace information" and the process stays at 100% CPU
until it is killed. Of 32 crashes I let run, 22 hung this way.

Workaround

Calling Kernel#class through bind_call did not crash:

klass = Kernel.instance_method(:class).bind_call(object)

Calling class once on an object of each class from the main Ractor, before starting the other
Ractors, also did not crash. That only works if the classes are known in advance.

Use case

We found this in a project that makes heavy use of Ractors. It runs several jobs at once, one per
Ractor, all running the same code. A helper that reports progress calls item.class, and that
call sees about sixty classes. About one run in fifty hung. After changing the helper to use
bind_call, 1800 runs passed; before, 6 of 300 failed.


Files

ractor_sweep.sh (1.5 KB) ractor_sweep.sh jamescook83 (James Cook), 11/15/2026 11:41 AM
ractor_repro.rb (1.41 KB) ractor_repro.rb jamescook83 (James Cook), 11/15/2026 11:41 AM

No data to display

Actions

Also available in: PDF Atom