rails: RuntimeError: can't add a new key into hash during iteration on lib/active_record/connection_adapters/abstract/query_cache.rb

Steps to reproduce

No idea how to reproduce.

Expected behavior

No concurrency errors in query cache.

Actual behavior

A concurrency error in query cache.

It is happening inside a Sidekiq worker, FWIW.

And it has been happening for at least a year now:

Screen Shot 2022-06-07 at 12 46 42

Here’s the backtrace:

      "/app/vendor/cache/ruby/2.7.0/gems/activerecord-6.1.3.2/lib/active_record/connection_adapters/abstract/query_cache.rb:118:in `block in cache_sql'",
      "/app/vendor/cache/ruby/2.7.0/gems/activesupport-6.1.3.2/lib/active_support/concurrency/load_interlock_aware_monitor.rb:26:in `block (2 levels) in synchronize'",
      "/app/vendor/cache/ruby/2.7.0/gems/activesupport-6.1.3.2/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `handle_interrupt'",
      "/app/vendor/cache/ruby/2.7.0/gems/activesupport-6.1.3.2/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `block in synchronize'",
      "/app/vendor/cache/ruby/2.7.0/gems/activesupport-6.1.3.2/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `handle_interrupt'",
      "/app/vendor/cache/ruby/2.7.0/gems/activesupport-6.1.3.2/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `synchronize'",
      "/app/vendor/cache/ruby/2.7.0/gems/activerecord-6.1.3.2/lib/active_record/connection_adapters/abstract/query_cache.rb:109:in `cache_sql'",
      "/app/vendor/cache/ruby/2.7.0/gems/activerecord-6.1.3.2/lib/active_record/connection_adapters/abstract/query_cache.rb:101:in `select_all'",
      "/app/vendor/cache/ruby/2.7.0/gems/activerecord-6.1.3.2/lib/active_record/querying.rb:47:in `find_by_sql'",
      "/app/vendor/cache/ruby/2.7.0/gems/scout_apm-2.6.6/lib/scout_apm/instruments/active_record.rb:333:in `find_by_sql_with_scout_instruments'",
      "/app/vendor/cache/ruby/2.7.0/gems/activerecord-6.1.3.2/lib/active_record/statement_cache.rb:150:in `execute'",
      "/app/vendor/cache/ruby/2.7.0/gems/activerecord-6.1.3.2/lib/active_record/associations/association.rb:227:in `find_target'",
      "/app/vendor/cache/ruby/2.7.0/gems/activerecord-6.1.3.2/lib/active_record/associations/singular_association.rb:39:in `find_target'",
      "/app/vendor/cache/ruby/2.7.0/gems/activerecord-6.1.3.2/lib/active_record/associations/association.rb:174:in `load_target'",
      "/app/vendor/cache/ruby/2.7.0/gems/activerecord-6.1.3.2/lib/active_record/associations/association.rb:67:in `reload'",
      "/app/vendor/cache/ruby/2.7.0/gems/activerecord-6.1.3.2/lib/active_record/associations/singular_association.rb:9:in `reader'",
      "/app/vendor/cache/ruby/2.7.0/gems/activerecord-6.1.3.2/lib/active_record/associations/builder/association.rb:103:in `location'",
      "/app/app/services/my_service.rb:9:in `initialize'",

Pseudo code from the original source code:

class MyWorker
  include Sidekiq::Worker

  def perform
    MyModel.where('x < ?', 1.month.ago).where(a_column: A_VALUE).find_each do |my_model|
      MyService.new(lead_source).something
    end
  end
end

class MyService
  def initialize(my_model)
    @my_model = my_model
    @something = my_model.a_standard_belongs_to_relation # error is triggered here!
  end

  def something
    # ...
  end
end

This has been reported before here but without a specific solution.

sentry-raven, sentry-ruby and activerecord-import were mentioned but we aren’t using it but we do have prometheus_exporter.

What I don’t get is how this error could happen considering there is a monitor around the query cache.

System configuration

Rails version: 6.1.3.2

Ruby version: 2.7.2

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 16 (11 by maintainers)

Most upvoted comments

Had the same issue. RuntimeError: can't add a new key into hash during iteration exception from Sidekiq jobs.

Ruby 3.2.2, Rails 7.1.3.4. prometheus_exporter gem was really outdated. After bumping to the newest version there is no issue anymore 😃