Issue #22622 has been reported by jamescook83 (James Cook). ---------------------------------------- Bug #22622: Crash when autoload constants are queried in Ractors (autoload?, defined?, const_defined?) while the main Ractor requires files https://bugs.ruby-lang.org/issues/22622 * Author: jamescook83 (James Cook) * Status: Open * ruby -v: ruby 4.1.0dev (2026-11-21T12:46:06Z master f2d62c677e) +PRISM [arm64-darwin25] * Backport: 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN ---------------------------------------- ## Environment - ruby 4.0.10 (2026-11-19 revision a704b7cd95) +PRISM [arm64-darwin25] - ruby 4.1.0dev (2026-11-21T12:46:06Z master f2d62c677e) +PRISM [arm64-darwin25] ## How this came up This came up in a test suite that runs each test in its own Ractor, using an autoload-based loader (Zeitwerk). A few tests run on the main Ractor and `require` files while the others run. Code in the Ractor tests checks constants with `defined?` or `const_defined?` without loading them, and some of those constants are autoloads not yet triggered. The process crashes now and then. The script below is that pattern reduced. ## Steps to reproduce Save the script below as `repro.rb` and run `ruby repro.rb`. The script registers 50 autoloads that are never triggered. Eight Ractors each loop over `Holder.constants(false)` calling `Holder.autoload?(c, false)`. At the same time, the main Ractor writes and requires 400 empty files. ```ruby require "tmpdir" Warning[:experimental] = false ractors = (ARGV[0] || 8).to_i rounds = (ARGV[1] || 300).to_i requires = (ARGV[2] || 400).to_i dir = Dir.mktmpdir module Holder; end 50.times { |i| Holder.autoload(:"Thing#{i}", File.join(dir, "thing_#{i}.rb")) } workers = ractors.times.map do Ractor.new(rounds) do |n| hits = 0 n.times do Holder.constants(false).each { |c| hits += 1 if Holder.autoload?(c, false) } Array.new(50) { |k| [k, k.to_s] } end hits end end requires.times do |i| path = File.join(dir, "extra_#{i}.rb") File.write(path, "") require path end p workers.map(&:value).sum ``` ## Expected The script prints a number and exits. ## Actual Ten runs of the script on each version, each run given 60 seconds. "Crash" is a `[BUG]` report, "error exit" a non-zero exit with no `[BUG]` line, and "hang" a run still going after 60 seconds. | | success | crash | error exit | hang | |---|---|---|---|---| | 4.0.10 | 0 | 10 | 0 | 0 | | 4.1.0dev | 0 | 9 | 0 | 1 | The first lines on 4.0.10: ``` repro.rb:16: [BUG] Aborted at 0x00000001890b25e8 ruby 4.0.10 (2026-11-19 revision a704b7cd95) +PRISM [arm64-darwin25] ... -- Control frame information ----------------------------------------------- c:0007 p:---- s:0030 e:000029 l:y b:0001 CFUNC :autoload? ``` On 4.1.0dev: ``` repro.rb:16: [BUG] Segmentation fault at 0x0000000000000004 ruby 4.1.0dev (2026-11-21T12:46:06Z master f2d62c677e) +PRISM [arm64-darwin25] ... -- Control frame information ----------------------------------------------- c:0007 p:---- s:0030 e:000029 l:y b:0001 CFUNC :autoload? ``` The C level backtrace in Ruby's own output was lost to repeated "ABRT received in ABRT handler" lines. macOS saved crash reports for the 4.1.0dev runs but none for the 4.0.10 runs. Two 4.1.0dev reports, crashed thread, innermost frames first, below the signal handler: Abort: ``` ___BUG_IN_CLIENT_OF_LIBMALLOC_POINTER_BEING_FREED_WAS_NOT_ALLOCATED ruby_xfree_sized loaded_features_index_clear_i st_general_foreach rb_st_foreach rebuild_loaded_features_index rb_ec_ensure rb_feature_p feature_provided check_autoload_required rb_autoload_at_p rb_mod_autoload_p ``` Segmentation fault: ``` st_general_foreach st_general_foreach rb_st_foreach rebuild_loaded_features_index rb_ec_ensure rb_feature_p feature_provided check_autoload_required rb_autoload_at_p rb_mod_autoload_p ``` In a larger program, a test suite running tests in Ractors, what looks like the same situation showed up later in GC instead, as `[BUG] try to mark T_NONE object (obj: out-of-heap, parent: out-of-heap)` followed by a segfault at 0xfffffffffffffff8. ## Variations tried The same script with the call inside the Ractors swapped, five runs each, 60 seconds a run. 4.0.10: | Call in the Ractors | success | crash | error exit | hang | |---|---|---|---|---| | `Holder.autoload?(c, false)` (as above) | 0 | 5 | 0 | 0 | | `defined?(Holder::Thing0)` | 0 | 5 | 0 | 0 | | `Holder.const_defined?(c, false)` | 0 | 5 | 0 | 0 | | `const_defined?` on a module of plain constants (no autoload) | 5 | 0 | 0 | 0 | | `defined?` on a plain constant (no autoload) | 5 | 0 | 0 | 0 | | `Holder.const_source_location(c)` | 5 | 0 | 0 | 0 | | `require "tmpdir"` (already loaded) | 5 | 0 | 0 | 0 | | `Holder.constants(false)` only | 5 | 0 | 0 | 0 | | no Ractors, main Ractor requires only | 5 | 0 | 0 | 0 | 4.1.0dev: | Call in the Ractors | success | crash | error exit | hang | |---|---|---|---|---| | `Holder.autoload?(c, false)` (as above) | 0 | 4 | 1 | 0 | | `defined?(Holder::Thing0)` | 0 | 4 | 0 | 1 | | `Holder.const_defined?(c, false)` | 0 | 5 | 0 | 0 | | `const_defined?` on a module of plain constants (no autoload) | 5 | 0 | 0 | 0 | | `defined?` on a plain constant (no autoload) | 5 | 0 | 0 | 0 | | `Holder.const_source_location(c)` | 5 | 0 | 0 | 0 | | `require "tmpdir"` (already loaded) | 5 | 0 | 0 | 0 | | `Holder.constants(false)` only | 5 | 0 | 0 | 0 | | no Ractors, main Ractor requires only | 5 | 0 | 0 | 0 | With the `require` loop removed and 2000 rounds instead of 300, so that only the Ractors call `autoload?`, both versions had 10 successes in 10 runs. The number of autoloads changes how often it crashes. With a single autoload, 4.0.10 crashed in 5 of 5 runs, while 4.1.0dev had 4 successes and 1 hang in 5. From 50 autoloads up, both crashed in every run or nearly every run. The number of Ractors, rounds and requires can be changed through the arguments, for example `ruby repro.rb 4 300 400`. -- https://bugs.ruby-lang.org/