rails: Tests cause ThreadError: already initialized with Ruby 2.6.0
Steps to reproduce
Test like this causes ThreadError: already initialized
after updating Ruby 2.6.0.
class EmployeesControllerTest < ActionController::TestCase
def test_list_markup
get :list
assert_response(:success)
end
...
end
EmployeesControllerTest#test_edit_markup:
ThreadError: already initialized
/home/shirosaki/.rbenv/versions/2.6.0/lib/ruby/2.6.0/monitor.rb:259:in `mon_initialize'
/home/shirosaki/.rbenv/versions/2.6.0/lib/ruby/2.6.0/monitor.rb:252:in `initialize'
/home/shirosaki/.rbenv/versions/2.6.0/lib/ruby/gems/2.6.0/gems/actionpack-4.2.11/lib/action_dispatch/http/response.rb:119:in `initialize'
/home/shirosaki/.rbenv/versions/2.6.0/lib/ruby/gems/2.6.0/gems/actionpack-4.2.11/lib/action_controller/test_case.rb:277:in `recycle!'
/home/shirosaki/.rbenv/versions/2.6.0/lib/ruby/gems/2.6.0/gems/actionpack-4.2.11/lib/action_controller/test_case.rb:617:in `process'
/home/shirosaki/.rbenv/versions/2.6.0/lib/ruby/gems/2.6.0/gems/actionpack-4.2.11/lib/action_controller/test_case.rb:67:in `process'
/home/shirosaki/.rbenv/versions/2.6.0/lib/ruby/gems/2.6.0/gems/actionpack-4.2.11/lib/action_controller/test_case.rb:514:in `get'
/home/shirosaki/work/master/test/functional/employees_controller_test.rb:24:in `test_edit_markup'
Expected behavior
Not raise ThreadError.
Actual behavior
Raise ThreadError.
System configuration
Rails version: 4.2.11 Ruby version: 2.6.0
The error would be related to this issue. https://bugs.ruby-lang.org/issues/15000
If I remove raise ThreadError, "already initialized"
in lib/monitor.rb
, tests pass.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 11
- Comments: 21 (11 by maintainers)
Commits related to this issue
- CI: allow ruby 2.6 and rails 4.2 failures ¯\_(ツ)_/¯ (https://github.com/rails/rails/issues/34790) — committed to markets/maily by markets 6 years ago
- Patch for Ruby 2.6 + Rails 4.2 @see https://github.com/rails/rails/issues/34790 — committed to doorkeeper-gem/doorkeeper by nbulaj 5 years ago
- Add a monkey patch to pass specs on Ruby 2.6 & Rails 4.2 This original patch come from https://github.com/rails/rails/issues/34790#issuecomment-450502805 — committed to tricknotes/view_source_map by tricknotes 5 years ago
- Add a monkey patch to pass specs on Ruby 2.6 & Rails 4.2 This original patch come from https://github.com/rails/rails/issues/34790#issuecomment-450502805 — committed to tricknotes/view_source_map by tricknotes 5 years ago
- Fix CI - Install pre-2.0 version of bundler - Bundler 2.0 does not support older versions of Ruby - Update Ruby versions - Rails 4.2 has a known issue with Ruby 2.6: https://github.com/rails/... — committed to apokalipto/devise_saml_authenticatable by adamstegman 5 years ago
- Monkeypath ActionController::TestResponse See https://github.com/rails/rails/issues/34790 — committed to europeana/europeana-portal-collections by rwd 5 years ago
- travis: exclude rails 4.2 and ruby 2.6 there is a known issue with tests raising a thread error and won't be fixed https://github.com/rails/rails/issues/34790 — committed to matthewrudy/responders by matthewrudy 5 years ago
- travis: exclude rails 4.2 and ruby 2.6 there is a known issue with tests raising a thread error and won't be fixed https://github.com/rails/rails/issues/34790 — committed to matthewrudy/responders by matthewrudy 5 years ago
- ruby 2.6 + rails 4.2 monkeypatch to make CI green (https://github.com/rails/rails/issues/34790) — committed to markets/maily by markets 5 years ago
- Patch Rails 4.2 tests to work with Ruby 2.6. An issue with reusing the `TestResponse` object occurs with Ruby 2.6 and causes `already initialized` exceptions to be thrown. This monkey-patch fixes th... — committed to thoughtbot/administrate by nickcharlton 5 years ago
- Do not test against Rails 4.2 if Ruby version is 2.6.x Rails 4.2 raises a ThreadError with Ruby 2.6 and higher, as documented in: https://github.com/rails/rails/issues/34790 — committed to carlesjove/clearance by carlesjove 5 years ago
- Convert all controller specs to request specs Controller specs under Rails 4.2 and Ruby 2.6 experience a ThreadError. This won't be fixed as 4.2 is currently receiving severe security fixes only. See... — committed to moneyadviceservice/rad by malcolmbaig 5 years ago
- Fix verify pipeline oc-id tests oc-id is built on rails 4.2.11 which fails tests with: `ThreadError: already initialized` with Ruby 2.6.x This commit adds src/oc-id/spec/support/monkeypatch.rb to p... — committed to chef/chef-server by snapp 5 years ago
- Fix verify pipeline oc-id tests oc-id is built on rails 4.2.11 which fails tests with: `ThreadError: already initialized` with Ruby 2.6.x This commit adds src/oc-id/spec/support/monkeypatch.rb to p... — committed to chef/chef-server by snapp 5 years ago
- Patch Rails 4.2 tests to work with Ruby 2.6. An issue with reusing the `TestResponse` object occurs with Ruby 2.6 and causes `already initialized` exceptions to be thrown. This monkey-patch fixes th... — committed to svqualitydev/admin-cms by deleted user 5 years ago
- Document that Ruby 2.6 is not supported Rails 4.2 doesn't support Ruby 2.6 without some monkey patching, see rails/rails#34790. — committed to concerto/concerto by bamnet 5 years ago
- patch for Rails 4.2 and Ruby 2.6 Thread error only for this combo Reference: https://github.com/rails/rails/issues/34790 — committed to zendesk/charcoal by icorson3 4 years ago
- patch for Rails 4.2 and Ruby 2.6 Thread error only for this combo Reference: https://github.com/rails/rails/issues/34790 — committed to zendesk/charcoal by icorson3 4 years ago
- add a monkeypath to solve https://github.com/rails/rails/issues/34790 — committed to ngarbezza/pim-pam-pum-backend by ngarbezza 4 years ago
- downgrade to ruby-2.6.6 error: ThreadError: already initialized fix: https://github.com/rails/rails/issues/34790#issuecomment-450502805 issue: this fix works on ruby 2.6 but not 2.7 decision: downgra... — committed to BFNZ/bfnz by ducktyper 4 years ago
@markets this is caused by rails reusing
ActionController::TestResponse
object in tests, can be fixed by this monkeypatch:The summary is, there are 2 preconditions here, that if satisfied lead to the error. Rails < 5 reuses (recycles)
ActionController::TestResponse
objects in tests. Starting with Ruby 2.6.0MonitorMixin
has a guard code that raises an exception if itsinitialize
method is called twice. And in Ruby 2.7.0 they renamed the@mon_mutex*
variables.So the revised workaround is:
Although, I’m considering switching to Ruby 2.6.0, since with Rails 4 and Ruby 2.7.0 the tests’ output is swarmed with warnings.
I would avoid using
config/initializers
and opt for adding something tospec/support
as @Vasfed has suggested as this will ensure load orders are not compromised. Using an initializer did not work for me due to some libraries being loaded later than the initializer.In case there are still poor souls on Rails 4.2 with Ruby 2.6/2.7, this is the full file working for my team, required from
rails_helper.rb
.If you use streaming/live stuff, you’ll also need
The Rails convention for files like this isconfig/initializers
. Thats where I put mine and I called itmonkey_patches.rb
Edit: Since this issue only appears to affect the
test
environment, the “best” place to put a monkey patch like this in inspec/support
. You may need torequire
it, you may not. It depends entirely on the contents ofrails_helper.rb
orspec_helper.rb
(depending on your RSpec version!).Thank you @mattheworiordan for the correction.
@timomitchel somewhere in your test suite setup - test_helper/rails_helper or a separate file that is required from there (in
test/support
orspec/support
depending on which test framework you’re using) Point is that for patch to work, it should have been run before controller testsThanks for your information.
As described above, 4.2 only support severe security issues.
If this only occurs in 4.2, we do nothing. So I close. Thanks.
@Vasfed thanks for the workaround 👍, I think a lot of people will need it (or something smilar at least) to continue supporting rails the 4.2 + ruby 2.6 combo in their CIs, I was allowing failures, but this is of course not ideal: https://github.com/markets/maily/commit/9857a98b5f646413c5743b779c9730bcd99a2a2e
Hi, @markets I checked maily’s CI. It seems tests were already failed in 1 week ago. https://travis-ci.org/markets/maily/jobs/470573918#L556-L563
It seems that it was allowed failures. https://travis-ci.org/markets/maily/builds/470573898