Skip to content

Commit

Permalink
Mark global flag for curl isInit as false on Shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyRyabinin committed Jan 4, 2024
1 parent f66839a commit 733b5fd
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
10 changes: 10 additions & 0 deletions src/aws-cpp-sdk-core/source/http/HttpClientFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

#include <aws/core/http/HttpClientFactory.h>
#include <aws/core/internal/AWSHttpResourceClient.h>

#if AWS_SDK_USE_CRT_HTTP
#include <aws/core/http/crt/CRTHttpClient.h>
Expand Down Expand Up @@ -181,15 +182,24 @@ namespace Aws
{
if(GetHttpClientFactory())
{
// EC2 metadata client uses http client from a factory, it will be invalidated
Aws::Internal::CleanupEC2MetadataClient();

GetHttpClientFactory()->CleanupStaticState();
GetHttpClientFactory() = nullptr;
}
}

void SetHttpClientFactory(const std::shared_ptr<HttpClientFactory>& factory)
{
bool recreateEC2Client = Aws::Internal::GetEC2MetadataClient() ? true : false;
CleanupHttp();
GetHttpClientFactory() = factory;

if (recreateEC2Client)
{
Aws::Internal::InitEC2MetadataClient();
}
}

std::shared_ptr<HttpClient> CreateHttpClient(const Aws::Client::ClientConfiguration& clientConfiguration)
Expand Down
10 changes: 8 additions & 2 deletions src/aws-cpp-sdk-core/source/http/curl/CurlHttpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -506,17 +506,23 @@ void CurlHttpClient::InitGlobalState()
<< ", ssl version: " << curlVersionData->ssl_version);
isInit = true;
#ifdef USE_AWS_MEMORY_MANAGEMENT
curl_global_init_mem(CURL_GLOBAL_ALL, &malloc_callback, &free_callback, &realloc_callback, &strdup_callback, &calloc_callback);
CURLcode curlResponseCode = curl_global_init_mem(CURL_GLOBAL_ALL, &malloc_callback, &free_callback, &realloc_callback, &strdup_callback, &calloc_callback);
#else
curl_global_init(CURL_GLOBAL_ALL);
CURLcode curlResponseCode = curl_global_init(CURL_GLOBAL_ALL);
#endif
if (curlResponseCode != CURLE_OK)
{
AWS_LOGSTREAM_FATAL(CURL_HTTP_CLIENT_TAG, "Failed to init curl, return code " << curlResponseCode);
isInit = false;
}
}
}


void CurlHttpClient::CleanupGlobalState()
{
curl_global_cleanup();
isInit = false;
}

Aws::String CurlInfoTypeToString(curl_infotype type)
Expand Down
10 changes: 10 additions & 0 deletions src/aws-cpp-sdk-core/source/internal/AWSHttpResourceClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,20 @@ namespace Aws
AmazonWebServiceResult<Aws::String> AWSHttpResourceClient::GetResourceWithAWSWebServiceResult(const std::shared_ptr<HttpRequest> &httpRequest) const
{
AWS_LOGSTREAM_TRACE(m_logtag.c_str(), "Retrieving credentials from " << httpRequest->GetURIString());
if (!m_httpClient)
{
AWS_LOGSTREAM_FATAL(m_logtag.c_str(), "Unable to get a response: missing http client!");
return {{}, {}, HttpResponseCode::REQUEST_NOT_MADE};
}

for (long retries = 0;; retries++)
{
std::shared_ptr<HttpResponse> response(m_httpClient->MakeRequest(httpRequest));
if (!response)
{
AWS_LOGSTREAM_FATAL(m_logtag.c_str(), "Unable to get a response: http client returned a nullptr!");
return {{}, {}, HttpResponseCode::NO_RESPONSE};
}

if (response->GetResponseCode() == HttpResponseCode::OK)
{
Expand Down
6 changes: 3 additions & 3 deletions tests/aws-cpp-sdk-eventbridge-tests/EventBridgeTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ static std::shared_ptr<Aws::Http::Standard::StandardHttpResponse> buildEventBrid

TEST_F(EventBridgeTests, TestPutEventsBasic)
{
Aws::Client::ClientConfiguration clientConfig;
Aws::Client::ClientConfiguration clientConfig("default", true);
clientConfig.region = "us-east-1";
Aws::Auth::AWSCredentials mockCreds("accessKey", "secretKey", "sessionToken");

Expand All @@ -100,7 +100,7 @@ TEST_F(EventBridgeTests, TestPutEventsBasic)

TEST_F(EventBridgeTests, TestPutEventsMultiRegional)
{
Aws::Client::ClientConfiguration clientConfig;
Aws::Client::ClientConfiguration clientConfig("default", true);
clientConfig.region = "us-east-1";
Aws::Auth::AWSCredentials mockCreds("accessKey", "secretKey", "sessionToken");

Expand Down Expand Up @@ -168,7 +168,7 @@ TEST_F(EventBridgeTests, TestPutEventsEndpointTests)
for(size_t tcIdx = 0; tcIdx < TEST_CASES.size(); ++tcIdx)
{
const EventBridgeEndpointTestCase& testCase = TEST_CASES[tcIdx];
Aws::Client::ClientConfiguration clientConfig;
Aws::Client::ClientConfiguration clientConfig("default", true);
clientConfig.region = testCase.clientRegion;
clientConfig.useDualStack = testCase.useDualStackEndpoint;
clientConfig.useFIPS = testCase.useFipsEndpoint;
Expand Down

0 comments on commit 733b5fd

Please sign in to comment.