From: marcandre-ruby-core@... Date: 2021-01-16T16:41:10+00:00 Subject: [ruby-core:102114] [Ruby master Bug#17543] Ractor isolation broken by `self` in shareable proc Issue #17543 has been updated by marcandre (Marc-Andre Lafortune). Eregon (Benoit Daloze) wrote in #note-3: > Not preserving `self` would be confusing, e.g., when calling other methods like: > ```ruby > class Foo > def bar > ... > end > > def foo > test { bar } > end > end > ``` In my proposal, calling that block would result in a `NoMethodError` on `<#DetachedSelf>`. Is it that confusing? The detached self could be created dynamically with the location of the call to `make_shareable` to help debugging. > I think the only case where it's acceptable to change the self of a proc implicitly I am not sure why you state "implicitly". It is explicit that `make_shareable` changes the block in more ways than just freezing, in particular by detaching the local variables and binding. > is `Ractor.new {}` because that already cannot capture any local variables: > ``` > ruby -e 'o=Object.new; Ractor.new { o }' > :267:in `new': can not isolate a Proc because it accesses outer variables (o). (ArgumentError) > ``` > > Capturing variables or self is very similar IMHO, so I think they should be treated the same. > ``` > ruby -e 'o=Object.new; Ractor.make_shareable(-> { o }); p o.frozen?' > :816:in `make_shareable': can not make shareable Proc because it can refer unshareable object # from variable `o' (Ractor::IsolationError) > ``` Note that they are *not* treated the same way. `Ractor.new{}` forbids any reference to outside variables, while `make_shareable(proc)` allows it as long as the values are themselves shareable. ``` o=Object.new.freeze Ractor.new { o } # => ArgumentError Ractor.make_shareable(-> { o }) # => no error ``` > The most obvious fix is therefore to raise the same error if `self` is not shareable. I agree that is the most obvious, but that makes `Ractor.make_shareable(-> { puts "hello" })` fail, which is far from being useful. ---------------------------------------- Bug #17543: Ractor isolation broken by `self` in shareable proc https://bugs.ruby-lang.org/issues/17543#change-89970 * Author: marcandre (Marc-Andre Lafortune) * Status: Open * Priority: Normal * Assignee: ko1 (Koichi Sasada) * ruby -v: 3.0.0p0 * Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN, 3.0: UNKNOWN ---------------------------------------- Discussing with @MaxLap we realized that the `self` in a shareable proc is not properly isolated: ``` class Foo attr_accessor :x def pr Ractor.make_shareable(Proc.new { self }) end end f = Foo.new f.x = [1, 2, 3] Ractor.new(f.pr) { |pr| pr.call.x << :oops } p f.x # => [1, 2, 3, :oops] ``` If the `self` refers to a shareable object then it's fine, but for non-shareable objects it has to be reset to `nil` or to a global shareable object that would have an instructive `inspect`. ```ruby Ractor::DETACHED_SELF = Object.new def << Ractor::DETACHED_SELF def inspect '<#detached self>' end alias to_s inspect end Ractor::DETACHED_SELF.freeze ``` -- https://bugs.ruby-lang.org/ Unsubscribe: