-
Notifications
You must be signed in to change notification settings - Fork 5.6k
[DOC] Update docs on sharing module instance variables in Ractors #5437
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
base: master
Are you sure you want to change the base?
Conversation
| # r.take | ||
| # # I see C | ||
| # # can not access instance variables of classes/modules from non-main Ractors (RuntimeError) | ||
| # |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't seem this example demonstrated anything useful 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It demonstrates accessing a non-sharable instance variable in a module or class raises an exception.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, but now I see how this example is useful :) The difference with the previous one is exactly in passing the class vs accessing it as a constant :)
9ac3745 to
00c93f6
Compare
00c93f6 to
28f7c94
Compare
|
@ko1 Can you review it, please? As of today, Ractor's docs still have pre-3.1 behavior described. |
jeremyevans
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the new example should probably be modified, see inline comments.
| # # I see C | ||
| # # can not access instance variables of classes/modules from non-main Ractors (RuntimeError) | ||
| # | ||
| # Shareable instance variables of modules and classes can be accessed: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This example doesn't do a good job of showing how this behavior is specific to modules and classes. The same type example works for instances:
class C
attr_accessor :tricky
end
c = C.new
c.tricky = ['test'.freeze].freeze
r = Ractor.new(c) do |cls|
puts "I see #{cls}"
puts "I see #{cls.tricky}"
end
r.takeThe same type of example also works for unsharable values for instances:
class C
attr_accessor :tricky
end
c = C.new
c.tricky = ['test'.freeze]
r = Ractor.new(c) do |c2|
puts "I see #{c2}"
puts "I see #{c2.tricky}"
end
r.takeIn most cases, you aren't going to be passing modules and classes to the ractor, but having the ractor access them directly. Here is an example that shows that such access is allowed for sharable values:
class C
class << self
attr_accessor :tricky
end
end
C.tricky = ['test'.freeze].freeze
r = Ractor.new do
puts "I see #{C}"
puts "I see #{C.tricky}"
end
r.takeHere's an example that shows failure for none sharable values:
class C
class << self
attr_accessor :tricky
end
end
C.tricky = ['test'.freeze]
r = Ractor.new do
puts "I see #{C}"
puts "I see #{C.tricky}"
end
r.takeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This example doesn't do a good job of showing how this behavior is specific to modules and classes.
Actually, it seems to be: the "(especially modules and classes)" part is important.
Here's an example that shows failure for non sharable values:
It wouldn't fail if you'll try it:
class C
attr_accessor :tricky
end
c = C.new
c.tricky = ['test']
r = Ractor.new(c) do |c2|
puts "I see #{c2}" # I see #<C:0x00007f46d4dc80d0>
puts "I see #{c2.tricky}"
end
r.take
p c # #<C:0x00007f46d4dc8620 @tricky=["test"]>Note that object ids of c and c2 are different. As far as I understand, it just copies c into c2 and it becomes fully accessible inside Ractor, but unrelated to c.
It's because Ractor.shareable?(c) is false, and the documentation describes the behavior of mutable shareable objects.
The behavior that changed in 3.1 is related specifically to module/class instance vars, as far as I understand. Your example works both on 3.0 and 3.1, while this one:
class C
class << self
attr_accessor :tricky
end
end
C.tricky = ['test'.freeze].freeze
r = Ractor.new(C) do |cls|
puts "I see #{cls}"
puts "I see #{cls.tricky}"
end
r.take...will work on 3.1, but on 3.0, it throws "can not access instance variables of classes/modules from non-main Ractor".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Edited the examples, sorry, didn't get it right the first time)
| # r.take | ||
| # # I see C | ||
| # # can not access instance variables of classes/modules from non-main Ractors (RuntimeError) | ||
| # |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It demonstrates accessing a non-sharable instance variable in a module or class raises an exception.
28f7c94 to
dee6db3
Compare
dee6db3 to
de7f3ac
Compare
The docs weren't updated in time after 3.1 release 🤷