Fix Issue #1464: Rails 7.1 Zeitwerk autoloading race condition#18
Merged
takaokouji merged 1 commit intomasterfrom Jan 21, 2026
Merged
Conversation
… condition Issue JSONAPI-Resources#1464 reported intermittent 'NameError: uninitialized constant JSONAPI::ResourceController' errors in Rails 7.1 when eager_load is disabled. Root cause: - ActiveSupport.on_load(:action_controller_base) lazy loading mechanism - Causes race condition with Zeitwerk autoloading in Rails 7.1+ - Errors occur randomly during test execution - Only happens locally, not in CI with eager loading enabled Solution: - Remove conditional lazy loading with ActiveSupport.on_load - Always require 'jsonapi/resource_controller' directly - Eliminates race condition with Zeitwerk The fix ensures ResourceController is always available without depending on ActionController::Base load timing. Workarounds no longer needed: - config.eager_load = true in test environment - Manual require 'jsonapi/resource_controller' before use - Switching to ActsAsResourceController mixin Related: https://github.com/cerebris/jsonapi-resources/issues/1464
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes JSONAPI-Resources#1464 by removing the ActiveSupport.on_load lazy loading mechanism that causes race conditions with Zeitwerk in Rails 7.1+.
Problem
In Rails 7.1 with Zeitwerk autoloading and eager_load disabled, tests would intermittently fail with:
The error was random (2-3 to 20-30 times per test run) and only occurred locally, not in CI where eager loading is enabled.
Root Cause
The gem used
ActiveSupport.on_load(:action_controller_base)to delay loading of ResourceController:This lazy loading mechanism creates a race condition with Zeitwerk's autoloading in Rails 7.1+.
Solution
Remove the conditional lazy loading and always require ResourceController directly:
This eliminates the race condition and ensures ResourceController is always available.
Testing
Workarounds No Longer Needed
config.eager_load = truein test environmentrequire 'jsonapi/resource_controller'before useRelated
🤖 Generated with Claude Code