Skip to content

Calling Aws::initAPI() spawns multiple threads #1854

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

Closed
dbowman-ion opened this issue Jan 28, 2022 · 8 comments
Closed

Calling Aws::initAPI() spawns multiple threads #1854

dbowman-ion opened this issue Jan 28, 2022 · 8 comments
Labels
bug This issue is a bug. closed-for-staleness p2 This is a standard priority issue response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 10 days.

Comments

@dbowman-ion
Copy link

dbowman-ion commented Jan 28, 2022

It appears that a call to Aws::initAPI() spawns '# cpu cores / 2' threads but so far I've been unable to find any documentation on how or why this happens, or how to configure it.

We spawn multiple process that call Aws::InitAPI and on a high cpu count machine this results in more than 2000 threads spawned by the SDK. As threads/PID's are not an infinite resource, any advice on how to remove/limit this behaviour would be appreciated.

Minimal reproducable example:

#include <thread>
#include <chrono>
#include <aws/core/Aws.h>
int main(int, char**)
{
    Aws::SDKOptions options;
    Aws::InitAPI(options);
    std::this_thread::sleep_for( std::chrono::seconds( 600 ) );
    return 0;
}

Compile & run and then do a pstack on its pid, you'll see multiple threads like:

Thread 19 (Thread 0x7f70257e2700 (LWP 301201)):
#0  0x00007f70349fc923 in epoll_wait () from /lib64/libc.so.6
#1  0x00007f7035b73c1b in s_main_loop () from /path/to/aws/1.9.114/lib64/libaws-cpp-sdk-core.so
#2  0x00007f7035c2ed5f in thread_fn () from /path/to/aws/1.9.114/lib64/libaws-cpp-sdk-core.so
#3  0x00007f7034ccee25 in start_thread () from /lib64/libpthread.so.0
#4  0x00007f70349fc34d in clone () from /lib64/libc.so.6
@dbowman-ion
Copy link
Author

Did a bit more investigation, and was able to limit the threads via:

    options.ioOptions.clientBootstrap_create_fn = []()
    {
        Aws::Crt::Io::EventLoopGroup eventLoopGroup( 2 );
        Aws::Crt::Io::DefaultHostResolver defaultHostResolver(eventLoopGroup, 8, 30);
        auto clientBootstrap = Aws::MakeShared<Aws::Crt::Io::ClientBootstrap>(ALLOCATION_TAG, eventLoopGroup, defaultHostResolver);
        clientBootstrap->EnableBlockingShutdown();
        return clientBootstrap;
    };

@KaibaLopez
Copy link
Contributor

Hi @dbowman-ion ,
So.. it seems you've found a sort of workaround, it this now a question on why this happens or?

@KaibaLopez KaibaLopez added guidance Question that needs advice or information. response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 10 days. labels Jan 28, 2022
@KaibaLopez KaibaLopez self-assigned this Jan 28, 2022
@github-actions
Copy link

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.

@github-actions github-actions bot added the closing-soon This issue will automatically close in 4 days unless further comments are made. label Jan 31, 2022
@dbowman-ion
Copy link
Author

Yes, understanding why would be nice.
It may also be worth some documentation as neither of these mention anything about threads or how to limit them:
https://sdk.amazonaws.com/cpp/api/LATEST/struct_aws_1_1_s_d_k_options.html
https://sdk.amazonaws.com/cpp/api/LATEST/struct_aws_1_1_io_options.html

I ended up finding the answer in aws-sdk-cpp-core/source/Aws.cpp

@github-actions github-actions bot removed closing-soon This issue will automatically close in 4 days unless further comments are made. response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 10 days. labels Feb 1, 2022
@KaibaLopez KaibaLopez added bug This issue is a bug. needs-review This issue or pull request needs review from a core team member. and removed guidance Question that needs advice or information. labels Feb 18, 2022
@saad-alsaad1
Copy link

saad-alsaad1 commented May 8, 2022

Hi,

I guess I have the same issue in my case.
I have an executable that gets some objects from S3, when I call the executable multiple times (sequential about 20-30 times) I get the crash below.
The crash happens when calling Aws::initAPI()
code:

#include <aws/core/Aws.h>

static Aws::SDKOptions options;
void init_aws(){
Aws::InitAPI(options);
}

main(){
init_aws();
Aws::ShutdownAPI(options);
}

Trace:
=5629==ERROR: AddressSanitizer: SEGV on unknown address 0x7fff65800002 (pc 0x7f3868b462bd bp 0x611000000400 sp 0x7f3861bfb0c0 T1)
==5629==The signal is caused by a READ memory access.
#0 0x7f3868b462bc in getenv (/lib64/libc.so.6+0x392bc)
#1 0x7f3867b5f863 (/path/libuv.so.1+0xc863)
#2 0x7f386993820a in __pthread_once_slow (/lib64/libpthread.so.0+0x620a)
#3 0x7f3867b71058 in uv_once (/path/libuv.so.1+0x1e058)
#4 0x7f3867b5fab8 in uv__work_submit (/path/libuv.so.1+0xcab8)
#5 0x7f3867b6a5a1 in uv_getaddrinfo (/path/libuv.so.1+0x175a1)

@pgj007
Copy link

pgj007 commented Jun 2, 2022

This problem occurs when the InitAPI and ShutdownAPI are not in the same thread。

@jmklix
Copy link
Member

jmklix commented Mar 8, 2023

Can you make sure you are using the sdk correctly as show in the docs:

#include <aws/core/Aws.h>
int main(int argc, char** argv)
{
   Aws::SDKOptions options;
   Aws::InitAPI(options);
   {
      // make your SDK calls here.
   }
   Aws::ShutdownAPI(options);
   return 0;
}

@jmklix jmklix added p2 This is a standard priority issue and removed needs-review This issue or pull request needs review from a core team member. labels Mar 8, 2023
@jmklix jmklix added the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 10 days. label Mar 8, 2023
@github-actions
Copy link

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.

@github-actions github-actions bot added closing-soon This issue will automatically close in 4 days unless further comments are made. closed-for-staleness and removed closing-soon This issue will automatically close in 4 days unless further comments are made. labels Mar 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. closed-for-staleness p2 This is a standard priority issue response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 10 days.
Projects
None yet
Development

No branches or pull requests

5 participants