Skip to content

Conversation

@zverok
Copy link
Contributor

@zverok zverok commented Jan 13, 2022

The docs weren't updated in time after 3.1 release 🤷

# r.take
# # I see C
# # can not access instance variables of classes/modules from non-main Ractors (RuntimeError)
#
Copy link
Contributor Author

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 🤔

Copy link
Contributor

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.

Copy link
Contributor Author

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 :)

@hsbt hsbt requested a review from ko1 January 14, 2022 23:59
@hsbt hsbt assigned ko1 Jan 14, 2022
@hsbt hsbt added the Documentation Improvements to documentation. label Jan 15, 2022
@hsbt hsbt force-pushed the docs-ractor-instance-vars branch from 9ac3745 to 00c93f6 Compare February 23, 2022 03:04
@zverok zverok force-pushed the docs-ractor-instance-vars branch from 00c93f6 to 28f7c94 Compare December 10, 2022 11:24
@zverok
Copy link
Contributor Author

zverok commented Dec 11, 2022

@ko1 Can you review it, please? As of today, Ractor's docs still have pre-3.1 behavior described.

@zverok zverok requested review from jeremyevans and nobu December 24, 2022 13:19
Copy link
Contributor

@jeremyevans jeremyevans left a 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:
Copy link
Contributor

@jeremyevans jeremyevans Dec 24, 2022

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.take

The 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.take

In 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.take

Here'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.take

Copy link
Contributor Author

@zverok zverok Dec 24, 2022

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".

Copy link
Contributor Author

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)
#
Copy link
Contributor

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.

@zverok zverok force-pushed the docs-ractor-instance-vars branch from 28f7c94 to dee6db3 Compare December 24, 2022 21:47
@zverok zverok force-pushed the docs-ractor-instance-vars branch from dee6db3 to de7f3ac Compare December 24, 2022 22:07
@zverok zverok requested a review from jeremyevans December 24, 2022 22:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Documentation Improvements to documentation.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants