From 881740cbc11b1408b0cbf9ca8322a20deaa2a49c Mon Sep 17 00:00:00 2001 From: Alex Woods Date: Wed, 9 Mar 2022 16:05:09 -0800 Subject: [PATCH] Make stubs thread safe by adding a mutex per stubbed request (#2679) --- gems/aws-sdk-core/CHANGELOG.md | 2 ++ gems/aws-sdk-core/lib/aws-sdk-core/client_stubs.rb | 6 +++++- .../aws-sdk-core/lib/aws-sdk-core/plugins/stub_responses.rb | 6 +++++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/gems/aws-sdk-core/CHANGELOG.md b/gems/aws-sdk-core/CHANGELOG.md index 5051c22d8da..29c45b3f4ce 100644 --- a/gems/aws-sdk-core/CHANGELOG.md +++ b/gems/aws-sdk-core/CHANGELOG.md @@ -1,6 +1,8 @@ Unreleased Changes ------------------ +* Issue - Make stubs thread safe by creating new responses for each operation call (#2675). + 3.129.0 (2022-03-08) ------------------ diff --git a/gems/aws-sdk-core/lib/aws-sdk-core/client_stubs.rb b/gems/aws-sdk-core/lib/aws-sdk-core/client_stubs.rb index ea2379be645..c8759098005 100644 --- a/gems/aws-sdk-core/lib/aws-sdk-core/client_stubs.rb +++ b/gems/aws-sdk-core/lib/aws-sdk-core/client_stubs.rb @@ -262,13 +262,17 @@ def apply_stubs(operation_name, stubs) end def convert_stub(operation_name, stub) - case stub + stub = case stub when Proc then stub when Exception, Class then { error: stub } when String then service_error_stub(stub) when Hash then http_response_stub(operation_name, stub) else { data: stub } end + if Hash === stub + stub[:mutex] = Mutex.new + end + stub end def service_error_stub(error_code) diff --git a/gems/aws-sdk-core/lib/aws-sdk-core/plugins/stub_responses.rb b/gems/aws-sdk-core/lib/aws-sdk-core/plugins/stub_responses.rb index dbfe6a05999..fa8cd7f3e34 100644 --- a/gems/aws-sdk-core/lib/aws-sdk-core/plugins/stub_responses.rb +++ b/gems/aws-sdk-core/lib/aws-sdk-core/plugins/stub_responses.rb @@ -51,7 +51,11 @@ def call(context) stub = context.client.next_stub(context) resp = Seahorse::Client::Response.new(context: context) async_mode = context.client.is_a? Seahorse::Client::AsyncBase - apply_stub(stub, resp, async_mode) + if Hash === stub && stub[:mutex] + stub[:mutex].synchronize { apply_stub(stub, resp, async_mode) } + else + apply_stub(stub, resp, async_mode) + end async_mode ? Seahorse::Client::AsyncResponse.new( context: context, stream: context[:input_event_stream_handler].event_emitter.stream, sync_queue: Queue.new) : resp