mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
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.
This commit is contained in:
parent
3a558aa2bc
commit
700639e4f6
1 changed files with 4 additions and 8 deletions
|
@ -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.
|
@new_cons_blocks = 0
|
||||||
@new_cons_enabled = true
|
|
||||||
|
|
||||||
@available = ConnectionLeasingQueue.new self
|
@available = ConnectionLeasingQueue.new self
|
||||||
end
|
end
|
||||||
|
@ -700,13 +699,10 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def with_new_connections_blocked
|
def with_new_connections_blocked
|
||||||
previous_value = nil
|
synchronize { @new_cons_blocks += 1 }
|
||||||
synchronize do
|
|
||||||
previous_value, @new_cons_enabled = @new_cons_enabled, false
|
|
||||||
end
|
|
||||||
yield
|
yield
|
||||||
ensure
|
ensure
|
||||||
synchronize { @new_cons_enabled = previous_value }
|
synchronize { @new_cons_blocks -= 1 }
|
||||||
end
|
end
|
||||||
|
|
||||||
# Acquire a connection by one of 1) immediately removing one
|
# 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
|
# 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 @new_cons_blocks.zero? && (@connections.size + @now_connecting) < @size
|
||||||
@now_connecting += 1
|
@now_connecting += 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue