Skip to content

Commit f64c89f

Browse files
luke-gruberbyroot
authored andcommitted
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.
1 parent b7e7511 commit f64c89f

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

ractor.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4217,9 +4217,12 @@ rb_ractor_require(VALUE feature)
42174217
rb_ractor_channel_close(ec, crr.ch);
42184218

42194219
if (crr.exception != Qundef) {
4220+
ractor_reset_belonging(crr.exception);
42204221
rb_exc_raise(crr.exception);
42214222
}
42224223
else {
4224+
RUBY_ASSERT(crr.result != Qundef);
4225+
ractor_reset_belonging(crr.result);
42234226
return crr.result;
42244227
}
42254228
}

test/ruby/test_ractor.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,25 @@ def test_default_thread_group
7979
end;
8080
end
8181

82+
def test_require_raises_and_no_ractor_belonging_issue
83+
assert_ractor(<<~'RUBY')
84+
require "tempfile"
85+
f = Tempfile.new(["file_to_require_from_ractor", ".rb"])
86+
f.write("raise 'uh oh'")
87+
f.flush
88+
err_msg = Ractor.new(f.path) do |path|
89+
begin
90+
require path
91+
rescue RuntimeError => e
92+
e.message # had confirm belonging issue here
93+
else
94+
nil
95+
end
96+
end.take
97+
assert_equal "uh oh", err_msg
98+
RUBY
99+
end
100+
82101
def assert_make_shareable(obj)
83102
refute Ractor.shareable?(obj), "object was already shareable"
84103
Ractor.make_shareable(obj)

0 commit comments

Comments
 (0)