Repository navigation
GH-50188: [C++][Python] Release S3 credentials providers in FinalizeS3 #55881
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
raulcd
merged 1 commit into
apache:main
from
jpatra72:GH-50188-finalize-credentials-providers
Dec 3, 2026
+105
−4
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -226,6 +226,14 @@ std::shared_ptr<S3RetryStrategy> S3RetryStrategy::GetAwsStandardRetryStrategy( | |||||||||||
| static_cast<long>(max_attempts))); // NOLINT: runtime/int | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| namespace { | ||||||||||||
|
|
||||||||||||
| // Defined below, next to S3ClientFinalizer. | ||||||||||||
| std::shared_ptr<Aws::Auth::AWSCredentialsProvider> MakeFinalizableCredentialsProvider( | ||||||||||||
| std::shared_ptr<Aws::Auth::AWSCredentialsProvider> provider); | ||||||||||||
|
|
||||||||||||
| } // namespace | ||||||||||||
|
|
||||||||||||
| // ----------------------------------------------------------------------- | ||||||||||||
| // S3Options implementation | ||||||||||||
|
|
||||||||||||
|
|
@@ -234,8 +242,8 @@ S3Options::S3Options() { | |||||||||||
| } | ||||||||||||
|
|
||||||||||||
| void S3Options::ConfigureDefaultCredentials() { | ||||||||||||
| credentials_provider = | ||||||||||||
| std::make_shared<Aws::Auth::DefaultAWSCredentialsProviderChain>(); | ||||||||||||
| credentials_provider = MakeFinalizableCredentialsProvider( | ||||||||||||
| std::make_shared<Aws::Auth::DefaultAWSCredentialsProviderChain>()); | ||||||||||||
| credentials_kind = S3CredentialsKind::Default; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
|
|
@@ -266,8 +274,8 @@ void S3Options::ConfigureAssumeRoleWithWebIdentityCredentials() { | |||||||||||
| // The AWS SDK uses environment variables AWS_DEFAULT_REGION, | ||||||||||||
| // AWS_ROLE_ARN, AWS_WEB_IDENTITY_TOKEN_FILE and AWS_ROLE_SESSION_NAME | ||||||||||||
| // to configure the required credentials | ||||||||||||
| credentials_provider = | ||||||||||||
| std::make_shared<Aws::Auth::STSAssumeRoleWebIdentityCredentialsProvider>(); | ||||||||||||
| credentials_provider = MakeFinalizableCredentialsProvider( | ||||||||||||
| std::make_shared<Aws::Auth::STSAssumeRoleWebIdentityCredentialsProvider>()); | ||||||||||||
| credentials_kind = S3CredentialsKind::WebIdentity; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
|
|
@@ -900,8 +908,47 @@ class S3ClientHolder { | |||||||||||
| std::shared_ptr<S3Client> client_; | ||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
| // Credentials provider wrapper whose underlying provider is released by | ||||||||||||
| // FinalizeS3(), even if S3Options copies outlive it. CRT-based providers | ||||||||||||
| // (e.g. STS web identity) hold a reference to the default ClientBootstrap | ||||||||||||
| // and would otherwise make Aws::ShutdownAPI() block forever (GH-50188). | ||||||||||||
| class FinalizableCredentialsProvider : public Aws::Auth::AWSCredentialsProvider { | ||||||||||||
| public: | ||||||||||||
| explicit FinalizableCredentialsProvider( | ||||||||||||
| std::shared_ptr<Aws::Auth::AWSCredentialsProvider> provider) | ||||||||||||
| : provider_(std::move(provider)) {} | ||||||||||||
|
|
||||||||||||
| Aws::Auth::AWSCredentials GetAWSCredentials() override { | ||||||||||||
| std::shared_ptr<Aws::Auth::AWSCredentialsProvider> provider; | ||||||||||||
| { | ||||||||||||
| std::unique_lock lock(mutex_); | ||||||||||||
| provider = provider_; | ||||||||||||
| } | ||||||||||||
| if (!provider) { | ||||||||||||
| // S3 was finalized | ||||||||||||
| return {}; | ||||||||||||
| } | ||||||||||||
| return provider->GetAWSCredentials(); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| void Finalize() { | ||||||||||||
| std::shared_ptr<Aws::Auth::AWSCredentialsProvider> provider; | ||||||||||||
| { | ||||||||||||
| std::unique_lock lock(mutex_); | ||||||||||||
| provider = std::move(provider_); | ||||||||||||
| } | ||||||||||||
| // Do not hold mutex while the provider destructor potentially runs | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| private: | ||||||||||||
| std::mutex mutex_; | ||||||||||||
| std::shared_ptr<Aws::Auth::AWSCredentialsProvider> provider_; | ||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
| class S3ClientFinalizer : public std::enable_shared_from_this<S3ClientFinalizer> { | ||||||||||||
| using ClientHolderList = std::vector<std::weak_ptr<S3ClientHolder>>; | ||||||||||||
| using CredentialsProviderList = | ||||||||||||
| std::vector<std::weak_ptr<FinalizableCredentialsProvider>>; | ||||||||||||
|
|
||||||||||||
| public: | ||||||||||||
| Result<std::shared_ptr<S3ClientHolder>> AddClient(std::shared_ptr<S3Client> client) { | ||||||||||||
|
|
@@ -921,11 +968,27 @@ class S3ClientFinalizer : public std::enable_shared_from_this<S3ClientFinalizer> | |||||||||||
| return holder; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| void AddCredentialsProvider( | ||||||||||||
| const std::shared_ptr<FinalizableCredentialsProvider>& provider) { | ||||||||||||
| std::unique_lock lock(mutex_); | ||||||||||||
| if (finalized_) { | ||||||||||||
| lock.unlock(); | ||||||||||||
| provider->Finalize(); | ||||||||||||
| return; | ||||||||||||
| } | ||||||||||||
| auto end = std::remove_if( | ||||||||||||
| providers_.begin(), providers_.end(), | ||||||||||||
| [](std::weak_ptr<FinalizableCredentialsProvider> p) { return p.expired(); }); | ||||||||||||
| providers_.erase(end, providers_.end()); | ||||||||||||
|
Comment on lines
+979
to
+982
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I think this could be simplified as we required C++ 20 now but we would require
Suggested change
|
||||||||||||
| providers_.emplace_back(provider); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| void Finalize() { | ||||||||||||
| std::unique_lock lock(mutex_); | ||||||||||||
| finalized_ = true; | ||||||||||||
|
|
||||||||||||
| ClientHolderList finalizing = std::move(holders_); | ||||||||||||
| CredentialsProviderList finalizing_providers = std::move(providers_); | ||||||||||||
| lock.unlock(); // avoid lock ordering issue with S3ClientHolder::Finalize | ||||||||||||
|
|
||||||||||||
| // Finalize all client holders, such that no S3Client remains alive | ||||||||||||
|
|
@@ -936,6 +999,14 @@ class S3ClientFinalizer : public std::enable_shared_from_this<S3ClientFinalizer> | |||||||||||
| holder->Finalize(); | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| // Then release the credentials providers, which S3Options instances | ||||||||||||
| // may still reference (GH-50188). | ||||||||||||
| for (auto&& weak_provider : finalizing_providers) { | ||||||||||||
| auto provider = weak_provider.lock(); | ||||||||||||
| if (provider) { | ||||||||||||
| provider->Finalize(); | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| auto LockShared() { return std::shared_lock(mutex_); } | ||||||||||||
|
|
@@ -945,6 +1016,7 @@ class S3ClientFinalizer : public std::enable_shared_from_this<S3ClientFinalizer> | |||||||||||
|
|
||||||||||||
| std::shared_mutex mutex_; | ||||||||||||
| ClientHolderList holders_; | ||||||||||||
| CredentialsProviderList providers_; | ||||||||||||
| bool finalized_ = false; | ||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
|
|
@@ -1002,6 +1074,13 @@ Result<std::shared_ptr<S3ClientHolder>> GetClientHolder( | |||||||||||
| return GetClientFinalizer()->AddClient(std::move(client)); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| std::shared_ptr<Aws::Auth::AWSCredentialsProvider> MakeFinalizableCredentialsProvider( | ||||||||||||
| std::shared_ptr<Aws::Auth::AWSCredentialsProvider> provider) { | ||||||||||||
| auto wrapped = std::make_shared<FinalizableCredentialsProvider>(std::move(provider)); | ||||||||||||
| GetClientFinalizer()->AddCredentialsProvider(wrapped); | ||||||||||||
| return wrapped; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| // ----------------------------------------------------------------------- | ||||||||||||
| // S3 client factory: build S3Client from S3Options | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: maybe a comment so intent is clear.