Fix deadlock in belongs_to/has_many :through; support polymorphic source_type#336
Open
kbrock wants to merge 3 commits into
Open
Fix deadlock in belongs_to/has_many :through; support polymorphic source_type#336kbrock wants to merge 3 commits into
kbrock wants to merge 3 commits into
Conversation
We undefine these classes in the after {}
So in theory, they should not be a duplicate.
rspec often uses stub_const, but active record has issues with this since
they dig into the class.name and stuff
f91dc58 to
b3a12f3
Compare
Move class resolution out of definition time and into method bodies. Call super first so AR registers reflections (preserving eager loading support), then override accessors with runtime ActiveHash checks. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
When source_type is present, resolve the class at runtime and look up by foreign key directly, bypassing AR's polymorphic belongs_to which does not work with ActiveHash. Fixes active-hash#334. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Collaborator
Author
|
update: removed class load |
flavorjones
reviewed
May 12, 2026
Collaborator
There was a problem hiding this comment.
@kbrock Sorry for not reviewing this earlier. It looks good, I left one comment about whether using :source_type will generate N+1 queries, but I think we can just collapse that to a where(id: ids).
| klass = source_type.safe_constantize | ||
| if klass < ActiveHash::Base | ||
| ids = send(options[:through]).map { |jm| jm.send(source_foreign_key) }.compact.uniq | ||
| ids.flat_map { |id| klass.find_by_id(id) }.compact |
Collaborator
There was a problem hiding this comment.
This could potentially make a lot of queries. Could we instead do:
Suggested change
| ids.flat_map { |id| klass.find_by_id(id) }.compact | |
| klass.where(id: ids) |
? If we need to keep the order we could do something like this to keep it to a single query:
records = klass.where(id: ids).index_by(&:id)
ids.filter_map { |id| records[id] }
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.
Resolving classes at definition time can deadlock in production. AR holds a schema lock and a class-load lock. Loading one class that triggers loading another will hit both locks and hang.
belongs_toandhas_many :throughnow resolve ActiveHash classes at runtime, avoiding the deadlockhas_many :throughwithsource_type(fixes Polymorphichas_manywith asource_typeraise an exception in ActiveHash 4.0 #334)Note:
belongs_to_active_hashis unchanged and still available for direct use.Thank you, @alexgriff, for the reproducer.