[Feature #21033] Proc を Ractor.make_shareable する際の条件を緩和する提案

2025/01/23に公開

[Feature #21033] Allow lambdas that don't access self to be Ractor shareable

  • Proc オブジェクトを Ractor.make_shareable で共有可能オブジェクトにする場合に selffrozen である必要がある
class Foo
  def make_lambda
    x = 123
    lambda { x }
  end
end

foo = Foo.new

# self = foo は frozen でないのでエラーになる
# error: Proc's self is not shareable: #<Proc:0x00007decf7597998 /tmp/vh9EJDh/25:4 (lambda)> (Ractor::IsolationError)
Ractor.make_shareable(foo.make_lambda)

foo.freeze

# self = foo は frozen なのでエラーにならない
# no error
Ractor.make_shareable(foo.make_lambda)
  • この時に Procself にアクセスしない場合は『 self の状態に関わらず共有可能オブジェクトにすることができるように緩和する』提案
  • proc 内の命令をみて self を利用しているかどうかの判断をするみたいですね
    • なので proc 内で eval を使用した場合は暗黙的に self への参照がされるのでこの場合はエラーになるみたい
    • binding を参照した場合も同様
  • これなんですが議論の内容をみていると self を参照していない、っていう部分がなかなか難しそうですねえ
GitHubで編集を提案

Discussion