aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorMatthew Draper <matthew@trebex.net>2016-11-19 20:24:59 +1030
committerMatthew Draper <matthew@trebex.net>2016-11-19 20:33:02 +1030
commitf9230a2d424b177257056be9b9247cbe25caa101 (patch)
treea915ce41c2037febfe4b6c787d83d8c9f4bab696 /activerecord
parentff2fe014d916a2f7c4330ca7208a857bd0ef8992 (diff)
parent700639e4f6d4d9e9be50fe282756045f9189dd23 (diff)
downloadrails-f9230a2d424b177257056be9b9247cbe25caa101.tar.gz
rails-f9230a2d424b177257056be9b9247cbe25caa101.tar.bz2
rails-f9230a2d424b177257056be9b9247cbe25caa101.zip
Merge pull request #27057 from kamipo/fix_race_condition
Fix the race condition caused by `with_new_connections_blocked`
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb13
1 files changed, 7 insertions, 6 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..c9f907b281 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
+ @threads_blocking_new_connections = 0
@available = ConnectionLeasingQueue.new self
end
@@ -700,13 +699,15 @@ module ActiveRecord
end
def with_new_connections_blocked
- previous_value = nil
synchronize do
- previous_value, @new_cons_enabled = @new_cons_enabled, false
+ @threads_blocking_new_connections += 1
end
+
yield
ensure
- synchronize { @new_cons_enabled = previous_value }
+ synchronize do
+ @threads_blocking_new_connections -= 1
+ end
end
# Acquire a connection by one of 1) immediately removing one
@@ -758,7 +759,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 @threads_blocking_new_connections.zero? && (@connections.size + @now_connecting) < @size
@now_connecting += 1
end
end