From fa14976a5d53c451b4400c9ddbfc29a5103228d7 Mon Sep 17 00:00:00 2001
From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com>
Date: Sun, 30 Apr 2023 15:45:02 +0900
Subject: [PATCH] Bump connection_pool from 2.3.0 to 2.4.0 (#24290)

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Claire <claire.github-309c@sitedethib.com>
---
 app/lib/connection_pool/shared_connection_pool.rb | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/app/lib/connection_pool/shared_connection_pool.rb b/app/lib/connection_pool/shared_connection_pool.rb
index 2865a4108..741529652 100644
--- a/app/lib/connection_pool/shared_connection_pool.rb
+++ b/app/lib/connection_pool/shared_connection_pool.rb
@@ -37,7 +37,17 @@ class ConnectionPool::SharedConnectionPool < ConnectionPool
   end
 
   def checkin(preferred_tag)
-    if ::Thread.current[key(preferred_tag)]
+    if preferred_tag.is_a?(Hash) && preferred_tag[:force]
+      # ConnectionPool 2.4+ calls `checkin(force: true)` after fork.
+      # When this happens, we should remove all connections from Thread.current
+
+      ::Thread.current.keys.each do |name| # rubocop:disable Style/HashEachMethods
+        next unless name.to_s.start_with?("#{@key}-")
+
+        @available.push(::Thread.current[name])
+        ::Thread.current[name] = nil
+      end
+    elsif ::Thread.current[key(preferred_tag)]
       if ::Thread.current[key_count(preferred_tag)] == 1
         @available.push(::Thread.current[key(preferred_tag)])
         ::Thread.current[key(preferred_tag)] = nil