Skip to content
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

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

Open
lucasthomazoni opened this issue Aug 9, 2022 · 7 comments
Open

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

lucasthomazoni opened this issue Aug 9, 2022 · 7 comments

Comments

@lucasthomazoni
Copy link

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
Copy link

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

@invisiblehermitt
Copy link

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
Copy link

pariv commented Jan 13, 2023

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

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

@curve-jyothish
Copy link

Same here. No updates?

@xcskier56
Copy link

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
Copy link

lxnewayfarer commented Nov 3, 2023

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

@xcskier56
Copy link

@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

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

7 participants