database_cleaner icon indicating copy to clipboard operation
database_cleaner copied to clipboard

NoMethodError: undefined method 'state' for nil:NilClass

Open lucasthomazoni opened this issue 3 years ago • 7 comments

Setup

- Ruby: 3.1.2
- Rails: 7.0.3.1
- PostgreSQL: 14.4
- OS: Ubuntu 20.04.4 LTS on WSL2
- GitHub Actions: 
  - Image: ubuntu-latest
    - PostgreSQL: 14.2

---

Gemfile.lock:
database_cleaner-active_record (2.0.1)
      activerecord (>= 5.a)
      database_cleaner-core (~> 2.0.0)
database_cleaner-core (2.0.1)

Error

From time to time, if a run my specs locally it might throw the error below, the fun thing is it errors for like two or three runs and then it passes (using same seed at all times);

This error also occurs occasionally on GitHub Actions, I have to re-run my workflow some times as well to make it pass (different seeds)

$ rspec spec/
 Failure/Error:
       DatabaseCleaner.cleaning do
         example.run
       end

     NoMethodError:
       undefined method `state' for nil:NilClass

                 transaction.rollback unless transaction.state.invalidated?
                                                        ^^^^^^
     # /home/lucas/.rvm/gems/ruby-3.1.2@perk-api-sch/gems/database_cleaner-active_record-2.0.1/lib/database_cleaner/active_record/transaction.rb:17:in `block in clean'
     # /home/lucas/.rvm/gems/ruby-3.1.2@perk-api-sch/gems/database_cleaner-active_record-2.0.1/lib/database_cleaner/active_record/transaction.rb:15:in `each'
     # /home/lucas/.rvm/gems/ruby-3.1.2@perk-api-sch/gems/database_cleaner-active_record-2.0.1/lib/database_cleaner/active_record/transaction.rb:15:in `clean'
     # /home/lucas/.rvm/gems/ruby-3.1.2@perk-api-sch/gems/database_cleaner-core-2.0.1/lib/database_cleaner/strategy.rb:32:in `cleaning'
     # /home/lucas/.rvm/gems/ruby-3.1.2@perk-api-sch/gems/database_cleaner-core-2.0.1/lib/database_cleaner/cleaners.rb:34:in `block (2 levels) in cleaning'
     # /home/lucas/.rvm/gems/ruby-3.1.2@perk-api-sch/gems/database_cleaner-core-2.0.1/lib/database_cleaner/cleaners.rb:35:in `cleaning'
     # ./spec/rails_helper.rb:77:in `block (2 levels) in <top (required)>'

Database Cleaner Config

DatabaseCleaner.strategy = :transaction
DatabaseCleaner[:active_record].clean_with :truncation, {
  except: %w(
    table_a
    table_b
    table_c
  )
}

config.around do |example|
  DatabaseCleaner.cleaning do
    example.run
  end
end

Any idea what might be causing this error and how to fix it?

Thanks for the gem and your attention.

lucasthomazoni avatar Aug 09 '22 20:08 lucasthomazoni

UP i'm getting randomly the same issue, any updates ?

navidemad avatar Nov 06 '22 23:11 navidemad

I saw @navidmad said it happened to him randomly so I solved it by restarting my PC. Quite fun,hh.

UP i'm getting randomly the same issue, any updates ?

invisiblehermitt avatar Jan 11 '23 06:01 invisiblehermitt

UP i'm getting randomly the same issue, any updates ?

Same here.... Looking forward for fix...

pariv avatar Jan 13 '23 06:01 pariv

Same here. No updates?

curve-jyothish avatar May 14 '23 14:05 curve-jyothish

Well, this is definitely still a problem but I have a few things:

  1. This is the location of the error now: https://github.com/rails/rails/blob/7-0-stable/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb#L311, and my hunch is that it is some weird race condition while waiting for the lock.
  2. That bit of code has been re-worked in rails#main and since I cannot re-create this error locally, I can't test if this is still a problem in the next release of rails

My current solution is the following:

# rails_helper.rb

RSpec.configure do |config|
  # ... 
  config.around do |example|
    DatabaseCleaner.cleaning do
      example.run
    end
  rescue NoMethodError => e
    next if e.message.match? 'undefined method `state\' for nil:NilClass'

    raise e
  end
end

This is definitely a bit ugly, but it works 🤷

xcskier56 avatar Sep 13 '23 03:09 xcskier56

Pay attention to the asynchronous behavior of your code. The cleaner may break precisely because of asynchronous interaction with the database

lxnewayfarer avatar Nov 03 '23 06:11 lxnewayfarer

@lxnewayfarer, I wish I had read your comment a few days ago. Doing a rails 7.1 upgrade had super super weird failures in the tests. Getting messages like server received message x234 from client and all sorts of other random weird failures.

Turns out ActiveStorage::AnalyzeJob was running asynchronously. We use sidekiq and so I hadn't set active job to run inline. 5 hours late and 1 line of code the problem was solved

xcskier56 avatar Nov 08 '23 22:11 xcskier56