Skip to content

Error using GraphQL::Dataloader::AsyncDataloader #4764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
timscott opened this issue Jan 6, 2024 · 5 comments
Closed

Error using GraphQL::Dataloader::AsyncDataloader #4764

timscott opened this issue Jan 6, 2024 · 5 comments

Comments

@timscott
Copy link
Contributor

timscott commented Jan 6, 2024

Migrate to GraphQL::Dataloader::AsyncDataloader as described here: https://graphql-ruby.org/dataloader/async_dataloader. Using v2.2.4. Getting error:

RuntimeError: running is given, but running
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/debug-1.8.0/lib/debug/thread_client.rb:155:in `set_mode'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/debug-1.8.0/lib/debug/thread_client.rb:889:in `wait_next_action_'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/debug-1.8.0/lib/debug/thread_client.rb:866:in `wait_next_action'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/debug-1.8.0/lib/debug/thread_client.rb:236:in `wait_reply'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/debug-1.8.0/lib/debug/thread_client.rb:240:in `on_load'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/debug-1.8.0/lib/debug/session.rb:140:in `block in initialize'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/activerecord-7.0.4/lib/active_record/relation/delegation.rb:65:in `module_eval'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/activerecord-7.0.4/lib/active_record/relation/delegation.rb:65:in `block in generate_method'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/3.2.0/mutex_m.rb:79:in `synchronize'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/3.2.0/mutex_m.rb:79:in `mu_synchronize'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/activerecord-7.0.4/lib/active_record/relation/delegation.rb:61:in `generate_method'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/activerecord-7.0.4/lib/active_record/relation/delegation.rb:39:in `generate_relation_method'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/activerecord-7.0.4/lib/active_record/relation/delegation.rb:107:in `method_missing'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/activerecord-7.0.4/lib/active_record/associations/preloader/association.rb:22:in `hash'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/activerecord-7.0.4/lib/active_record/associations/preloader/batch.rb:41:in `each'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/activerecord-7.0.4/lib/active_record/associations/preloader/batch.rb:41:in `group_by'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/activerecord-7.0.4/lib/active_record/associations/preloader/batch.rb:41:in `group_and_load_similar'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/activerecord-7.0.4/lib/active_record/associations/preloader/batch.rb:27:in `call'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/activerecord-7.0.4/lib/active_record/associations/preloader.rb:118:in `call'
  /Users/tscott/code/side_projects/slotted/app/graphql/sources/association.rb:8:in `fetch'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/graphql-2.2.4/lib/graphql/dataloader/source.rb:136:in `run_pending_keys'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/graphql-2.2.4/lib/graphql/dataloader/async_dataloader.rb:79:in `each'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/graphql-2.2.4/lib/graphql/dataloader/async_dataloader.rb:79:in `block in spawn_source_task'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/async-2.8.0/lib/async/task.rb:161:in `block in run'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/async-2.8.0/lib/async/task.rb:331:in `block in schedule'
@rmosolgo
Copy link
Owner

rmosolgo commented Jan 8, 2024

Aw, shoot -- I should have said in the docs: I think you need Rails 7.1+ for this, because isolation_level = :fiber didn't work right before that. For reference, the async docs: https://socketry.github.io/async/guides/compatibility/index#rails.

I see that you're on Rails 7.0.4, so my suggestion is to get on Rails 7.1 first, then switch to isolation_level = :fiber, then let me know if this pops up again. In the meantime I'll update the docs to point out Rails 7.1 and close this.

@timscott
Copy link
Contributor Author

timscott commented Jan 9, 2024

Thanks. Same error:

RuntimeError: running is given, but running
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/debug-1.8.0/lib/debug/thread_client.rb:155:in `set_mode'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/debug-1.8.0/lib/debug/thread_client.rb:889:in `wait_next_action_'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/debug-1.8.0/lib/debug/thread_client.rb:866:in `wait_next_action'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/debug-1.8.0/lib/debug/thread_client.rb:236:in `wait_reply'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/debug-1.8.0/lib/debug/thread_client.rb:240:in `on_load'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/debug-1.8.0/lib/debug/session.rb:140:in `block in initialize'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/bootsnap-1.13.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/bootsnap-1.13.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/zeitwerk-2.6.12/lib/zeitwerk/kernel.rb:38:in `require'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/activerecord-7.1.2/lib/active_record/reflection.rb:863:in `association_class'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/activerecord-7.1.2/lib/active_record/associations.rb:320:in `association'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/activerecord-7.1.2/lib/active_record/associations/preloader/branch.rb:79:in `block in grouped_records'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/activerecord-7.1.2/lib/active_record/associations/preloader/branch.rb:77:in `each'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/activerecord-7.1.2/lib/active_record/associations/preloader/branch.rb:77:in `grouped_records'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/activerecord-7.1.2/lib/active_record/associations/preloader/branch.rb:114:in `loaders'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/activerecord-7.1.2/lib/active_record/associations/preloader/branch.rb:71:in `runnable_loaders'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/activerecord-7.1.2/lib/active_record/associations/preloader/batch.rb:15:in `each'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/activerecord-7.1.2/lib/active_record/associations/preloader/batch.rb:15:in `flat_map'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/activerecord-7.1.2/lib/active_record/associations/preloader/batch.rb:15:in `call'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/activerecord-7.1.2/lib/active_record/associations/preloader.rb:121:in `call'
  /Users/tscott/code/side_projects/slotted/app/graphql/sources/association.rb:8:in `fetch'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/graphql-2.2.4/lib/graphql/dataloader/source.rb:136:in `run_pending_keys'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/graphql-2.2.4/lib/graphql/dataloader/async_dataloader.rb:79:in `each'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/graphql-2.2.4/lib/graphql/dataloader/async_dataloader.rb:79:in `block in spawn_source_task'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/async-2.8.0/lib/async/task.rb:161:in `block in run'
  /Users/tscott/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/async-2.8.0/lib/async/task.rb:331:in `block in schedule'

I noticed that the request gets a 500 response immediately, and the error pops up in the console after a few seconds.

@rmosolgo
Copy link
Owner

rmosolgo commented Jan 9, 2024

I see that the debug gem is the top line of the backtrace. I looked at the changelog for that gem (https://github.com/ruby/debug/releases) to see if there were any recent relevant changes, and I found ruby/debug#987, which fixed compatibility with Fiber scheduler in debug v1.9.0. (That PR is from the author of the async gem!) Could you try updating that gem to v1.9.0 or higher?

@rmosolgo rmosolgo reopened this Jan 9, 2024
@timscott
Copy link
Contributor Author

That seems to have fixed it. Thanks!

One thing to mention. At first I was getting intermittent database connection errors. I fixed it by increase by the connection pool size from 10 to 20. Might be worth noting that async means more concurrent connections.

@rmosolgo
Copy link
Owner

Glad to hear it, thanks for sharing.

Thanks for the suggestion on database connections. I'll update the docs soon to include a note about that 👍

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

No branches or pull requests

2 participants