Project

General

Profile

Actions

Bug #17722

closed

define_method with shareable results in "defined in a different Ractor"

Added by chucke (Tiago Cardoso) about 3 years ago. Updated 7 months ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 3.1.0dev (2021-03-13 master c7e6914b39) [x86_64-linux]
[ruby-core:102849]

Description

Testing against latest master, and expectinng this to already be handled, I nonetheless found a weird case, for which I managed to build a short reproduction.

class A
  define_method :"a=" do |val|
    instance_variable_set(:"@#{v}", val)
  end
  attr_reader :a
  
  def initialize(opts)
    opts.each do |k, v|
      puts "#{k} = #{v}"
      __send__(:"#{k}=", v)
    end
  end
end

ractors = []

DEFAULTS = { a: 1 }
Ractor.make_shareable(DEFAULTS)

1.times do
  ractors << Ractor.new do
    a = A.new(DEFAULTS)
  end
end
ractors.map(&:take)

This script fails with "defined in a different Ractor (RuntimeError)".

The error comes from the execution of "define_method". I was under the expectation that it would work, given that the passed value is shareable, so it shouldn't make a difference if it was defined in a different ractor.

Updated by tagomoris (Satoshi Tagomori) over 2 years ago

The reason of the unexpected error is, the block parameter of define_method is un-shareable.
I created a patch to explain it: https://github.com/ruby/ruby/pull/4771

Actions #2

Updated by jeremyevans0 (Jeremy Evans) 7 months ago

  • Status changed from Open to Closed
Actions

Also available in: Atom PDF

Like0
Like0Like0