Repository: rails/rails

Issue #2943: Nil results with multiple connections (cucumber) 
Created: 2011-09-08T19:10:20Z
We just upgraded an application to 3.1, and see a consistent issue in the test suite (cucumber, capybara, selenium).
Seeing issue #1339 we upgraded to the 3-1-stable branch, but this did not fix our issue.
Since we are using capybara, there are two PG connections active.  Each is constructed here, in PostgreSQLAdapter#connect https://github.com/rails/rails/blob/5ed3e6a61f/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb#L1032  One connection is initialized during rails boot, the other by Capybara::Server#boot (in a separate thread).
The issue only shows up when we use direct model access (e.g. creating models via a Factory, or looking models up) after interacting with the app through capybara.  If we place all direct model access before the web interactions, the error doesn't happen.
If I patch aforementioned private method PostgreSQLAdapter#connect to use the same PGconn object for both connections, then the error does not happen:
    # Connects to a PostgreSQL server and sets up the adapter depending on the
    # connected server's characteristics.
    def connect
      # Commented out the original line:
      # @connection = PGconn.connect(*@connection_parameters)
      #
      # And replaced it with:
      $global_pg_connection ||= PGconn.connect(*@connection_parameters)
      @connection = $global_pg_connection
The full error:
Scenario: Canceling editing the worry score after an error                      # features/user/edit_patient_worry_score.feature:66
  Given the following patient exists:                                           # factory_girl-2.0.5/lib/factory_girl/step_definitions.rb:99
    | first_name | middle_name | last_name | Practice       |
    | Wayne      | Mark        | Rooney    | Name: Beantown |
    NoMethodError: undefined method `fields' for nil:NilClass: SELECT  1 FROM "patients"  WHERE ("patients"."last_name" = 'Rooney' AND "patients"."first_name" = 'Wayne' AND "patients"."dob" = '1914-11-24 18:50:15.500542') LIMIT 1 (ActiveRecord::StatementInvalid)
    /Users/jason/dev/rails/activesupport/lib/active_support/whiny_nil.rb:48:in `method_missing'
    /Users/jason/dev/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb:594:in `block in exec_query'
    /Users/jason/dev/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:244:in `block in log'
    /Users/jason/dev/rails/activesupport/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
    /Users/jason/dev/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:239:in `log'
    /Users/jason/dev/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb:590:in `exec_query'
    /Users/jason/dev/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb:1078:in `select'
    /Users/jason/dev/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:18:in `select_all'
    /Users/jason/dev/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'
    /Users/jason/dev/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:24:in `select_one'
    /Users/jason/dev/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:30:in `select_value'
    /Users/jason/dev/rails/activerecord/lib/active_record/relation/finder_methods.rb:203:in `exists?'
    /Users/jason/dev/rails/activerecord/lib/active_record/validations/uniqueness.rb:33:in `validate_each'
    /Users/jason/dev/rails/activemodel/lib/active_model/validator.rb:153:in `block in validate'
    /Users/jason/dev/rails/activemodel/lib/active_model/validator.rb:150:in `each'
    /Users/jason/dev/rails/activemodel/lib/active_model/validator.rb:150:in `validate'
    /Users/jason/dev/rails/activesupport/lib/active_support/callbacks.rb:302:in `_callback_before_137'
    /Users/jason/dev/rails/activesupport/lib/active_support/callbacks.rb:476:in `_run_validate_callbacks'
    /Users/jason/dev/rails/activesupport/lib/active_support/callbacks.rb:81:in `run_callbacks'
    /Users/jason/dev/rails/activemodel/lib/active_model/validations.rb:212:in `run_validations!'
    /Users/jason/dev/rails/activemodel/lib/active_model/validations/callbacks.rb:53:in `block in run_validations!'
    /Users/jason/dev/rails/activesupport/lib/active_support/callbacks.rb:390:in `_run_validation_callbacks'
    /Users/jason/dev/rails/activesupport/lib/active_support/callbacks.rb:81:in `run_callbacks'
    /Users/jason/dev/rails/activemodel/lib/active_model/validations/callbacks.rb:53:in `run_validations!'
    /Users/jason/dev/rails/activemodel/lib/active_model/validations.rb:179:in `valid?'
    /Users/jason/dev/rails/activerecord/lib/active_record/validations.rb:69:in `valid?'
    /Users/jason/dev/rails/activerecord/lib/active_record/validations.rb:77:in `perform_validations'
    /Users/jason/dev/rails/activerecord/lib/active_record/validations.rb:56:in `save!'
    /Users/jason/dev/rails/activerecord/lib/active_record/attribute_methods/dirty.rb:33:in `save!'
    /Users/jason/dev/rails/activerecord/lib/active_record/transactions.rb:246:in `block in save!'
    /Users/jason/dev/rails/activerecord/lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status'
    /Users/jason/dev/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
    /Users/jason/dev/rails/activerecord/lib/active_record/transactions.rb:208:in `transaction'
    /Users/jason/dev/rails/activerecord/lib/active_record/transactions.rb:293:in `with_transaction_returning_status'
    /Users/jason/dev/rails/activerecord/lib/active_record/transactions.rb:246:in `save!'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/factory_girl-2.0.5/lib/factory_girl/proxy/create.rb:9:in `result'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/factory_girl-2.0.5/lib/factory_girl/factory.rb:93:in `run'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/factory_girl-2.0.5/lib/factory_girl/syntax/methods.rb:54:in `create'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/factory_girl-2.0.5/lib/factory_girl/step_definitions.rb:102:in `block (4 levels) in <top (required)>'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/factory_girl-2.0.5/lib/factory_girl/step_definitions.rb:100:in `each'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/factory_girl-2.0.5/lib/factory_girl/step_definitions.rb:100:in `block (3 levels) in <top (required)>'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/core_ext/instance_exec.rb:48:in `instance_exec'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/core_ext/instance_exec.rb:48:in `block in cucumber_instance_exec'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/core_ext/instance_exec.rb:69:in `cucumber_run_with_backtrace_filtering'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/core_ext/instance_exec.rb:36:in `cucumber_instance_exec'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/rb_support/rb_step_definition.rb:62:in `invoke'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/step_match.rb:25:in `invoke'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/ast/step_invocation.rb:59:in `invoke'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/ast/step_invocation.rb:38:in `accept'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/ast/tree_walker.rb:99:in `block in visit_step'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/ast/tree_walker.rb:163:in `broadcast'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/ast/tree_walker.rb:98:in `visit_step'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/ast/step_collection.rb:15:in `block in accept'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/ast/step_collection.rb:14:in `each'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/ast/step_collection.rb:14:in `accept'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/ast/tree_walker.rb:93:in `block in visit_steps'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/ast/tree_walker.rb:163:in `broadcast'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/ast/tree_walker.rb:92:in `visit_steps'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/ast/scenario.rb:55:in `block (2 levels) in accept'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/runtime.rb:79:in `block (2 levels) in with_hooks'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/runtime.rb:95:in `before_and_after'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/runtime.rb:78:in `block in with_hooks'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/runtime/support_code.rb:120:in `call'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/runtime/support_code.rb:120:in `block (3 levels) in around'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/language_support/language_methods.rb:13:in `block in around'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/language_support/language_methods.rb:95:in `call'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/language_support/language_methods.rb:95:in `execute_around'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/language_support/language_methods.rb:12:in `around'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/runtime/support_code.rb:119:in `block (2 levels) in around'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/runtime/support_code.rb:117:in `call'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/runtime/support_code.rb:117:in `around'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/runtime.rb:90:in `around'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/runtime.rb:77:in `with_hooks'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/ast/scenario.rb:53:in `block in accept'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/ast/scenario.rb:108:in `with_visitor'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/ast/scenario.rb:47:in `accept'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/ast/tree_walker.rb:51:in `block in visit_feature_element'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/ast/tree_walker.rb:163:in `broadcast'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/ast/tree_walker.rb:50:in `visit_feature_element'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/ast/feature.rb:43:in `block in accept'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/ast/feature.rb:42:in `each'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/ast/feature.rb:42:in `accept'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/ast/tree_walker.rb:20:in `block in visit_feature'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/ast/tree_walker.rb:163:in `broadcast'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/ast/tree_walker.rb:19:in `visit_feature'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/ast/features.rb:29:in `block in accept'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/ast/features.rb:17:in `each'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/ast/features.rb:17:in `each'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/ast/features.rb:28:in `accept'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/ast/tree_walker.rb:14:in `block in visit_features'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/ast/tree_walker.rb:163:in `broadcast'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/ast/tree_walker.rb:13:in `visit_features'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/runtime.rb:45:in `run!'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/cli/main.rb:43:in `execute!'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/lib/cucumber/cli/main.rb:20:in `execute'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-1.0.3/bin/cucumber:14:in `<top (required)>'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/bin/cucumber:19:in `load'
    /Users/jason/.rvm/gems/ruby-1.9.2-p290/bin/cucumber:19:in `<main>'
    features/user/edit_patient_worry_score.feature:67:in `Given the following patient exists:'

Duplicate issue #9003: Randomly raised nil.collect! in find_by_sql
Created: 2013-01-20T14:29:50Z
There is a problem while running cucumber features (originally started in cucumber issues). Sometimes ActiveRecord raised nil.collect!.
This problem began after we upgrading from ruby enterprise to 1.9 (currently using 1.9.3-p362). It raised only in test environment, in production and development we don't see it ever.
In all environments used PostgreSQL.
Backtrace chunk from one of the raises:
And I have new user order with line items                  # features/step_definitions/orders_steps.rb:1
  You have a nil object when you didn't expect it!
  You might have expected an instance of Array.
  The error occurred while evaluating nil.collect! (NoMethodError)
  /home/ssherif/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.0.19/lib/active_record/base.rb:473:in `find_by_sql'
  /home/ssherif/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.0.19/lib/active_record/relation.rb:64:in `to_a'
  /home/ssherif/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.0.19/lib/active_record/relation/finder_methods.rb:143:in `all'
  /home/ssherif/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.0.19/lib/active_record/associations/association_collection.rb:70:in `block in find'
  /home/ssherif/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.0.19/lib/active_record/base.rb:1131:in `with_scope'
  /home/ssherif/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.0.19/lib/active_record/associations/association_proxy.rb:212:in `with_scope'
  /home/ssherif/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.0.19/lib/active_record/associations/association_collection.rb:63:in `find'
  /home/ssherif/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.0.19/lib/active_record/associations/association_collection.rb:468:in `find_target'
  /home/ssherif/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.0.19/lib/active_record/associations/association_collection.rb:410:in `load_target'
  /home/ssherif/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.0.19/lib/active_record/associations/association_proxy.rb:117:in `_load'
  /home/ssherif/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.0.19/lib/active_record/associations/association_proxy.rb:124:in `reload'
  /home/ssherif/app/app/models/order.rb:608:in `update_adjustments'
  /home/ssherif/app/app/models/order.rb:252:in `update!'
It's hard to understand what going wrong, but I doubt for query.cache.rb and #cache_sql method, where it's quite possible return nil result instead of Array.
This problem is very similar to #5471.

Closing comment (2013-01-20T22:31:56Z): Seems somewhat related to #2943 and cucumber/cucumber-rails#166 - is this running javascript enabled features?