Skip to content

Use a constraint object for the database route scope - #547

Closed
mhenrixon wants to merge 1 commit into
ankane:masterfrom
mhenrixon:ractor-shareable-route-constraint
Closed

mhenrixon wants to merge 1 commit into
ankane:masterfrom
mhenrixon:ractor-shareable-route-constraint

Conversation

@mhenrixon

Copy link
Copy Markdown
Contributor

This replaces the constraints: proc { ... } on the (:database) scope with a module that responds to matches?. The check is the same, so behavior doesn't change.

Rails main has an experimental Rails.application.ractorize!. It makes the route set Ractor-shareable at boot, and with ActiveSupport::Ractors.unshareable_proc_action = :raise set, an unshareable proc stops the boot. The proc's self is the routes file context, so it can't be made shareable. Apps with PgHero installed then fail to boot, even if they never request a PgHero page.

Before (Ruby 4.0.7, Rails main, a small app that boots, calls ractorize! and serves two requests in a Ractor):

Ractor::IsolationError: Proc's self is not shareable: #<Proc:0x... pghero-4.0.1/config/routes.rb:2>

After: the app boots and serves both requests.

This only fixes boot. Serving PgHero's own pages from a Ractor needs more work, starting in Rails.

I added tests for a valid and an unknown database segment (the unknown one returns a 500 without the constraint). bundle exec rake test passes on Ruby 4.0 with Active Record 8.1 and on Ruby 3.3 with Active Record 7.2.

The proc's self is the routes file context, so the route set cannot be made
Ractor-shareable (Rails main's experimental Rails.application.ractorize!).
A module with matches? is shareable and keeps the behavior.
@ankane

ankane commented Oct 17, 2026

Copy link
Copy Markdown
Owner

Hi @mhenrixon, thanks for the PR. Merged a version of this in the commit above.

However, I wasn't able to get the test suite working with Rails.application.ractorize! (ractorize branch). Please let me know if you have any ideas. It currently fails with:

/home/user/.rbenv/versions/4.0.7/lib/ruby/gems/4.0.0/bundler/gems/rails-e9aab74b131f/railties/lib/rails/application.rb:680:in 'Ractor.make_shareable': can not make shareable object for #<Monitor:0x0000ffff7f4e2c60> (Ractor::Error)
    from /home/user/.rbenv/versions/4.0.7/lib/ruby/gems/4.0.0/bundler/gems/rails-e9aab74b131f/railties/lib/rails/application.rb:680:in 'Rails::Application#ractorize!'
    from /home/user/code/test/test_helper.rb:73:in '<top (required)>'

@mhenrixon

Copy link
Copy Markdown
Contributor Author

Appreciate the fast action @ankane, I clearly have more work to do. I have a few open issues towards rails to fix some stuff as well so I expect a lot to be needed before everything works together.

I'll use that branch and keep testing. Appreciate you!

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

Successfully merging this pull request may close these issues.

2 participants