< :前の番号
^ :番号順リスト
> :次の番号
P :前の記事(スレッド移動)
N :次の記事(スレッド移動)
|<:前のスレッド
>|:次のスレッド
^ :返事先
_:自分への返事
>:同じ返事先を持つ記事(前)
<:同じ返事先を持つ記事(後)
---:分割してスレッド表示、再表示
| :分割して(縦)スレッド表示、再表示
~ :スレッドのフレーム消去
.:インデックス
..:インデックスのインデックス
Issue #18243 has been updated by Eregon (Benoit Daloze).
After some discussion with @ko1, it seems there are different cases.
Sharing `self` is the only way to preserve the Proc's context correctly, i.e., method calls, @ivar, etc still work in the Proc
I think raising if `self` is not shareable might be a good way to let the user choose appropriately.
It's not clear if the user wants to make the Proc's `self` shareable, or if they want to change the Proc's self to a shareable object.
Then user can choose:
* `someProc = -> { ... }; Ractor.make_shareable(self); Ractor.make_shareable(someProc)`
* `someProc = -> { ... }; Ractor.make_shareable(someProc.with_self(nil))`.
`define_method(:name) do; ...; end` is special case because the receiver will be changed when called. And that's expected for users too. So maybe some special integration between `define_method` and Ractor makes sense (to skip the `self` check since not needed in that case).
----------------------------------------
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-94392
* Author: Eregon (Benoit Daloze)
* Status: Open
* 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>