Closed nabelekt closed 1 year ago
I think the issue has to do with the scope of m_connection
. If I change
m_connection = client.NewConnection(clientConfig);
to
auto connection = client.NewConnection(clientConfig);
the problem goes away. Still not sure why the created connection being in scope prevents the program from continuing. And this doesn't help me much for creating a class that allows creating the connection, disconnecting, and using the connection to be handled in separate methods.
Looking into the issue, the program stalls on the destructor of apiHandle
, and apiHandle
was waiting on destroy of the m_connection
.
You should keep the apiHandle
while you are using the API. As the m_connection
is still alive, you have to keep the apiHandle
.
You can initialize the ApiHandle in main function and remove it from the member function. Example:
int main(int argc, char** argv)
{
// Init ApiHandle here, and remove it from `InitializeConnection `
Aws::Crt::ApiHandle apiHandle;
std::cout << "\nStarting: " << argv[0] << "\n";
DEBUG_PRINT
AwsIotMqttHandler::connectionParameters_t connectionParameters;
connectionParameters.certificateFilepath = "/root/gateway/aws_keys/certificate.pem.crt";
connectionParameters.privateKeyFilepath = "/root/gateway/aws_keys/private.pem.key";
connectionParameters.rootCertificateFilepath = "/root/gateway/aws_keys/AmazonRootCA1.pem";
connectionParameters.endpoint = "a1ivqb4fk63pz7-ats.iot.us-east-1.amazonaws.com";
AwsIotMqttHandler awsIotMqttHandler;
awsIotMqttHandler.InitializeConnection(connectionParameters);
DEBUG_PRINT
return 0;
}
Also, that is the reason why the issue was resolved by changing the connection
to a local variable.
auto connection = client.NewConnection(clientConfig);
As now the connection
is a local variable, it will be destroyed when it goes out of the scope as the apiHandle
does.
Solved! Thank you, @xiazhvera!
Describe the bug
It is likely that this is user error rather than a bug. If so, please educate me on what I am doing wrong here.
Here's my simplified code:
Expected Behavior
Print
and exit. This is what happens if I comment out the
m_connection = client.NewConnection(clientConfig);
line.Current Behavior
Prints
(no
/root/gateway/experiments/aws_mqtt_connection_hang.cpp:83
line) and and does not exit. Three threads are left running:aws_mqtt_connec
AwsEventLoop 1
AwsEventLoop 2
Reproduction Steps
main
function.g++-13
c++20
.Possible Solution
No response
Additional Information/Context
No response
aws-crt-cpp version used
AWS CRT C++ 0.24.3
Compiler and version used
g++-13 (Ubuntu 13.1.0-8ubuntu1~22.04) 13.1.0
Operating System and version
Ubuntu 22.04.2 LTS