DatabaseCleaner / database_cleaner

Strategies for cleaning databases in Ruby. Can be used to ensure a clean state for testing.
https://www.rubydoc.info/github/DatabaseCleaner/database_cleaner
MIT License
2.93k stars 484 forks source link

NoMethodError: undefined method 'state' for nil:NilClass #692

Open lucasthomazoni opened 2 years ago

lucasthomazoni commented 2 years ago

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.

navidemad commented 1 year ago

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

invisiblehermitt commented 1 year ago

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 ?

pariv commented 1 year ago

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

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

curve-jyothish commented 1 year ago

Same here. No updates?

xcskier56 commented 1 year ago

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 🤷

lxnewayfarer commented 11 months ago

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

xcskier56 commented 10 months ago

@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