Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when using in Ractor #291

Closed
ukolovda opened this issue Apr 11, 2024 · 3 comments
Closed

Error when using in Ractor #291

ukolovda opened this issue Apr 11, 2024 · 3 comments

Comments

@ukolovda
Copy link

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.

@fxn
Copy link
Owner

fxn commented Apr 11, 2024

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.

@fxn fxn closed this as completed Apr 11, 2024
@casperisfine
Copy link
Contributor

To add to this, if you want to use Ractors, you should eager load your application, that's easy to do with Zeitwerk.

@ukolovda
Copy link
Author

@fxn , @casperisfine thank you! I will try it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants