From f64c89f18d3a0cd15ea334d43f73f72e7bd99140 Mon Sep 17 00:00:00 2001 From: Luke Gruber Date: Fri, 23 May 2025 11:12:14 -0400 Subject: [PATCH] Fix 'require' from a ractor when the required file raises an error If you catch an error that was raised from a file you required in a ractor, that error did not have its belonging reset from the main ractor to the current ractor, so you hit assertion errors in debug mode. --- ractor.c | 3 +++ test/ruby/test_ractor.rb | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/ractor.c b/ractor.c index 101dc41562..e8d2f7a69e 100644 --- a/ractor.c +++ b/ractor.c @@ -4217,9 +4217,12 @@ rb_ractor_require(VALUE feature) rb_ractor_channel_close(ec, crr.ch); if (crr.exception != Qundef) { + ractor_reset_belonging(crr.exception); rb_exc_raise(crr.exception); } else { + RUBY_ASSERT(crr.result != Qundef); + ractor_reset_belonging(crr.result); return crr.result; } } diff --git a/test/ruby/test_ractor.rb b/test/ruby/test_ractor.rb index e61c6beffc..abfbc18218 100644 --- a/test/ruby/test_ractor.rb +++ b/test/ruby/test_ractor.rb @@ -79,6 +79,25 @@ class TestRactor < Test::Unit::TestCase end; end + def test_require_raises_and_no_ractor_belonging_issue + assert_ractor(<<~'RUBY') + require "tempfile" + f = Tempfile.new(["file_to_require_from_ractor", ".rb"]) + f.write("raise 'uh oh'") + f.flush + err_msg = Ractor.new(f.path) do |path| + begin + require path + rescue RuntimeError => e + e.message # had confirm belonging issue here + else + nil + end + end.take + assert_equal "uh oh", err_msg + RUBY + end + def assert_make_shareable(obj) refute Ractor.shareable?(obj), "object was already shareable" Ractor.make_shareable(obj)