1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #27057 from kamipo/fix_race_condition

Fix the race condition caused by `with_new_connections_blocked`
This commit is contained in:
Matthew Draper 2016-11-19 20:24:59 +10:30
commit f9230a2d42

View file

@ -350,8 +350,7 @@ module ActiveRecord
# currently in the process of independently establishing connections to the DB. # currently in the process of independently establishing connections to the DB.
@now_connecting = 0 @now_connecting = 0
# A boolean toggle that allows/disallows new connections. @threads_blocking_new_connections = 0
@new_cons_enabled = true
@available = ConnectionLeasingQueue.new self @available = ConnectionLeasingQueue.new self
end end
@ -700,13 +699,15 @@ module ActiveRecord
end end
def with_new_connections_blocked def with_new_connections_blocked
previous_value = nil
synchronize do synchronize do
previous_value, @new_cons_enabled = @new_cons_enabled, false @threads_blocking_new_connections += 1
end end
yield yield
ensure ensure
synchronize { @new_cons_enabled = previous_value } synchronize do
@threads_blocking_new_connections -= 1
end
end end
# Acquire a connection by one of 1) immediately removing one # 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 # and increment @now_connecting, to prevent overstepping this pool's @size
# constraint # constraint
do_checkout = synchronize do 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 @now_connecting += 1
end end
end end