🐙

[Bug #19765] Ractor.make_shareable に Method#to_proc を渡してもエラーにならないバグ報告

に公開

[Bug #19765] Ractor.make_shareable ignores self of a proc created from a Method

  • 以下のような proc オブジェクトを Ractor.make_shareable に渡すとエラーになります
    • Ractor.make_shareable は Ractor 間で共有可能にするためのメソッド
    • 簡単にいうと frozen のように『不変であること』を定義する
str = "homu"

a = str.instance_exec { proc { to_s } }
Ractor.make_shareable a
# => <internal:ractor>:820:in `make_shareable': Proc's self is not shareable: #<Proc:0x00000001064b62c8 (irb):1> (Ractor::IsolationError)

b = str.instance_exec { method(:to_s) }
Ractor.make_shareable b
# => <internal:ractor>:820:in `make_shareable': can not make shareable object for #<Method: String#to_s()> (Ractor::Error)
  • 上記のような proc オブジェクトの場合は『意図せず値を変更してしまう可能性がある』のでそれが防がれている形です
  • このチケットでは以下のように Proc#to_proc を渡した場合にエラーにならないというバグ報告
str = "homu"
block = str.method(:upcase!).to_proc
Ractor.make_shareable block

# block オブジェクトは不変であるはずが他の変数の値を変更できてしまう
block.call
pp str
# => "HOMU"
  • この不具合は開発版の Ruby 3.5-dev で既に修正済み
str = "homu"
block = str.method(:upcase!).to_proc
Ractor.make_shareable block
# Ruby 3.4 => no error
# Ruby 3.5 => 'Ractor.make_shareable': can not make shareable object for #<Method: String#upcase!(*)> (Ractor::Error)
GitHubで編集を提案

Discussion