Mysql2::Error: # MySQL client is not connected — Rails 6.0.2.2 with rspec; Ruby 2.6.4 mac os Catalina + Mojave

Viewed 5536

I'm getting this extremely strange intermittent problem with MySQL on Rails 6.0.2.2, RSpec 4.0.0.rc1, Ruby 2.6.4

on a two separate Macs, one is running macOS Mojave and the other is running MacOS Catalina

Interestingly, the mac running Catalina appears to do it less often, although it still does this. the failure is intermittent, seems to happen immediately after another failure in the suite, but then often continues to show up (across about half of the test suite) on subsequent runs, even if I've fixed the originally failing spec.

  1) Template 
     Failure/Error: _query(sql, @query_options.merge(options))

     ActiveRecord::StatementInvalid:
       Mysql2::Error: MySQL client is not connected
     # /Users/jason/.rvm/gems/ruby-2.6.4/gems/mysql2-0.5.3/lib/mysql2/client.rb:131:in `_query'
     # /Users/jason/.rvm/gems/ruby-2.6.4/gems/mysql2-0.5.3/lib/mysql2/client.rb:131:in `block in query'
     # /Users/jason/.rvm/gems/ruby-2.6.4/gems/mysql2-0.5.3/lib/mysql2/client.rb:130:in `handle_interrupt'
     # /Users/jason/.rvm/gems/ruby-2.6.4/gems/mysql2-0.5.3/lib/mysql2/client.rb:130:in `query'
     # /Users/jason/.rvm/gems/ruby-2.6.4/gems/rspec-wait-0.0.9/lib/rspec/wait.rb:46:in `block (2 levels) in <top (required)>'
     # ------------------
     # --- Caused by: ---
     # Mysql2::Error:
     #   MySQL client is not connected
     #   /Users/jason/.rvm/gems/ruby-2.6.4/gems/mysql2-0.5.3/lib/mysql2/client.rb:131:in `_query'

2 Answers

I began having this problem when I set up my personal laptop (Mac OS Catalina) to do work from home. In the process, I updated our environment from Ruby 2.4.7 to 2.6.3 and gems system from 3.0.6 to 3.1.2. And, no doubt, incremental updates to our bundled gems also happened. I have not been able to identify which change resulted in the new, seemingly random and intermittent failures.

Google led me here first, and second to this discussion on a minitest issue: https://github.com/seattlerb/minitest/issues/744

Then, looking closer at my tests, I noticed that the first test to fail always triggered an async method.

I added this to my config/environments/test.rb file:

config.active_job.queue_adapter = :inline

And all tests are passing again.

I don't know if this constitutes a fix or a work around. I'm not sure if asynchronous processes were working before or if they were previously being handled inline and a default behavior changed. The tests we currently have aren't specifically validating asynchronicity. So, for the moment, this solved my problem.

In my case I had issues while running tests for ActiveJob background jobs, and adding config.active_job.queue_adapter = :test to config/environments/test.rb worked for me.

Related