Skip to content

Commit c941fce

Browse files
luke-grubyroot
authored andcommitted
Throw RuntimeError if getting/setting ractor local storage for non-main ractor
[Bug #19367]
1 parent 7517d76 commit c941fce

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

bootstraptest/test_ractor.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,6 +1564,24 @@ class C
15641564
}.take
15651565
}
15661566

1567+
# Ractor-local storage
1568+
assert_equal '2', %q{
1569+
Ractor.new {
1570+
fails = 0
1571+
begin
1572+
Ractor.main[:key] # cannot get ractor local storage from non-main ractor
1573+
rescue => e
1574+
fails += 1 if e.message =~ /Cannot get ractor local/
1575+
end
1576+
begin
1577+
Ractor.main[:key] = 'val'
1578+
rescue => e
1579+
fails += 1 if e.message =~ /Cannot set ractor local/
1580+
end
1581+
fails
1582+
}.take
1583+
}
1584+
15671585
###
15681586
### Synchronization tests
15691587
###

ractor.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -835,15 +835,21 @@ def self.make_shareable obj, copy: false
835835
end
836836
end
837837

838-
# get a value from ractor-local storage of current Ractor
838+
# get a value from ractor-local storage for current Ractor
839839
# Obsolete and use Ractor.[] instead.
840840
def [](sym)
841+
if (self != Ractor.current)
842+
raise RuntimeError, "Cannot get ractor local storage for non-current ractor"
843+
end
841844
Primitive.ractor_local_value(sym)
842845
end
843846

844-
# set a value in ractor-local storage of current Ractor
847+
# set a value in ractor-local storage for current Ractor
845848
# Obsolete and use Ractor.[]= instead.
846849
def []=(sym, val)
850+
if (self != Ractor.current)
851+
raise RuntimeError, "Cannot set ractor local storage for non-current ractor"
852+
end
847853
Primitive.ractor_local_value_set(sym, val)
848854
end
849855

0 commit comments

Comments
 (0)