Skip to content

Commit 2c40aad

Browse files
committed
Revert "Allow reading cvars from non-main Ractors (ruby#16308)"
This reverts commit ab32c0e.
1 parent b146c07 commit 2c40aad

2 files changed

Lines changed: 6 additions & 110 deletions

File tree

bootstraptest/test_ractor.rb

Lines changed: 4 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,8 +1058,8 @@ def initialize
10581058
values.join
10591059
}
10601060
1061-
# Reading non-shareable cvar from non-main Ractor is not allowed
1062-
assert_equal 'can not read non-shareable class variable @@cv from non-main Ractors (C)', %q{
1061+
# cvar in shareable-objects are not allowed to access from non-main Ractor
1062+
assert_equal 'can not access class variables from non-main Ractors (@@cv from C)', %q{
10631063
class C
10641064
@@cv = 'str'
10651065
end
@@ -1077,8 +1077,8 @@ class C
10771077
end
10781078
}
10791079
1080-
# also cached non-shareable cvar read from non-main Ractor is not allowed
1081-
assert_equal 'can not read non-shareable class variable @@cv from non-main Ractors (C)', %q{
1080+
# also cached cvar in shareable-objects are not allowed to access from non-main Ractor
1081+
assert_equal 'can not access class variables from non-main Ractors (@@cv from C)', %q{
10821082
class C
10831083
@@cv = 'str'
10841084
def self.cv
@@ -1099,95 +1099,6 @@ def self.cv
10991099
end
11001100
}
11011101
1102-
# Reading shareable cvar from non-main Ractor is allowed
1103-
assert_equal 'shareable', %q{
1104-
class C
1105-
@@cv = 'shareable'.freeze
1106-
def self.cv
1107-
@@cv
1108-
end
1109-
end
1110-
1111-
Ractor.new { C.cv }.value
1112-
}
1113-
1114-
# Reading shareable cvar (integer) from non-main Ractor is allowed
1115-
assert_equal '42', %q{
1116-
class C
1117-
@@cv = 42
1118-
def self.cv
1119-
@@cv
1120-
end
1121-
end
1122-
1123-
Ractor.new { C.cv }.value.to_s
1124-
}
1125-
1126-
# Reading shareable cvar via module include from non-main Ractor is allowed
1127-
assert_equal 'hello', %q{
1128-
module M
1129-
@@cv = 'hello'.freeze
1130-
def self.cv
1131-
@@cv
1132-
end
1133-
end
1134-
1135-
class C
1136-
include M
1137-
def self.cv
1138-
@@cv
1139-
end
1140-
end
1141-
1142-
Ractor.new { C.cv }.value
1143-
}
1144-
1145-
# Writing cvar from non-main Ractor is not allowed
1146-
assert_equal 'can not set class variables from non-main Ractors (@@cv from C)', %q{
1147-
class C
1148-
@@cv = 'str'
1149-
def self.cv=(v)
1150-
@@cv = v
1151-
end
1152-
end
1153-
1154-
r = Ractor.new do
1155-
C.cv = 'new'
1156-
end
1157-
1158-
begin
1159-
r.join
1160-
rescue Ractor::RemoteError => e
1161-
e.cause.message
1162-
end
1163-
}
1164-
1165-
# Reading cvar that was made shareable after initial assignment
1166-
assert_equal 'made shareable', %q{
1167-
class C
1168-
@@cv = +'made shareable'
1169-
Ractor.make_shareable(@@cv)
1170-
def self.cv
1171-
@@cv
1172-
end
1173-
end
1174-
1175-
Ractor.new { C.cv }.value
1176-
}
1177-
1178-
# cvar_defined? works from non-main Ractor
1179-
assert_equal 'true', %q{
1180-
class C
1181-
@@cv = 42
1182-
def self.cv?
1183-
defined?(@@cv)
1184-
end
1185-
end
1186-
1187-
r = Ractor.new { C.cv? ? 'true' : 'false' }
1188-
r.value
1189-
}
1190-
11911102
# Getting non-shareable objects via constants by other Ractors is not allowed
11921103
assert_equal 'can not access non-shareable objects in constant C::CONST by non-main Ractor.', <<~'RUBY', frozen_string_literal: false
11931104
class C

variable.c

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,20 +1216,7 @@ CVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(VALUE klass, ID id)
12161216
if (UNLIKELY(rb_ractor_isolation_check_active())) {
12171217
/* See comment on the instance-variable warning below for why we
12181218
* pass rb_class_path() rather than the class itself. */
1219-
rb_ractor_isolation_violation("can not set class variables from non-main Ractors (%"PRIsVALUE" from %"PRIsVALUE")", rb_id2str(id), rb_class_path(klass));
1220-
}
1221-
}
1222-
1223-
static void
1224-
cvar_read_ractor_check(VALUE klass, ID id, VALUE val)
1225-
{
1226-
if (UNLIKELY(rb_ractor_isolation_check_active())) {
1227-
VALUE chain = Qnil;
1228-
if (!rb_ractor_shareable_p_continue(val, &chain)) {
1229-
rb_ractor_isolation_violation_with_chain(chain,
1230-
"can not read non-shareable class variable %"PRIsVALUE" from non-main Ractors (%"PRIsVALUE")",
1231-
rb_id2str(id), rb_class_path(klass));
1232-
}
1219+
rb_ractor_isolation_violation("can not access class variables from non-main Ractors (%"PRIsVALUE" from %"PRIsVALUE")", rb_id2str(id), rb_class_path(klass));
12331220
}
12341221
}
12351222

@@ -4330,6 +4317,7 @@ cvar_overtaken(VALUE front, VALUE target, ID id)
43304317
}
43314318

43324319
#define CVAR_LOOKUP(v,r) do {\
4320+
CVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(klass, id); \
43334321
if (cvar_lookup_at(klass, id, (v))) {r;}\
43344322
CVAR_FOREACH_ANCESTORS(klass, v, r);\
43354323
} while(0)
@@ -4351,8 +4339,6 @@ find_cvar(VALUE klass, VALUE * front, VALUE * target, ID id)
43514339
void
43524340
rb_cvar_set(VALUE klass, ID id, VALUE val)
43534341
{
4354-
CVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(klass, id);
4355-
43564342
VALUE tmp, front = 0, target = 0;
43574343

43584344
tmp = klass;
@@ -4427,7 +4413,6 @@ rb_cvar_find(VALUE klass, ID id, VALUE *front)
44274413
klass, ID2SYM(id));
44284414
}
44294415
cvar_overtaken(*front, target, id);
4430-
cvar_read_ractor_check(klass, id, value);
44314416
return (VALUE)value;
44324417
}
44334418

0 commit comments

Comments
 (0)