Bug #21782
openRactor::IsolationError reports incorrect path for constants found through upwards search
Description
Ractor::IsolationError is raised when non-main Ractors attempt access to non-shareable constants. The message contains the path to the constant which triggered the violation.
However, the path is incorrect when the constant was resolved through recursive search.
Repro¶
Run the this Ruby program:
TOPLEVEL = [1]
module M
def self.f
TOPLEVEL
end
end
Ractor.new { M.f }.value
Expected¶
Raises Ractor::IsolationError reporting that illegal access occured on Object::TOPLEVEL or TOPLEVEL
bug.rb:5:in 'M.f': can not access non-shareable objects in constant Object::TOPLEVEL by non-main Ractor. (Ractor::IsolationError)
from bug.rb:9:in 'block in <main>'
Actual¶
Raises Ractor::IsolationError reporting that illegal access occured on M::TOPLEVEL, which is incorrect. TOPLEVEL cannot be accessed as M::TOPLEVEL.
bug.rb:5:in 'M.f': can not access non-shareable objects in constant M::TOPLEVEL by non-main Ractor. (Ractor::IsolationError)
from bug.rb:9:in 'block in <main>'
Suspected code¶
This message is constructed in rb_const_get_0() defined in variable.c.
static VALUE
rb_const_get_0(VALUE klass, ID id, int exclude, int recurse, int visibility)
{
VALUE c = rb_const_search(klass, id, exclude, recurse, visibility);
if (!UNDEF_P(c)) {
if (UNLIKELY(!rb_ractor_main_p())) {
if (!rb_ractor_shareable_p(c)) {
rb_raise(rb_eRactorIsolationError, "can not access non-shareable objects in constant %"PRIsVALUE"::%s by non-main Ractor.", rb_class_path(klass), rb_id2name(id));
}
}
return c;
}
return rb_const_missing(klass, ID2SYM(id));
}
The path part is built as rb_class_path(klass), but klass may not be the module/class which the constant belongs, when the constant was found through recursive search in rb_const_search.
Updated by osyoyu (Daisuke Aritomo) about 18 hours ago
Patch submitted: https://github.com/ruby/ruby/pull/15556