Skip to content

馃悰 RelationConnection loads the same relation twice under AsyncDataloader (2.6.7) - #5685

Open
benschwarz wants to merge 1 commit into
rmosolgo:masterfrom
benschwarz:patch-1
Open

馃悰 RelationConnection loads the same relation twice under AsyncDataloader (2.6.7)#5685
benschwarz wants to merge 1 commit into
rmosolgo:masterfrom
benschwarz:patch-1

Conversation

@benschwarz

Copy link
Copy Markdown

Given a query like:

{ posts(first: 20) { edges { node { id } } pageInfo { endCursor } } }

Both fields call load_nodes. 2.6.7 runs them concurrently, earlier releases didn't.

@nodes ||= limited_nodes.to_a   # relation_connection.rb:224

Fiber one waits on the DB before setting @nodes. Fiber two sees empty and loads the same relation again. Whichever finishes first clears ActiveRecord's load state from under the other.

This results in:

NoMethodError: undefined method 'instantiate' for nil, plus a duplicate query on every paginated connection.

Refactor load_nodes method to improve node loading logic and handle concurrent access.
@rmosolgo

rmosolgo commented Aug 6, 2026

Copy link
Copy Markdown
Owner

Hey, thanks for reporting this and sorry for the trouble 馃槚

I'm hesitant to address this with dataloader.yield because I expect this to all happen inside GraphQL-Ruby's execution steps -- without handing off to Dataloader to fetch results from Dataloader sources.

Do you have a guess why the Fiber is yielding here? Is this a custom connection implementation?

@benschwarz

Copy link
Copy Markdown
Author

Hey @rmosolgo, fair enough. I am not sure if this is the right fix either, but I was able to get my test suite running with it -- good to open the discussion though.

I'm on 1.6.3, and there are seemingly many issues with async dataloader where db connections get used by old/finished/crashed fibres. On 1.6.3 we have a custom ConnectionReleasingDataloader < GraphQL::Dataloader::AsyncDataloader that calls release_connection and also reclaims abandoned fibres. It's non ideal but is working well.

Given the updates in 1.6.7 I thought it was worthwhile to see if I could get any joy from it. In using the code in this PR, I was able to upgrade, and also remove the connection releasing dataloader 馃槆

@rmosolgo

rmosolgo commented Aug 6, 2026

Copy link
Copy Markdown
Owner

Thanks for sharing a bit more. I'd love to hear more details (eg error messages, backtraces) about what goes wrong without manual connection handling. Maybe there's a clue in there to an underlying bug!

Also, if you find any clues about why load_nodes would cause a fiber yield, please do share -- I'd love to understand this better and find a solid fix.

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