Skip to content

Commit 8a7477d

Browse files
Prevent recursive isolation warnings and unify reporting
1 parent fd52ef5 commit 8a7477d

6 files changed

Lines changed: 127 additions & 27 deletions

File tree

‎cont.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2172,6 +2172,8 @@ fiber_t_alloc(VALUE fiber_value, unsigned int blocking)
21722172
fiber->blocking = blocking;
21732173
fiber->killed = 0;
21742174
cont_init(&fiber->cont, th);
2175+
// A warning hook in the creator must not silence this new fiber.
2176+
fiber->cont.saved_ec.ractor_isolation_warning = false;
21752177

21762178
fiber->cont.saved_ec.fiber_ptr = fiber;
21772179
fiber->cont.saved_ec.serial = next_ec_serial(th->ractor);

‎ractor.c‎

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1873,9 +1873,8 @@ make_shareable_check_shareable(VALUE obj)
18731873
else if (!allow_frozen_shareable_p(obj)) {
18741874
if (!RB_TYPE_P(obj, T_DATA)) {
18751875
if (rb_ractor_isolation_check_p()) {
1876-
rb_category_warn(RB_WARN_CATEGORY_RACTOR_ISOLATION,
1877-
"can not make shareable object of class %+"PRIsVALUE,
1878-
rb_class_of(obj));
1876+
rb_ractor_isolation_violation("can not make shareable object of class %+"PRIsVALUE,
1877+
rb_class_of(obj));
18791878
return traverse_stop;
18801879
}
18811880
rb_raise(rb_eRactorError,
@@ -1888,9 +1887,8 @@ make_shareable_check_shareable(VALUE obj)
18881887
return traverse_skip;
18891888
}
18901889
else if (rb_ractor_isolation_check_p()) {
1891-
rb_category_warn(RB_WARN_CATEGORY_RACTOR_ISOLATION,
1892-
"can not make shareable object of class %+"PRIsVALUE
1893-
" because it refers unshareable objects", rb_class_of(obj));
1890+
rb_ractor_isolation_violation("can not make shareable object of class %+"PRIsVALUE
1891+
" because it refers unshareable objects", rb_class_of(obj));
18941892
return traverse_stop;
18951893
}
18961894
else {
@@ -1903,9 +1901,8 @@ make_shareable_check_shareable(VALUE obj)
19031901
return rb_ractor_shareable_p(obj) ? traverse_cont : traverse_stop;
19041902
}
19051903
else if (rb_ractor_isolation_check_p()) {
1906-
rb_category_warn(RB_WARN_CATEGORY_RACTOR_ISOLATION,
1907-
"can not make shareable object of class %+"PRIsVALUE,
1908-
rb_class_of(obj));
1904+
rb_ractor_isolation_violation("can not make shareable object of class %+"PRIsVALUE,
1905+
rb_class_of(obj));
19091906
return traverse_stop;
19101907
}
19111908
else {
@@ -4296,7 +4293,8 @@ static const struct st_hash_type isolation_warn_hash_type = {
42964293
static bool
42974294
isolation_warnings_enabled_p(void)
42984295
{
4299-
return !NIL_P(ruby_verbose) && rb_warning_category_enabled_p(RB_WARN_CATEGORY_RACTOR_ISOLATION);
4296+
return !GET_EC()->ractor_isolation_warning && !NIL_P(ruby_verbose) &&
4297+
rb_warning_category_enabled_p(RB_WARN_CATEGORY_RACTOR_ISOLATION);
43004298
}
43014299

43024300
// nearest Ruby frame outside <internal:...>, so Ractor.new and Port#<< report the app site
@@ -4354,20 +4352,39 @@ isolation_warn_first_p(VALUE message, VALUE path, int line)
43544352
return first;
43554353
}
43564354

4357-
void
4358-
rb_ractor_isolation_warn(VALUE message)
4355+
static VALUE
4356+
isolation_warn_emit(VALUE message)
43594357
{
4360-
if (!isolation_warnings_enabled_p()) return;
4361-
43624358
int line;
43634359
VALUE path = isolation_source_location(&line);
43644360
StringValueCStr(message);
4365-
if (ruby_ractor_isolation_enabled < 2 && !isolation_warn_first_p(message, path, line)) return;
4361+
if (ruby_ractor_isolation_enabled < 2 && !isolation_warn_first_p(message, path, line)) return Qnil;
43664362

43674363
const char *file = NIL_P(path) ? NULL : RSTRING_PTR(path);
43684364
rb_category_compile_warn(RB_WARN_CATEGORY_RACTOR_ISOLATION, file, line, "%s", RSTRING_PTR(message));
43694365
RB_GC_GUARD(message);
43704366
RB_GC_GUARD(path);
4367+
return Qnil;
4368+
}
4369+
4370+
static VALUE
4371+
isolation_warn_end(VALUE ec_ptr)
4372+
{
4373+
((rb_execution_context_t *)ec_ptr)->ractor_isolation_warning = false;
4374+
return Qnil;
4375+
}
4376+
4377+
void
4378+
rb_ractor_isolation_warn(VALUE message)
4379+
{
4380+
if (!isolation_warnings_enabled_p()) return;
4381+
4382+
// A user-defined warning hook can itself violate isolation, including
4383+
// before entering its body when defined with define_singleton_method.
4384+
// Keep the guard fiber-local and restore it even if the hook raises.
4385+
rb_execution_context_t *ec = GET_EC();
4386+
ec->ractor_isolation_warning = true;
4387+
rb_ensure(isolation_warn_emit, message, isolation_warn_end, (VALUE)ec);
43714388
}
43724389

43734390
void

‎test/ruby/ractor_isolation_helper.rb‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,8 @@ module RactorIsolationWarnings
55
QUEUE = Thread::Queue.new
66

77
def warn(message, category: nil)
8-
if category == :ractor_isolation && !Thread.current[:capturing_isolation_warning]
9-
Thread.current[:capturing_isolation_warning] = true
10-
begin
11-
QUEUE << Ractor.make_shareable(message)
12-
ensure
13-
Thread.current[:capturing_isolation_warning] = false
14-
end
8+
if category == :ractor_isolation
9+
QUEUE << Ractor.make_shareable(message)
1510
return nil
1611
end
1712
super

‎test/ruby/test_ractor.rb‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,92 @@ def test_isolation_check_warns_on_outer_variable_capture
12071207
RUBY
12081208
end
12091209

1210+
def test_isolation_check_handles_block_defined_warning_hooks
1211+
[1, 2].each do |level|
1212+
assert_ractor(<<~'RUBY', args: [{"RUBY_RACTOR_ISOLATION" => level.to_s}], ignore_stderr: true)
1213+
messages = []
1214+
$warning_hook_global = 1
1215+
$first_warning_global = 2
1216+
$second_warning_global = 3
1217+
Warning.define_singleton_method(:warn) do |message, category: nil|
1218+
next unless category == :ractor_isolation
1219+
$warning_hook_global
1220+
messages << message
1221+
raise "warning hook failed" if message.include?("$first_warning_global")
1222+
end
1223+
1224+
result = Ractor.new do
1225+
begin
1226+
$first_warning_global
1227+
rescue RuntimeError => error
1228+
raise unless error.message == "warning hook failed"
1229+
end
1230+
$second_warning_global
1231+
:done
1232+
end.value
1233+
1234+
assert_equal :done, result
1235+
assert_equal 2, messages.size
1236+
assert_match(/global variable \$first_warning_global/, messages[0])
1237+
assert_match(/global variable \$second_warning_global/, messages[1])
1238+
RUBY
1239+
end
1240+
end
1241+
1242+
def test_isolation_check_deduplicates_all_warning_paths
1243+
source = <<~'RUBY'
1244+
require "etc"
1245+
klass = Class.new
1246+
klass.define_method(:call) { :done }
1247+
Ractor.new(klass) do |type|
1248+
file = File.open(IO::NULL)
1249+
3.times { Ractor.make_shareable(file) }
1250+
file.close
1251+
3.times { Etc.getlogin }
1252+
3.times { type.new.call }
1253+
end.value
1254+
RUBY
1255+
1256+
[1, 2].each do |level|
1257+
env = {"RUBY_RACTOR_ISOLATION" => level.to_s}
1258+
assert_in_out_err([env, "-W:no-experimental", "-e", source], success: true) do |_stdout, stderr|
1259+
expected = level == 1 ? 1 : 3
1260+
assert_equal expected, stderr.grep(/^-e:6: warning: can not make shareable object/).size
1261+
assert_equal expected, stderr.grep(/^-e:8: warning: ractor unsafe method/).size
1262+
assert_equal expected, stderr.grep(/^-e:9: warning: can not call method call/).size
1263+
assert_empty stderr.grep(/<internal:/)
1264+
end
1265+
end
1266+
end
1267+
1268+
def test_isolation_check_warning_guard_is_fiber_local
1269+
assert_ractor(<<~'RUBY', args: [{"RUBY_RACTOR_ISOLATION" => "2"}], ignore_stderr: true)
1270+
messages = []
1271+
$suspended_warning_global = 1
1272+
$other_fiber_global = 2
1273+
$parent_fiber_global = 3
1274+
Warning.define_singleton_method(:warn) do |message, category: nil|
1275+
next unless category == :ractor_isolation
1276+
messages << message
1277+
if message.include?("$suspended_warning_global")
1278+
Fiber.yield Fiber.new { $other_fiber_global }
1279+
end
1280+
end
1281+
1282+
result = Ractor.new do
1283+
suspended = Fiber.new { $suspended_warning_global }
1284+
other = suspended.resume
1285+
[other.resume, $parent_fiber_global, suspended.resume]
1286+
end.value
1287+
1288+
assert_equal [2, 3, 1], result
1289+
assert_equal 3, messages.size
1290+
assert_match(/global variable \$suspended_warning_global/, messages[0])
1291+
assert_match(/global variable \$other_fiber_global/, messages[1])
1292+
assert_match(/global variable \$parent_fiber_global/, messages[2])
1293+
RUBY
1294+
end
1295+
12101296
def test_isolation_check_does_not_mark_an_invalid_proc_shareable
12111297
assert_ractor(<<~'RUBY', args: [{"RUBY_RACTOR_ISOLATION" => "1"}], ignore_stderr: true)
12121298
captured = []

‎vm_core.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,7 @@ struct rb_execution_context_struct {
11241124
VALUE passed_block_handler; /* for rb_iterate */
11251125

11261126
uint8_t raised_flag; /* only 3 bits needed */
1127+
bool ractor_isolation_warning; /* suppress violations inside Warning.warn */
11271128

11281129
/* n.b. only 7 bits needed, really: */
11291130
BITFIELD(enum method_missing_reason, method_missing_reason, 8);

‎vm_insnhelper.c‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3555,8 +3555,7 @@ ractor_unsafe_check(void)
35553555

35563556
if (rb_ractor_isolation_check_p()) {
35573557
// same category as IsolationError: to the caller both mean "not Ractor-safe"
3558-
rb_category_warn(RB_WARN_CATEGORY_RACTOR_ISOLATION,
3559-
"ractor unsafe method called from not main ractor");
3558+
rb_ractor_isolation_violation("ractor unsafe method called from not main ractor");
35603559
return;
35613560
}
35623561

@@ -4130,9 +4129,9 @@ static void
41304129
vm_bmethod_unshareable_proc_violation(rb_execution_context_t *ec, const rb_callable_method_entry_t *cme)
41314130
{
41324131
if (rb_ractor_isolation_check_p()) {
4133-
rb_category_warn(RB_WARN_CATEGORY_RACTOR_ISOLATION,
4134-
"can not call method %"PRIsVALUE" defined with an un-shareable Proc from a different Ractor",
4135-
rb_id2str(cme->called_id));
4132+
rb_ractor_isolation_violation(
4133+
"can not call method %"PRIsVALUE" defined with an un-shareable Proc from a different Ractor",
4134+
rb_id2str(cme->called_id));
41364135
}
41374136
else {
41384137
rb_raise(rb_eRuntimeError, "defined with an un-shareable Proc in a different Ractor");

0 commit comments

Comments
 (0)