[ruby-core:108418] [Ruby master Bug#18243] Ractor.make_shareable does not freeze the receiver of a Proc but allows accessing ivars of it
From:
"shan (Shannon Skipper)" <noreply@...>
Date:
2022-04-27 15:20:50 UTC
List:
ruby-core #108418
Issue #18243 has been updated by shan (Shannon Skipper).
Eregon (Benoit Daloze) wrote in #note-9:
> From #18276,
> I think the best solution by far is `Ractor.make_shareable(proc.bind(nil))` (concise, clear, clean as it does not pass unshared object to other Ractor), and raise on `Ractor.make_shareable(proc)` if the Proc's `self` is not shareable.
> And of course `Ractor.make_shareable(self); Ractor.make_shareable(proc)` is also possible if desired, and explicit so safe and expected/intuitive.
A `proc.bind(nil)` seems like a nice solution. This gist, for example, was using Procs with unfrozen receivers and now raises an `IsolationError`, but I can't think of a reasonable way to use `nil.instance_eval` when the proc isn't created up front (updated the gist to use a thinly veiled instance_eval for demonstration purposes). https://gist.github.com/havenwood/6ac4d8c32f8af0364c27ffa26241db67
----------------------------------------
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-97452
* 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
```
#<C:0x00007f2a3aae7a98 @foo=1>
false
#<C:0x00007f2a3aae7a98 @foo=42> # 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
```
#<OpenStruct foo=2>
<internal:ractor>:267: warning: Ractor is experimental, and the behavior may change in future versions of Ruby! Also there are many implementation issues.
#<OpenStruct foo=42> # BUG
```
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>