Issue #17722 has been reported by chucke (Tiago Cardoso).

----------------------------------------
Bug #17722: define_method with shareable results in "defined in a different Ractor"
https://bugs.ruby-lang.org/issues/17722

* Author: chucke (Tiago Cardoso)
* Status: Open
* Priority: Normal
* ruby -v: ruby 3.1.0dev (2021-03-13 master c7e6914b39) [x86_64-linux]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN, 3.0: UNKNOWN
----------------------------------------
Testing against latest master, and expectinng [this](https://bugs.ruby-lang.org/issues/17159?tab=history) to already be handled, I nonetheless found a weird case, for which I managed to build a short reproduction.

```ruby
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.



-- 
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>