aws-crt-cpp: Program hangs with Aws::Iot::MqttClient:NewConnection()

0

This is a crosspost of my GitHub issue found at https://github.com/awslabs/aws-crt-cpp/issues/556. It is not clear to me how closely issues in the repository are monitored. This issue is with using the aws-crt-cpp library provided by AWS Labs.

Here's my simplified code:

// Some code adopted from https://github.com/aws/aws-iot-device-sdk-cpp-v2/blob/main/samples/pub_sub/basic_pub_sub/main.cpp

#include <aws/crt/Api.h>
#include <aws/iot/MqttClient.h>
#include <iostream>

#define DEBUG_PRINT std::cout << __FILE__ << ":" << __LINE__ << "\n" << std::flush;

class AwsIotMqttHandler
{
    public:

        typedef struct
        {
            Aws::Crt::String certificateFilepath;
            Aws::Crt::String privateKeyFilepath;
            Aws::Crt::String rootCertificateFilepath;
            Aws::Crt::String endpoint;
        } connectionParameters_t;

        AwsIotMqttHandler();

        void InitializeConnection(connectionParameters_t connectionParameters);

    protected:

        std::shared_ptr<Aws::Crt::Mqtt::MqttConnection> m_connection;
};

AwsIotMqttHandler::AwsIotMqttHandler()
{
}

void AwsIotMqttHandler::InitializeConnection(connectionParameters_t connectionParameters)
{
    // Do some global initialization for the API
    Aws::Crt::ApiHandle apiHandle;

    // Create the MQTT builder and populate it with connection parameters
    auto clientConfigBuilder =
        Aws::Iot::MqttClientConnectionConfigBuilder(connectionParameters.certificateFilepath.c_str(),
                                                    connectionParameters.privateKeyFilepath.c_str());
    clientConfigBuilder.WithEndpoint(connectionParameters.endpoint);
    clientConfigBuilder.WithCertificateAuthority(connectionParameters.rootCertificateFilepath.c_str());
    DEBUG_PRINT

    // Create the MQTT connection from the MQTT builder
    Aws::Iot::MqttClientConnectionConfig clientConfig = clientConfigBuilder.Build();
    if (!clientConfig)
    {
        std::cerr << std::string("Client configuration initialization failed with error: ")
                        + std::string(Aws::Crt::ErrorDebugString(clientConfig.LastError()));
        return;
    }
    DEBUG_PRINT
    Aws::Iot::MqttClient client = Aws::Iot::MqttClient();
    m_connection = client.NewConnection(clientConfig);
    DEBUG_PRINT
    if (m_connection != nullptr && !*m_connection)
    {
        std::cerr << std::string("MQTT connection creation failed with error: ")
                        + std::string(Aws::Crt::ErrorDebugString(m_connection->LastError()));
        return;
    }
    DEBUG_PRINT
}

int main(int argc, char** argv)
{
    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;
}

Expected Behavior

Print


Starting: ./aws_mqtt_connection_hang.exe
/root/gateway/experiments/aws_mqtt_connection_hang.cpp:72
/root/gateway/experiments/aws_mqtt_connection_hang.cpp:45
/root/gateway/experiments/aws_mqtt_connection_hang.cpp:55
/root/gateway/experiments/aws_mqtt_connection_hang.cpp:58
/root/gateway/experiments/aws_mqtt_connection_hang.cpp:65
/root/gateway/experiments/aws_mqtt_connection_hang.cpp:83

and exit. This is what happens if I comment out the m_connection = client.NewConnection(clientConfig); line.

Current Behavior

Prints


Starting: ./aws_mqtt_connection_hang.exe
/root/gateway/experiments/aws_mqtt_connection_hang.cpp:72
/root/gateway/experiments/aws_mqtt_connection_hang.cpp:45
/root/gateway/experiments/aws_mqtt_connection_hang.cpp:55
/root/gateway/experiments/aws_mqtt_connection_hang.cpp:58
/root/gateway/experiments/aws_mqtt_connection_hang.cpp:65

(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

  1. Change the filepaths and endpoint in the main function.
  2. Build the code under g++-13 c++20.
  3. Run the code.

Question

How do I modify the code so that my program can proceed after the call to NewConnection()?

asked 7 months ago200 views
1 Answer
0
Accepted Answer

This was solved in the GitHub issue linked above.

answered 7 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions