Actions
Bug #17722
opendefine_method with shareable results in "defined in a different Ractor"
Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 3.1.0dev (2021-03-13 master c7e6914b39) [x86_64-linux]
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) almost 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
Like0
Like0