Skip to content

Fix deadlock in belongs_to/has_many :through; support polymorphic source_type#336

Open
kbrock wants to merge 3 commits intoactive-hash:masterfrom
kbrock:issue_334
Open

Fix deadlock in belongs_to/has_many :through; support polymorphic source_type#336
kbrock wants to merge 3 commits intoactive-hash:masterfrom
kbrock:issue_334

Conversation

@kbrock
Copy link
Copy Markdown
Collaborator

@kbrock kbrock commented Aug 18, 2025

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.

Note: belongs_to_active_hash is unchanged and still available for direct use.

Thank you, @alexgriff, for the reproducer.

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
@kbrock kbrock force-pushed the issue_334 branch 2 times, most recently from f91dc58 to b3a12f3 Compare April 4, 2026 06:07
kbrock and others added 2 commits April 6, 2026 22:20
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>
@kbrock
Copy link
Copy Markdown
Collaborator Author

kbrock commented Apr 7, 2026

update: removed class load
update: removed another class loader (total tangent, but solves a similar issue)

@kbrock kbrock changed the title Handle belongs_to :through, :polymorphic: true Fix deadlock in belongs_to/has_many :through; support polymorphic source_type Apr 21, 2026
@kbrock kbrock requested a review from flavorjones April 21, 2026 00:21
@kbrock kbrock requested a review from zilkey May 2, 2026 22:53
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.

Polymorphic has_many with a source_type raise an exception in ActiveHash 4.0

1 participant