aws / aws-sdk-cpp

AWS SDK for C++
Apache License 2.0
1.95k stars 1.05k forks source link

aws-sdk-cpp crashed #3089

Closed liuxinan0402 closed 23 hours ago

liuxinan0402 commented 2 weeks ago

Describe the bug

When startup my application, the function AWSAuth::start() calls Aws::InitAPI() and create CognitoIdentityProviderClient. This function is called only once when the class AWSAuth construction. When shut down the application, Aws::ShutdownAPI() is call during ~AWSAuth().

After the stress testing startup and shut down application many times, the application crashed in aws-sdk-cpp.

Expected Behavior

No crash happened in case of press testing startup application many times

Current Behavior

The stack trace is as following,

aws_tls_init_static_state
/home/buildbot/integration_onecloud_client_cpp-stabilization_2022_earth-tag-android-arm64-v8a-RelWithDebInfo/src/external/aws-sdk-cpp/crt/aws-crt-cpp/crt/aws-c-io/source/s2n/s2n_tls_channel_handler.c:131
aws_mqtt_library_init
/home/buildbot/integration_onecloud_client_cpp-stabilization_2022_earth-tag-android-arm64-v8a-RelWithDebInfo/src/external/aws-sdk-cpp/crt/aws-crt-cpp/crt/aws-c-mqtt/source/mqtt.c:186
Aws::Crt::s_initApi(aws_allocator*)
/home/buildbot/integration_onecloud_client_cpp-stabilization_2022_earth-tag-android-arm64-v8a-RelWithDebInfo/src/external/aws-sdk-cpp/crt/aws-crt-cpp/source/Api.cpp:39
Aws::Crt::ApiHandle* Aws::New<Aws::Crt::ApiHandle, aws_allocator*>(char const*, aws_allocator*&&)
/home/buildbot/integration_onecloud_client_cpp-stabilization_2022_earth-tag-android-arm64-v8a-RelWithDebInfo/src/external/aws-sdk-cpp/aws-cpp-sdk-core/include/aws/core/utils/memory/AWSMemory.h:66
Aws::InitAPI(Aws::SDKOptions const&)
/home/buildbot/integration_onecloud_client_cpp-stabilization_2022_earth-tag-android-arm64-v8a-RelWithDebInfo/src/external/aws-sdk-cpp/aws-cpp-sdk-core/source/Aws.cpp:31
na::mgr::onecloud::AWSAuth::start()
/home/buildbot/integration_onecloud_client_cpp-stabilization_2022_earth-tag-android-arm64-v8a-RelWithDebInfo/src/mgr/onecloud/src/onlineuser/provider/AWSAuth.cpp:78

Reproduction Steps

Do construction and destruction many many times, please see the code fragment from the attached file. AWSAuth-fragment.zip

Possible Solution

No response

Additional Information/Context

No response

AWS CPP SDK version used

Version 1.9

Compiler and Version used

gcc version 9.4.0

Operating System and version

Android 12

jmklix commented 2 weeks ago

You are using a rather old version of this sdk where we don't recommend that you don't startup and shutdown the sdk repeatedly. I would suggest that you don't stress test it in that way, or upgrade to a newer version of this sdk.

sbiscigl commented 2 weeks ago

as joseph has mentioned 1.9 is fairly old, updating to 1.11 would be advised as we will not be patching any version before 1.11. per your issuse and your code i tried to replicate and could not. I simplified your code as welll as it didn't compile as provided. Here I loop over client initialization 10k times (we can do more just it takes longer and I wanted it to run in a reasonable amount of time)

project structure

~/sdk-usage-workspace ❯❯❯ tree
.
├── CMakeLists.txt
├── Dockerfile
├── main.cpp
└── replicate.sh

replicate.sh

#!/bin/zsh

set -u

# build image
docker build -t test-image .

# run example
docker run \
    -e AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID} \
    -e AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY} \
    -e AWS_SESSION_TOKEN=${AWS_SESSION_TOKEN} \
    --name test-image test-image /sdk-example/build/sdk_usage_workspace

Dockerfile

# Using offical Amazon Linux 2023 image from public ECR
FROM public.ecr.aws/amazonlinux/amazonlinux:2023

# Install compiler et al.
RUN yum groupinstall "Development Tools" -y

# Install required dependencies
RUN yum install -y curl-devel openssl-devel ninja-build cmake3

# install sdk
RUN git clone --depth 1 --recurse-submodules https://github.com/aws/aws-sdk-cpp && \
    cd aws-sdk-cpp && \
    mkdir build && \
    cd build && \
    cmake -G Ninja -DBUILD_ONLY="cognito-idp" -DAUTORUN_UNIT_TESTS=OFF .. && \
    cmake --build . && \
    cmake --install .

# Copy over and build
RUN mkdir sdk-example
COPY CMakeLists.txt /sdk-example/CMakeLists.txt
COPY main.cpp /sdk-example/main.cpp
RUN cd sdk-example &&\
    mkdir build &&\
    cd build &&\
    cmake -G Ninja .. &&\
    cmake --build .

CMakeLists.txt

cmake_minimum_required(VERSION 3.13)
project(sdk_usage_workspace)
set(CMAKE_CXX_STANDARD 20)
find_package(AWSSDK REQUIRED COMPONENTS cognito-idp)
add_executable(${PROJECT_NAME} "main.cpp")
target_link_libraries(${PROJECT_NAME} PRIVATE ${AWSSDK_LINK_LIBRARIES})

main.cpp

#include <aws/core/Aws.h>
#include <aws/cognito-idp/CognitoIdentityProviderClient.h>

using namespace Aws::CognitoIdentityProvider;

class AwsTestObject
{
public:
   explicit AwsTestObject(const Aws::SDKOptions& options)
      : options_(options)
   {
      Aws::InitAPI(options_);
   }

   AwsTestObject(const AwsTestObject& other) = delete;
   AwsTestObject(AwsTestObject&& other) noexcept = delete;
   AwsTestObject& operator=(const AwsTestObject& other) = delete;
   AwsTestObject& operator=(AwsTestObject&& other) noexcept = delete;

   ~AwsTestObject()
   {
      Aws::ShutdownAPI(options_);
   };

   void CreateClient()
   {
      const auto cognitoClient = Aws::MakeShared<CognitoIdentityProviderClient>("test-alloc-tag");
      // avoid optimization
      (void) cognitoClient;
   }

private:
   Aws::SDKOptions options_;
};

int main()
{
   Aws::SDKOptions options{};
   for (size_t initLoops = 0; initLoops < 10'000; ++initLoops)
   {
      AwsTestObject object{options};
      object.CreateClient();
   }
   std::cout << "all done\n";
   return 0;
}

If you can update this example to replicate your issue would be happy to look at it, but as far as we can tell this is working as expected.

github-actions[bot] commented 5 days ago

Greetings! It looks like this issue hasn’t been active in longer than a week. We encourage you to check if this is still an issue in the latest release. Because it has been longer than a week since the last update on this, and in the absence of more information, we will be closing this issue soon. If you find that this is still a problem, please feel free to provide a comment or add an upvote to prevent automatic closure, or if the issue is already closed, please feel free to open a new one.