From c5659255bde3f17a8d67ecd329ad4bee1eb25f1b Mon Sep 17 00:00:00 2001 From: Joe Feeney Date: Sat, 11 Mar 2017 11:38:38 -0800 Subject: [PATCH 1/4] Add Aws.empty_connection_pools! API --- aws-sdk-core/lib/aws-sdk-core.rb | 15 +++++++++++++++ aws-sdk-core/spec/aws_spec.rb | 19 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/aws-sdk-core/lib/aws-sdk-core.rb b/aws-sdk-core/lib/aws-sdk-core.rb index ae1da815399..987d74d314b 100644 --- a/aws-sdk-core/lib/aws-sdk-core.rb +++ b/aws-sdk-core/lib/aws-sdk-core.rb @@ -403,6 +403,21 @@ def use_bundled_cert! )) end + # Close any long-lived connections maintained by the SDK's internal + # connection pool. + # + # Applications that rely heavily on the `fork()` system call on POSIX systems + # should call this method in the child process directly after fork to ensure + # there are no race conditions between the parent process and its children + # for the pooled TCP connections. + # + # @return [nil] + def empty_connection_pools! + Seahorse::Client::NetHttp::ConnectionPool.pools.each do |pool| + pool.empty! + end + end + # Loads modules that are normally loaded with Ruby's `autoload`. # This can avoid thread-safety issues that some Ruby versions have # with `autoload`. diff --git a/aws-sdk-core/spec/aws_spec.rb b/aws-sdk-core/spec/aws_spec.rb index fde38d9cd9f..c0c12b63c62 100644 --- a/aws-sdk-core/spec/aws_spec.rb +++ b/aws-sdk-core/spec/aws_spec.rb @@ -138,4 +138,23 @@ module Aws end end + + describe '.empty_connection_pools!' do + it 'close any existing sessions' do + expect_any_instance_of( + Seahorse::Client::NetHttp::ConnectionPool::ExtendedSession + ).to receive(:finish) + + Aws.empty_connection_pools! + end + + it 'clears any pool' do + # ConnectionPool maintains its pool as a hash in a instance variable + expect_any_instance_of( + Hash + ).to receive(:clear) + + Aws.empty_connection_pools! + end + end end From 9fdfb53f0064f05b89535fca52b62351751d0a4d Mon Sep 17 00:00:00 2001 From: Joe Feeney Date: Sun, 12 Mar 2017 11:53:22 -0700 Subject: [PATCH 2/4] modifying tests to checkout travis build under personal account --- aws-sdk-core/spec/aws_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aws-sdk-core/spec/aws_spec.rb b/aws-sdk-core/spec/aws_spec.rb index c0c12b63c62..f5a01e2d590 100644 --- a/aws-sdk-core/spec/aws_spec.rb +++ b/aws-sdk-core/spec/aws_spec.rb @@ -140,10 +140,10 @@ module Aws end describe '.empty_connection_pools!' do - it 'close any existing sessions' do + it 'closes any existing sessions' do expect_any_instance_of( Seahorse::Client::NetHttp::ConnectionPool::ExtendedSession - ).to receive(:finish) + ).to receive(:finish).at_least(:once) Aws.empty_connection_pools! end From 6a867c47deaba845c2b3ecdd2a36508e67ff9dbe Mon Sep 17 00:00:00 2001 From: Joe Feeney Date: Sun, 12 Mar 2017 21:27:14 -0700 Subject: [PATCH 3/4] fix failing tests --- aws-sdk-core/spec/aws_spec.rb | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/aws-sdk-core/spec/aws_spec.rb b/aws-sdk-core/spec/aws_spec.rb index f5a01e2d590..db383d713fb 100644 --- a/aws-sdk-core/spec/aws_spec.rb +++ b/aws-sdk-core/spec/aws_spec.rb @@ -141,20 +141,16 @@ module Aws describe '.empty_connection_pools!' do it 'closes any existing sessions' do - expect_any_instance_of( - Seahorse::Client::NetHttp::ConnectionPool::ExtendedSession - ).to receive(:finish).at_least(:once) + endpoint = "http://example.com" + conn_pool = Seahorse::Client::NetHttp::ConnectionPool.for({}) + session = conn_pool.send(:start_session, endpoint) + conn_pool.instance_variable_get(:@pool)[endpoint] = [session] - Aws.empty_connection_pools! - end - - it 'clears any pool' do - # ConnectionPool maintains its pool as a hash in a instance variable - expect_any_instance_of( - Hash - ).to receive(:clear) + expect(conn_pool.size).to eq(1) Aws.empty_connection_pools! + + expect(conn_pool.size).to eq(0) end end end From 9d6cbd135940e7d296e326c8e854fd9a7fdbcab2 Mon Sep 17 00:00:00 2001 From: Joe Feeney Date: Sun, 19 Mar 2017 10:57:10 -0700 Subject: [PATCH 4/4] updated method documentation --- aws-sdk-core/lib/aws-sdk-core.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/aws-sdk-core/lib/aws-sdk-core.rb b/aws-sdk-core/lib/aws-sdk-core.rb index 987d74d314b..50614fcad42 100644 --- a/aws-sdk-core/lib/aws-sdk-core.rb +++ b/aws-sdk-core/lib/aws-sdk-core.rb @@ -411,6 +411,9 @@ def use_bundled_cert! # there are no race conditions between the parent process and its children # for the pooled TCP connections. # + # Child processes that make multi-threaded calls to the SDK should block on + # this call before beginning work. + # # @return [nil] def empty_connection_pools! Seahorse::Client::NetHttp::ConnectionPool.pools.each do |pool|