aws / aws-sdk-cpp

AWS SDK for C++
Apache License 2.0
1.97k stars 1.06k forks source link

Aws::ShutdownAPI hangs when S3Crt is used #2769

Closed jeanbez closed 11 months ago

jeanbez commented 11 months ago

Describe the bug

When calling the Aws::ShutdownAPI(options) the execution seems to hang, this happens in MacOS with the latest version of the SDK and only when using the S3Crt.

Expected Behavior

Shutdown to complete and application to exit successfully.

Current Behavior

The logs indicate it get stuck mid-shutdown process:

[INFO] 2023-11-28 23:13:05.921 Aws_Init_Cleanup [0x7ff8517cae80] Shutdown AWS SDK for C++.
[DEBUG] 2023-11-28 23:13:05.922 HttpClientFactory [0x7ff8517cae80] Cleanup Http Static State
[DEBUG] 2023-11-28 23:13:05.922 HttpClientFactory [0x7ff8517cae80] Cleanup Curl Http Client

Reproduction Steps

Sample code based on the provided examples in documentation that cause this behavior:

#include <iostream>
#include <aws/core/Aws.h>
#include <aws/s3-crt/S3CrtClient.h>
#include <aws/s3-crt/model/CreateBucketRequest.h>
#include <aws/s3-crt/model/BucketLocationConstraint.h>
#include <aws/s3-crt/model/DeleteBucketRequest.h>
#include <aws/s3-crt/model/PutObjectRequest.h>
#include <aws/s3-crt/model/GetObjectRequest.h>
#include <aws/s3-crt/model/DeleteObjectRequest.h>
#include <aws/s3-crt/model/HeadObjectRequest.h>

int main() {
    std::shared_ptr<Aws::S3Crt::S3CrtClient> aws_crt_client;

    Aws::SDKOptions options;
    options.loggingOptions.logLevel = Aws::Utils::Logging::LogLevel::Trace;

    Aws::InitAPI(options);

    const double throughput_target_gbps = 100;
    const uint64_t part_size = 1 * 1024 * 1024; // 1 MB

    Aws::S3Crt::ClientConfiguration ctr_config;
    ctr_config.region = Aws::Region::US_WEST_1;
    ctr_config.throughputTargetGbps = throughput_target_gbps;
    ctr_config.partSize = part_size;

    aws_crt_client = std::make_shared<Aws::S3Crt::S3CrtClient>(ctr_config);

    Aws::S3Crt::Model::ListBucketsOutcome outcome = aws_crt_client->ListBuckets();

    if (outcome.IsSuccess()) {
        std::cout << "All buckets under my account:" << std::endl;

        for (auto const& bucket : outcome.GetResult().GetBuckets())
        {
            std::cout << "  * " << bucket.GetName() << std::endl;
        }
        std::cout << std::endl;
    }
    else {
        std::cout << "ListBuckets error:\n"<< outcome.GetError() << std::endl << std::endl;
    }

    Aws::ShutdownAPI(options);

    return 0;
}

The contents are listed, and upon calling shutdown, the code hangs indefinitely. Upon interrupting the execution in lldb the stack trace is the following:

* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGSTOP
  * frame #0: 0x00007ff80e1b55d6 libsystem_kernel.dylib`__psynch_cvwait + 10
    frame #1: 0x00007ff80e1f276b libsystem_pthread.dylib`_pthread_cond_wait + 1211
    frame #2: 0x00007ff80e12a3c2 libc++.1.dylib`std::__1::condition_variable::wait(std::__1::unique_lock<std::__1::mutex>&) + 18
    frame #3: 0x00007ff80e12ac37 libc++.1.dylib`std::__1::__assoc_sub_state::__sub_wait(std::__1::unique_lock<std::__1::mutex>&) + 45
    frame #4: 0x00007ff80e12ac9e libc++.1.dylib`std::__1::__assoc_sub_state::wait() + 46
    frame #5: 0x0000000101216d85 libaws-crt-cpp.dylib`Aws::Crt::Io::ClientBootstrap::~ClientBootstrap() + 45
    frame #6: 0x0000000100845b70 libaws-cpp-sdk-core.dylib`std::__1::shared_ptr<Aws::Crt::Io::ClientBootstrap>::operator=[abi:v160006](std::__1::shared_ptr<Aws::Crt::Io::ClientBootstrap> const&) + 66
    frame #7: 0x0000000100845c5d libaws-cpp-sdk-core.dylib`Aws::CleanupCrt() + 34
    frame #8: 0x0000000100845787 libaws-cpp-sdk-core.dylib`Aws::ShutdownAPI(Aws::SDKOptions const&) + 544

Possible Solution

No response

Additional Information/Context

No response

AWS CPP SDK version used

1.11.211

Compiler and Version used

Apple clang version 15.0.0 (clang-1500.0.40.1)

Operating System and version

MacOs Sonoma 14.0 (23A344)

sbiscigl commented 11 months ago

Hey @jeanbez thanks for reaching out i'd like to redirect you to our basic usage documentation specifically

Aws::InitAPI(options);
{
    // make your SDK calls here.
}
Aws::ShutdownAPI(options);

because the specific issue is that your shared ptr for the s3 client still exists when you call shutdown. because of this shutdown will wait for you to delete the client to release the associated resources. refactoring your code to use the RAII to automatically clean up the client like

#include <iostream>
#include <aws/core/Aws.h>
#include <aws/s3-crt/S3CrtClient.h>
#include <aws/s3-crt/model/CreateBucketRequest.h>

int main() {

  Aws::SDKOptions options;
  options.loggingOptions.logLevel = Aws::Utils::Logging::LogLevel::Trace;

  Aws::InitAPI(options);
  {
    std::shared_ptr<Aws::S3Crt::S3CrtClient> aws_crt_client;
    const double throughput_target_gbps = 100;
    const uint64_t part_size = 1 * 1024 * 1024; // 1 MB

    Aws::S3Crt::ClientConfiguration ctr_config;
    ctr_config.region = Aws::Region::US_WEST_1;
    ctr_config.throughputTargetGbps = throughput_target_gbps;
    ctr_config.partSize = part_size;

    aws_crt_client = std::make_shared<Aws::S3Crt::S3CrtClient>(ctr_config);

    Aws::S3Crt::Model::ListBucketsOutcome outcome = aws_crt_client->ListBuckets();

    if (outcome.IsSuccess()) {
      std::cout << "All buckets under my account:" << std::endl;

      for (auto const&bucket: outcome.GetResult().GetBuckets()) {
        std::cout << "  * " << bucket.GetName() << std::endl;
      }
      std::cout << std::endl;
    }
    else {
      std::cout << "ListBuckets error:\n" << outcome.GetError() << std::endl << std::endl;
    }
  }
  Aws::ShutdownAPI(options);
  return 0;
}

will make sure that std::shared_ptr<Aws::S3Crt::S3CrtClient> aws_crt_client has been destructed when shutdown is called. this could also be accomplished by adding aws_crt_client.reset(); before calling shutdown but i'd recommend using RAII instead of directly invoking reset.

let me know if you have any questions.

jeanbez commented 11 months ago

got it, thanks! calling reset solved the issue

github-actions[bot] commented 11 months ago

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.