Repository navigation
GH-50188: [C++][Python] Release S3 credentials providers in FinalizeS3 - #55881
Conversation
…alizeS3 CRT-based credentials providers (e.g. STS web identity, since AWS SDK 1.11.778) hold a reference to the default ClientBootstrap. S3Options copies kept alive by a live S3FileSystem would keep such a provider alive past FinalizeS3(), making Aws::ShutdownAPI() block forever. Wrap the providers created by S3Options::ConfigureDefaultCredentials() and ConfigureAssumeRoleWithWebIdentityCredentials() so that S3ClientFinalizer can release them after finalizing the S3 clients.
|
Unfortunately the C++ macOS failures are timing out on s3fs tests and those tests are not timing out on main, I am going to re-run them but we should probably investigate they are not related: |
|
Might be an unrelated issue I've seen a macOS JNI job failing on a different PR with s3 timeouts too. |
|
@github-actions crossbow submit wheel-cp314 |
|
Revision: 984d93c Submitted crossbow builds: ursacomputing/crossbow @ actions-120902d990 |
| std::shared_ptr<Aws::Auth::AWSCredentialsProvider> provider; | ||
| { | ||
| std::unique_lock lock(mutex_); | ||
| provider = std::move(provider_); |
There was a problem hiding this comment.
nit: maybe a comment so intent is clear.
| provider = std::move(provider_); | |
| // Don't reset as provider's destructor could run while mutex is locked and could deadlock | |
| provider = std::move(provider_); |
| auto end = std::remove_if( | ||
| providers_.begin(), providers_.end(), | ||
| [](std::weak_ptr<FinalizableCredentialsProvider> p) { return p.expired(); }); | ||
| providers_.erase(end, providers_.end()); |
There was a problem hiding this comment.
nit: I think this could be simplified as we required C++ 20 now but we would require #include <vector> too. We can follow up this simplification pattern afterwards as we could do with client too and this PR shouldn't grow more.
| auto end = std::remove_if( | |
| providers_.begin(), providers_.end(), | |
| [](std::weak_ptr<FinalizableCredentialsProvider> p) { return p.expired(); }); | |
| providers_.erase(end, providers_.end()); | |
| std::erase_if(providers_, [](const auto& p) { return p.expired(); }); |
|
Thanks for investigating the fix @jpatra72 . I am merging! |
Rationale for this change
Since Arrow 24,
finalize_s3()hangs forever if anS3FileSystemusing web identity credentials is still alive, and so does interpreter exit. Web identity credentials meansAWS_WEB_IDENTITY_TOKEN_FILE+AWS_ROLE_ARN, e.g. EKS IRSA. The issue has a deterministic repro using a bogus token.This is a lifetime issue, not a race:
STSAssumeRoleWebIdentityCredentialsProviderbecame aCrtCredentialsProvider. It's created withGetDefaultClientBootstrap(), so while it is alive it holds a reference to the defaultClientBootstrap. In 1.11.587, used by Arrow 23, it usedSTSCredentialsClientwithout CRT.S3ClientFinalizerreleases allS3Clients beforeAws::ShutdownAPI(), but the credentials provider is also owned byS3Options::credentials_providerandClientBuilder::credentials_provider_. A liveS3FileSystemkeeps it alive, soShutdownAPI()waits forever for the bootstrap reference to go away.This is also why removing
EnableBlockingShutdown()(#50193) is not enough. The wait moves to~ApiHandle→aws_thread_join_all_managed, as reported in the issue.What changes are included in this PR?
FinalizableCredentialsProvider: anAWSCredentialsProviderthat delegates to the wrapped provider and drops it on finalization. After that it returns empty credentials. That isn't reachable through Arrow, because S3 operations already fail with "S3 finalized" at that point.S3ClientFinalizertracks these wrappers (weak refs) and finalizes them after the client holders.S3Options::ConfigureDefaultCredentials()andConfigureAssumeRoleWithWebIdentityCredentials()wrap their providers. These are the ones that can be CRT-based.A provider assigned directly to
S3Options::credentials_providerby C++ users is not wrapped. Its lifetime remains the caller's responsibility.Are these changes tested?
Yes:
test_s3_finalize_with_live_web_identity_fsruns in a subprocess with a timeout and needs no network. It covers both an explicitfinalize_s3()and plain interpreter exit with a live filesystem. It times out without this change and passes with it.test_fs.pypass against minio.Are there any user-facing changes?
Processes using web identity credentials no longer hang in
finalize_s3()or at exit.Was AI used for this PR?
In accordance to the AI generation guidelines, please disclose below whether and how AI was used in this PR.
PR code and description written by:
Reviewed before submission by:
AI usage summary:
An AI coding assistant (Claude) helped trace the root cause through the Arrow and AWS SDK sources, wrote the code change, test and description, and ran the builds and repros.
I directed the investigation, reproduced the issue, and validated the fix on EKS with IRSA.
GitHub Issue: Closes [Python][C++] Deadlock in ensure_s3_finalized atexit handler when pyarrow.fs.S3FileSystem reference outlives interpreter shutdown (pyarrow 24.x regression) #50188