rails: `ThreadError: can't create Thread: Resource temporarily unavailable` after multiple calls to `Model.establish_connection`
Steps to reproduce
I appreciate that this issue may be filed under “yes, but is this really a problem for anyone” so I have added an ‘Explanation’ section below.
- Use the mysql2 database connection on development and test
- Create a model called
DummyTable
- Create and execute then the following test:
require 'test_helper'
class DummyTableTest < ActiveSupport::TestCase
test "lots of connects and disconnect" do
2000.times do |i|
puts i
Rails.cache.clear
DummyTable.establish_connection :development
Rails.cache.clear
DummyTable.establish_connection :test
end
end
end
Expected behavior
The test runs and switches the database connection back and forth 2000 times. This works in Rails 5.1.
Actual behavior
...
1020
1021
E
Error:
DummyTableTest#test_lots_of_connects_and_disconnect:
ThreadError: can't create Thread: Resource temporarily unavailable
test/models/dummy_table_test.rb:11:in `block (2 levels) in <class:DummyTableTest>'
test/models/dummy_table_test.rb:5:in `times'
test/models/dummy_table_test.rb:5:in `block in <class:DummyTableTest>'
System configuration
Rails version: 5.2.1
Ruby version: 2.5.1
Explanation
In our app we have two databases; the main Rails application database and a read-only database that we are given from other parts of the business. For our tests, for historical (or ‘technical debt’) reasons we need to connect to the read-only database and then, to be able to have reliable test data, we have a test helper that temporarily switch between this database and the test database, where all the tables are mirrored. This helper looks like:
def connect_to db
MODELS_FROM_WEBDB.each { |model|
model.establish_connection db
}
end
def clean_database
MODELS_FROM_WEBDB.each(&:delete_all)
end
def with_clean_test_db
Rails.cache.clear
connect_to :test
clean_database
yield
ensure
clean_database
connect_to :webdb
end
so we can write tests like:
with_clean_test_db do
# Set up test data
# Run tests
end
When we run our tests after upgrading to Rails 5.2.1 (or 5.2.0) we start getting errors consistently after 2034 establish_connection
calls. The errors are actually ActiveRecord::ConnectionNotEstablished: No connection pool with 'Branch' found.
, which is different from what I have reported above, but I have not yet managed to reproduce the ‘no connection pool’ error. I am guessing that there is a resource that does not get cleared down when a database connection is dropped.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 17 (5 by maintainers)
Commits related to this issue
- Applied workaround for issue #2656: Disable new Rails database connection reaping functionality until the Thread-Zombie issue is resolved: https://github.com/rails/rails/issues/33600 . — committed to zammad/zammad by thorsteneckel 5 years ago
- Applied workaround for issue #2656: Disable new Rails database connection reaping functionality until the Thread-Zombie issue is resolved: https://github.com/rails/rails/issues/33600 . — committed to zammad/zammad by thorsteneckel 5 years ago
- Adding reaping frequency of 0 to avoid this error: "ThreadError (can't create Thread: Resource temporarily unavailable)" according to this issue: https://github.com/rails/rails/issues/33600 — committed to barryw/bjr by barryw 4 years ago
If anyone is still facing this issue, I back-ported the fix with this money patch, since setting
reaping_frequency: 0
didn’t help.It worked for me as a temporary solution before updating application to Rails 6.
config/initializers/rails6_backports.rb
This issue has been automatically marked as stale because it has not been commented on for at least three months. The resources of the Rails team are limited, and so we are asking for your help. If you can still reproduce this error on the
6-0-stable
branch or onmaster
, please reply with all of the information you have about it in order to keep the issue open. Thank you for all your contributions.It is failing on the 1021th iteration for revision e559d6dcecb871c174870fdeeed1560a0353cc49 in
5-2-stable
. Thank you very much @jrmhaig for the repository.I can still reproduce this with
5-2-stable
andmaster
, failing my example test on the 2045th iteration in both cases. The top lines of theGemfile.lock
files in each case are:I have put my test example here, mainly for my own reference for the next time that @rails-bot marks this as stale.