Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions bootstraptest/test_ractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1564,6 +1564,24 @@ class C
}.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
###
Expand Down
10 changes: 8 additions & 2 deletions ractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -835,15 +835,21 @@ def self.make_shareable obj, copy: false
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.
def [](sym)
if (self != Ractor.current)
raise RuntimeError, "Cannot get ractor local storage for non-current ractor"
end
Primitive.ractor_local_value(sym)
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.
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)
end

Expand Down
Loading