aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2016-11-16 05:24:45 +0900
committerRyuta Kamizono <kamipo@gmail.com>2016-11-18 09:59:42 +0900
commit700639e4f6d4d9e9be50fe282756045f9189dd23 (patch)
tree66b712a97947972e3e3ba8d3b96fd0e66cc8f03f /activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
parent3a558aa2bc8ce3834ee79ff3346bcf5d7debbbd0 (diff)
downloadrails-700639e4f6d4d9e9be50fe282756045f9189dd23.tar.gz
rails-700639e4f6d4d9e9be50fe282756045f9189dd23.tar.bz2
rails-700639e4f6d4d9e9be50fe282756045f9189dd23.zip
Fix the race condition caused by `with_new_connections_blocked`
`with_new_connections_blocked` was introduced at #14938. But the method sometimes causes `@new_cons_enabled = false` then never toggled to true.
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb')
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb12
1 files changed, 4 insertions, 8 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
index e6b6b60c1b..ee51419f8d 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
@@ -350,8 +350,7 @@ module ActiveRecord
# currently in the process of independently establishing connections to the DB.
@now_connecting = 0
- # A boolean toggle that allows/disallows new connections.
- @new_cons_enabled = true
+ @new_cons_blocks = 0
@available = ConnectionLeasingQueue.new self
end
@@ -700,13 +699,10 @@ module ActiveRecord
end
def with_new_connections_blocked
- previous_value = nil
- synchronize do
- previous_value, @new_cons_enabled = @new_cons_enabled, false
- end
+ synchronize { @new_cons_blocks += 1 }
yield
ensure
- synchronize { @new_cons_enabled = previous_value }
+ synchronize { @new_cons_blocks -= 1 }
end
# Acquire a connection by one of 1) immediately removing one
@@ -758,7 +754,7 @@ module ActiveRecord
# and increment @now_connecting, to prevent overstepping this pool's @size
# constraint
do_checkout = synchronize do
- if @new_cons_enabled && (@connections.size + @now_connecting) < @size
+ if @new_cons_blocks.zero? && (@connections.size + @now_connecting) < @size
@now_connecting += 1
end
end