Throw RuntimeError if getting/setting ractor local storage for non-main ractor

[Bug #19367]
This commit is contained in:
lukeg 2023-01-22 09:42:14 -05:00 committed by Jean Boussier
parent 7517d7629d
commit c941fced21
Notes: git 2025-05-13 11:18:23 +00:00
2 changed files with 26 additions and 2 deletions

View File

@ -1564,6 +1564,24 @@ assert_equal '1', %q{
}.take }.take
} }
# Ractor-local storage
assert_equal '2', %q{
Ractor.new {
fails = 0
begin
Ractor.main[:key] # cannot get ractor local storage from non-main ractor
rescue => e
fails += 1 if e.message =~ /Cannot get ractor local/
end
begin
Ractor.main[:key] = 'val'
rescue => e
fails += 1 if e.message =~ /Cannot set ractor local/
end
fails
}.take
}
### ###
### Synchronization tests ### Synchronization tests
### ###

View File

@ -835,15 +835,21 @@ class Ractor
end end
end end
# get a value from ractor-local storage of current Ractor # get a value from ractor-local storage for current Ractor
# Obsolete and use Ractor.[] instead. # Obsolete and use Ractor.[] instead.
def [](sym) def [](sym)
if (self != Ractor.current)
raise RuntimeError, "Cannot get ractor local storage for non-current ractor"
end
Primitive.ractor_local_value(sym) Primitive.ractor_local_value(sym)
end end
# set a value in ractor-local storage of current Ractor # set a value in ractor-local storage for current Ractor
# Obsolete and use Ractor.[]= instead. # Obsolete and use Ractor.[]= instead.
def []=(sym, val) def []=(sym, val)
if (self != Ractor.current)
raise RuntimeError, "Cannot set ractor local storage for non-current ractor"
end
Primitive.ractor_local_value_set(sym, val) Primitive.ractor_local_value_set(sym, val)
end end