Error when using in Ractor
ukolovda opened this issue · 3 comments
Ruby 3 introduced Ractor, allowing utilize more than 1 core of CPU.
But when I try use it (make second instance of Rails Application), I got the error:
ruby-3.2.3/gems/zeitwerk-2.6.13/lib/zeitwerk/registry.rb:122:in
loader_for': can not get unshareable values from instance variables of classes/modules from non-main Ractors (Ractor::IsolationError)`
Full test code:
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gem "rails"
# If you want to test against edge Rails replace the previous line with this:
# gem "rails", github: "rails/rails", branch: "main"
end
require "active_support"
require "active_support/core_ext/object/blank"
require "minitest/autorun"
class TestApplication < Rails::Application
end
# Workaround of Railties error
Ractor.make_shareable(Rails::Railtie::ABSTRACT_RAILTIES)
main_app = Rails.application
class BugTest < Minitest::Test
def test_stuff
r = Ractor.new do
app2 = TestApplication.new
end
end
end
Ruby 3.2.3
I don't sure that my approach is correct, but we can create multiple instance of application, it can reduce RAM usage for many applications in the world.
Ractors cannot even do concurrent require
. For example, this:
# c.rb
class C
end
# foo.rb
r1 = Ractor.new do
require './c.rb'
end
r2 = Ractor.new do
require './c.rb'
end
r1.take
r2.take
raises
foo.rb:1: warning: Ractor is experimental, and the behavior may change in future versions of Ruby! Also there are many implementation issues.
#<Thread:0x0000000104ab9028 run> terminated with exception (report_on_exception is true):
<internal:/Users/fxn/.rbenv/versions/3.3.0/lib/ruby/site_ruby/3.3.0/rubygems/core_ext/kernel_require.rb>:39:in `require'#<Thread:0x0000000104b1d6e0 run> terminated with exception (report_on_exception is true):
<internal:/Users/fxn/.rbenv/versions/3.3.0/lib/ruby/site_ruby/3.3.0/rubygems/core_ext/kernel_require.rb>:39:in `require': can not access non-shareable objects in constant Kernel::RUBYGEMS_ACTIVATION_MONITOR by non-main ractor. (Ractor::IsolationError)
which was reported in https://bugs.ruby-lang.org/issues/19154.
As the error says, this is an experimental feature. By now, I am closing.
To add to this, if you want to use Ractors, you should eager load your application, that's easy to do with Zeitwerk.
@fxn , @casperisfine thank you! I will try it.