From 54ab29da20a44ba90589d27df107d8a162431ca1 Mon Sep 17 00:00:00 2001 From: Joel Cornelius Date: Wed, 19 Feb 2025 13:15:16 -0600 Subject: [PATCH 1/2] Do not use connection-pool in single-threaded process --- .../client/net_http/connection_pool.rb | 56 +++++++++++++------ 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/gems/aws-sdk-core/lib/seahorse/client/net_http/connection_pool.rb b/gems/aws-sdk-core/lib/seahorse/client/net_http/connection_pool.rb index 36a328c4d17..20176743e8a 100644 --- a/gems/aws-sdk-core/lib/seahorse/client/net_http/connection_pool.rb +++ b/gems/aws-sdk-core/lib/seahorse/client/net_http/connection_pool.rb @@ -17,9 +17,11 @@ module Client module NetHttp class ConnectionPool + if Thread.list.select {|thread| thread.status == "run"}.count > 1 + @pools_mutex = Mutex.new + @pools = {} + end - @pools_mutex = Mutex.new - @pools = {} @default_logger = Logger.new($stdout) OPTIONS = { @@ -45,8 +47,10 @@ def initialize(options = {}) value = options[opt_name].nil? ? default_value : options[opt_name] instance_variable_set("@#{opt_name}", value) end - @pool_mutex = Mutex.new - @pool = {} + if @pools_mutex + @pool_mutex = Mutex.new + @pool = {} + end end OPTIONS.keys.each do |attr_name| @@ -91,10 +95,12 @@ def session_for(endpoint, &block) session = nil # attempt to recycle an already open session - @pool_mutex.synchronize do - _clean - if @pool.key?(endpoint) - session = @pool[endpoint].shift + if @pool_mutex + @pool_mutex.synchronize do + _clean + if @pool.key?(endpoint) + session = @pool[endpoint].shift + end end end @@ -108,10 +114,14 @@ def session_for(endpoint, &block) session.finish if session raise else - # No error raised? Good, check the session into the pool. - @pool_mutex.synchronize do - @pool[endpoint] = [] unless @pool.key?(endpoint) - @pool[endpoint] << session + if @pool_mutex + # No error raised? Good, check the session into the pool. + @pool_mutex.synchronize do + @pool[endpoint] = [] unless @pool.key?(endpoint) + @pool[endpoint] << session + end + else + session.finish end end nil @@ -120,6 +130,8 @@ def session_for(endpoint, &block) # @return [Integer] Returns the count of sessions currently in the # pool, not counting those currently in use. def size + return 0 unless @pool_mutex + @pool_mutex.synchronize do @pool.values.flatten.size end @@ -129,7 +141,7 @@ def size # the idle timeout). # @return [nil] def clean! - @pool_mutex.synchronize { _clean } + @pool_mutex.synchronize { _clean } if @pool_mutex nil end @@ -139,9 +151,11 @@ def clean! # state. # @return [nil] def empty! - @pool_mutex.synchronize do - @pool.values.flatten.map(&:finish) - @pool.clear + if @pool_mutex + @pool_mutex.synchronize do + @pool.values.flatten.map(&:finish) + @pool.clear + end end nil end @@ -214,14 +228,20 @@ class << self # @return [ConnectionPool] def for options = {} options = pool_options(options) - @pools_mutex.synchronize do - @pools[options] ||= new(options) + if @pools_mutex + @pools_mutex.synchronize do + @pools[options] ||= new(options) + end + else + new(options) end end # @return [Array] Returns a list of the # constructed connection pools. def pools + return [] unless @pools_mutex + @pools_mutex.synchronize do @pools.values end From aad9fdf291e95d748eca853d8615f2ed1834b383 Mon Sep 17 00:00:00 2001 From: Joel Cornelius Date: Wed, 19 Feb 2025 13:21:38 -0600 Subject: [PATCH 2/2] add to CHANGELOG --- gems/aws-sdk-core/CHANGELOG.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/gems/aws-sdk-core/CHANGELOG.md b/gems/aws-sdk-core/CHANGELOG.md index 599deac066e..0ad37554b00 100644 --- a/gems/aws-sdk-core/CHANGELOG.md +++ b/gems/aws-sdk-core/CHANGELOG.md @@ -1,6 +1,10 @@ Unreleased Changes ------------------ +3.219.1 (2025-02-19) +------------------ +* Issue - Do not use connection-pooling in single-threaded processes. + 3.219.0 (2025-02-18) ------------------ @@ -41,7 +45,7 @@ Unreleased Changes * Issue - Use epoch seconds instead of milliseconds in cbor encode/decode. -* Issue - Add handling of block in response delegation (#3169). +* Issue - Add handling of block in response delegation (#3169). 3.216.0 (2025-01-15) ------------------