Skip to content

Commit 1d5d5f5

Browse files
etiennebarrieparacycle
authored andcommitted
Show chain of references for unshareable class variables
1 parent a67cfb6 commit 1d5d5f5

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

test/ruby/test_ractor.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,22 @@ def test_error_for_module_instance_variable
310310
RUBY
311311
end
312312

313+
def test_error_for_class_variable
314+
assert_ractor(<<~'RUBY')
315+
module ModuleWithUnshareableClassVariable
316+
def self.unshareable = @@unshareable
317+
@@unshareable = Hash.new {}.freeze
318+
end
319+
e = Ractor.new do |mod|
320+
ModuleWithUnshareableClassVariable.unshareable
321+
rescue
322+
$!
323+
end.value
324+
assert_kind_of Ractor::IsolationError, e
325+
assert_match(/from Hash default proc/, e.detailed_message)
326+
RUBY
327+
end
328+
313329
def test_error_for_module_constant
314330
assert_ractor(<<~'RUBY')
315331
module ModuleWithUnshareableConstant

variable.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,10 +1206,13 @@ CVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(VALUE klass, ID id)
12061206
static void
12071207
cvar_read_ractor_check(VALUE klass, ID id, VALUE val)
12081208
{
1209-
if (UNLIKELY(!rb_ractor_main_p()) && !rb_ractor_shareable_p(val)) {
1210-
rb_raise(rb_eRactorIsolationError,
1211-
"can not read non-shareable class variable %"PRIsVALUE" from non-main Ractors (%"PRIsVALUE")",
1212-
rb_id2str(id), klass);
1209+
if (UNLIKELY(!rb_ractor_main_p())) {
1210+
VALUE chain = Qnil;
1211+
if (!rb_ractor_shareable_p_continue(val, &chain)) {
1212+
rb_raise(rb_eRactorIsolationError,
1213+
"can not read non-shareable class variable %"PRIsVALUE" from non-main Ractors (%"PRIsVALUE")%"PRIsVALUE,
1214+
rb_id2str(id), klass, chain);
1215+
}
12131216
}
12141217
}
12151218

0 commit comments

Comments
 (0)