From: "Eregon (Benoit Daloze)" Date: 2022-04-27T15:31:29+00:00 Subject: [ruby-core:108419] [Ruby master Bug#18243] Ractor.make_shareable does not freeze the receiver of a Proc but allows accessing ivars of it Issue #18243 has been updated by Eregon (Benoit Daloze). I think in general it's good the caller code is aware it needs a shareable `self`, otherwise there might be big surprises. For instance if your script adds ``` def self.helper_method ... end ``` and used that within the block, it would not work since `self` is `nil` and not the main object (or in an instance method of some class it'd be `nil` instead of the instance). The `Parallel do` there is a good hint there might be a change of receiver, without that it could be very confusing why the `self` was magically changed to `nil`/why the context around the block is not the expected one. ---------------------------------------- Bug #18243: Ractor.make_shareable does not freeze the receiver of a Proc but allows accessing ivars of it https://bugs.ruby-lang.org/issues/18243#change-97454 * Author: Eregon (Benoit Daloze) * Status: Closed * Priority: Normal * Backport: 2.6: UNKNOWN, 2.7: UNKNOWN, 3.0: UNKNOWN ---------------------------------------- ```ruby class C attr_accessor :foo def setter_proc Ractor.make_shareable(-> v { @foo = v }) end end c = C.new c.foo = 1 p c proc = c.setter_proc p c.frozen? Ractor.new(proc) { |s| s.call(42) }.take p c ``` gives ``` # false # # BUG ``` But that must be a bug, it means the non-main Ractor can directly mutate an object from the main Ractor. I found this while thinking about https://github.com/ruby/ostruct/pull/29/files and whether `Ractor.make_shareable` would freeze `@table` and the `OpenStruct` instance (I think it needs to). Repro code for ostruct and changing ostruct.rb to `$setter = ::Ractor.make_shareable(setter_proc)`: ```ruby require 'ostruct' os = OpenStruct.new os.foo = 1 $setter.call(2) p os Ractor.new($setter) { |s| s.call(42) }.take p os ``` gives ``` # :267: warning: Ractor is experimental, and the behavior may change in future versions of Ruby! Also there are many implementation issues. # # BUG ``` -- https://bugs.ruby-lang.org/ Unsubscribe: