Issue #19154 has been updated by luke-gru (Luke Gruber).
I've taken a look at this a bit and got this working:
```ruby
require 'rubygems' # get rubygems version of require
rs = []
100.times do
rs << Ractor.new do
require './c.rb'
end
end
rs.each(&:take)
p C
```
In file c.rb, it's
```ruby
class C
puts "GOT HERE"
end
```
And it works correctly, only printing "GOT HERE" once and defining C once.
I've had to make several small changes to the interpreter to enable setting ivars and
class variables on classes and modules.
I've also added an API of `Ractor.force_shareable!(obj)` that forces the shareable
flag on the object and all nested objects that it contains. This is necessary for certain
objects like monitor objects and other deeply nested objects. I only had to add
`Ractor.force_shareable!` to 2 or 3 places and requiring gems now works across Ractors. I
didn't add any
tests but I might add some if other people are interested in this work.
Also I'll take a look at `autoload`. I know for some, forcing objects to be marked as
shareable is a bit unsavory but as long as you know what you're doing it should be
okay, like the rust escape hatch of `unsafe`.
----------------------------------------
Bug #19154: Specify require and autoload guarantees in ractors
https://bugs.ruby-lang.org/issues/19154#change-101222
* Author: fxn (Xavier Noria)
* Status: Open
* Priority: Normal
* ruby -v: ruby 3.2.0preview3 (2022-11-27) [x86_64-darwin22]
* Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN
----------------------------------------
Given a file `c.rb`:
```ruby
class C
end
```
the following script:
```ruby
r1 = Ractor.new do
require './c.rb'
end
r2 = Ractor.new do
require './c.rb'
end
r1.take
r2.take
```
raises:
```
% ruby -v foo.rb
ruby 3.2.0preview3 (2022-11-27) [x86_64-darwin22]
foo.rb:1: warning: Ractor is experimental, and the behavior may change in future versions
of Ruby! Also there are many implementation issues.
#<Thread:0x000000010fee2928 run> terminated with exception (report_on_exception is
true):
#<Thread:0x00000001102acfe0 run> terminated with exception (report_on_exception is
true):
<internal:/Users/fxn/.rbenv/versions/3.2.0-preview3/lib/ruby/3.2.0+3/rubygems/core_ext/kernel_require.rb>:164:in
`ensure in require': can not access non-shareable objects in constant
Kernel::RUBYGEMS_ACTIVATION_MONITOR by non-main ractor. (Ractor::IsolationError)
from
<internal:/Users/fxn/.rbenv/versions/3.2.0-preview3/lib/ruby/3.2.0+3/rubygems/core_ext/kernel_require.rb>:167:in
`require'
from foo.rb:6:in `block in <main>'
<internal:/Users/fxn/.rbenv/versions/3.2.0-preview3/lib/ruby/3.2.0+3/rubygems/core_ext/kernel_require.rb>:37:in
`require'<internal:/Users/fxn/.rbenv/versions/3.2.0-preview3/lib/ruby/3.2.0+3/rubygems/core_ext/kernel_require.rb>:164:in
`ensure in require': : can not access non-shareable objects in constant
Kernel::RUBYGEMS_ACTIVATION_MONITOR by non-main ractor. (Ractor::IsolationError)
from
<internal:/Users/fxn/.rbenv/versions/3.2.0-preview3/lib/ruby/3.2.0+3/rubygems/core_ext/kernel_require.rb>:167:in
`require'
can not access non-shareable objects in constant Kernel::RUBYGEMS_ACTIVATION_MONITOR by
non-main ractor. (Ractor::IsolationError) from foo.rb:2:in `block in
<main>'
<internal:/Users/fxn/.rbenv/versions/3.2.0-preview3/lib/ruby/3.2.0+3/rubygems/core_ext/kernel_require.rb>:37:in
`require': can not access non-shareable objects in constant
Kernel::RUBYGEMS_ACTIVATION_MONITOR by non-main ractor. (Ractor::IsolationError)
from foo.rb:2:in `block in <main>'
from foo.rb:6:in `block in <main>'
<internal:ractor>:698:in `take': thrown by remote Ractor. (Ractor::RemoteError)
from foo.rb:9:in `<main>'
<internal:/Users/fxn/.rbenv/versions/3.2.0-preview3/lib/ruby/3.2.0+3/rubygems/core_ext/kernel_require.rb>:164:in
`ensure in require': can not access non-shareable objects in constant
Kernel::RUBYGEMS_ACTIVATION_MONITOR by non-main ractor. (Ractor::IsolationError)
from
<internal:/Users/fxn/.rbenv/versions/3.2.0-preview3/lib/ruby/3.2.0+3/rubygems/core_ext/kernel_require.rb>:167:in
`require'
from foo.rb:2:in `block in <main>'
<internal:/Users/fxn/.rbenv/versions/3.2.0-preview3/lib/ruby/3.2.0+3/rubygems/core_ext/kernel_require.rb>:37:in
`require': can not access non-shareable objects in constant
Kernel::RUBYGEMS_ACTIVATION_MONITOR by non-main ractor. (Ractor::IsolationError)
from foo.rb:2:in `block in <main>'
```
Would it be possible to have documentation about their interaction?
This is important also to understand autoloading within ractors, since constant references
may trigger `require` calls.
--
https://bugs.ruby-lang.org/