#58069·rails

Intermittent FrozenError on ActiveSupport::Dependencies.autoload_paths at boot — setup_main_autoloader freeze races engine set_autoload_paths (Rails 8.1.3, Ruby 4.0)

Author: markhuzevkaCreated Jul 10, 2026Updated Sep 28, 2026

Environment

  • Rails 8.1.3 (railties 8.1.3), Ruby 4.0.5 (+PRISM), Zeitwerk 2.8.2, bootsnap 1.24.6
  • GitHub Actions ubuntu-latest, ruby/setup-ruby with bundler-cache: true
  • App has ~25 Rails engines (view_component, solid_queue/cache/cable, turbo, stimulus, tailwind, administrate, noticed, etc.)

What happens

On CI, booting the environment intermittently fails:

FrozenError:
  can't modify frozen Array: ["…/lib", "…/app/models", "…/app/controllers", … , "…/gems/view_component-contrib-0.2.5/app/views"]
# railties-8.1.3/lib/rails/engine.rb:578:in 'Array#unshift'
# railties-8.1.3/lib/rails/engine.rb:578:in 'block in <class:Engine>'
# railties-8.1.3/lib/rails/initializable.rb:24 … #run_initializers
# railties-8.1.3/lib/rails/application.rb:442:in 'Rails::Application#initialize!'
# config/environment.rb:5

Every spec file then fails to load (0 examples, N errors occurred outside of examples).

The two lines involved

  • Unshift — the Rails::Engine initializer :set_autoload_paths (railties/lib/rails/engine.rb, before: :bootstrap_hook):
    ruby
    ActiveSupport::Dependencies.autoload_paths.unshift(*_all_autoload_paths)   # crashes here
  • Freeze — the Rails::Application::Finisher initializer :setup_main_autoloader (railties/lib/rails/application/finisher.rb):
    ruby
    ActiveSupport::Dependencies.autoload_paths.freeze

So on the affected runs, an engine's :set_autoload_paths runs after :setup_main_autoloader froze the shared array.

Why this looks like an initializer-ordering issue

:set_autoload_paths is before: :bootstrap_hook; :setup_main_autoloader is a finisher — so the freeze should always run after every unshift. In every run we can observe, it does (dumping Rails.application.initializers.tsort_each.map(&:name): the freeze lands near index ~579, the 26 :set_autoload_paths at ~77–102, bootstrap_hook at ~158 — never violated). :setup_main_autoloader has no explicit after: binding it after :set_autoload_paths; the safe ordering appears to rely on tsort tie-breaking, i.e. initializer/railtie insertion order, which is gem-install-directory (filesystem) iteration order dependent.

What flips it

It surfaces only after a single-gem dependency bump (e.g. pagy 43.5.6 → 43.6.0) — an install-directory rename that reshuffles gem/railtie iteration order. A full-lockfile re-resolution does not trigger it. The bumped gem is irrelevant (pagy defines no Railtie); a different single-gem bump (aws-sdk-s3) produced the identical crash.

Reproduction — honest caveat

I have not been able to produce a minimal reproduction; it only manifests on the CI runner:

  • 10/10 clean boots on the exact commit+bump locally (matched Ruby 4.0.5, CI=true eager_load, cold bootsnap, isolated vendor/bundle, real RSpec).
  • Forcing a fully reversed ordered_railties still boots safely.
  • Adding any instrumentation before initialize! (e.g. calling Rails.application.initializers.tsort_each) makes the CI run pass — observing it perturbs it away.

Question

Should :setup_main_autoloader (the autoload_paths.freeze) carry an explicit after: on the engine :set_autoload_paths (or should the freeze be deferred / the unshift be order-tolerant), so a filesystem-order-dependent tsort tie-break can't order the freeze ahead of a late engine's unshift? I'm happy to test a candidate patch against CI that currently reproduces this on single-gem bumps.