aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2019-02-16 23:10:42 +0900
committerRyuta Kamizono <kamipo@gmail.com>2019-02-16 23:11:27 +0900
commitfed7888c83183892f5a7ceb5df40004df7ad5b06 (patch)
tree94985c51e5ab27a35d2f272110810cc02230e371 /activerecord
parentfdc05952bd667e37dc0c1326c645a3dd65d450db (diff)
parent20a72627900330cf9c95cf506458cdec4d16a836 (diff)
downloadrails-fed7888c83183892f5a7ceb5df40004df7ad5b06.tar.gz
rails-fed7888c83183892f5a7ceb5df40004df7ad5b06.tar.bz2
rails-fed7888c83183892f5a7ceb5df40004df7ad5b06.zip
Merge pull request #35297 from yhara/fix-ar-connection-handler-leak
Fix possible memory leak of ConnectionHandler
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb18
1 files changed, 11 insertions, 7 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 1c8df3c08a..0ded1a5318 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
@@ -915,6 +915,16 @@ module ActiveRecord
# about the model. The model needs to pass a specification name to the handler,
# in order to look up the correct connection pool.
class ConnectionHandler
+ def self.create_owner_to_pool # :nodoc:
+ Concurrent::Map.new(initial_capacity: 2) do |h, k|
+ # Discard the parent's connection pools immediately; we have no need
+ # of them
+ discard_unowned_pools(h)
+
+ h[k] = Concurrent::Map.new(initial_capacity: 2)
+ end
+ end
+
def self.unowned_pool_finalizer(pid_map) # :nodoc:
lambda do |_|
discard_unowned_pools(pid_map)
@@ -929,13 +939,7 @@ module ActiveRecord
def initialize
# These caches are keyed by spec.name (ConnectionSpecification#name).
- @owner_to_pool = Concurrent::Map.new(initial_capacity: 2) do |h, k|
- # Discard the parent's connection pools immediately; we have no need
- # of them
- ConnectionHandler.discard_unowned_pools(h)
-
- h[k] = Concurrent::Map.new(initial_capacity: 2)
- end
+ @owner_to_pool = ConnectionHandler.create_owner_to_pool
# Backup finalizer: if the forked child never needed a pool, the above
# early discard has not occurred