aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
diff options
context:
space:
mode:
authorYutaka HARA <yutaka.hara+github@gmail.com>2019-02-16 22:31:15 +0900
committerYutaka HARA <yutaka.hara+github@gmail.com>2019-02-16 22:31:15 +0900
commit20a72627900330cf9c95cf506458cdec4d16a836 (patch)
treef5499de37b85d2a853f4f50c2234346665ac7a78 /activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
parent850e6aaad9c276d1b84708448221eb3becf0b917 (diff)
downloadrails-20a72627900330cf9c95cf506458cdec4d16a836.tar.gz
rails-20a72627900330cf9c95cf506458cdec4d16a836.tar.bz2
rails-20a72627900330cf9c95cf506458cdec4d16a836.zip
Fix possible memory leak of ConnectionHandler
refs #35296
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.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..1c792c76cd 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
+ 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
+ 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