Skip to content
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

How to handle onConnectionInterrupted callback due to connection close in 24 hours #771

Closed
cyber274 opened this issue Dec 31, 2024 · 6 comments
Labels
bug This issue is a bug. p3 This is a minor priority issue

Comments

@cyber274
Copy link

Describe the bug

We are creating MQTT connection(software.amazon.awssdk.crt.mqtt.MqttClientConnection) over WebSocket using IAM Role credentials. The connection gets successfully created and we are able to listen to incoming events.

However, we noticed that MQTT connection gets closed unexpectedly in 24 hours due to IOT Core limitation. And the connection does not resume automatically.

This is the code.
{
final EventLoopGroup eventLoopGroup = new EventLoopGroup(1);
final ClientBootstrap clientBootstrap = new ClientBootstrap(eventLoopGroup,
new HostResolver(eventLoopGroup));

        final SocketOptions socketOptions = new SocketOptions();
        socketOptions.keepAliveTimeoutSecs = 30;
        socketOptions.keepAliveIntervalSecs = 5;
        socketOptions.keepAlive = true;

        return AwsIotMqttConnectionBuilder.newDefaultBuilder()
                .withBootstrap(clientBootstrap)
                .withClientId(iotConnectClientId)
                .withEndpoint(iotCoreEndpoint)
                .withWebsockets(true)
                .withReconnectTimeoutSecs(1, 60)
                .withWebsocketSigningRegion(region)
                .withProtocolOperationTimeoutMs(30 * 1000)
                .withPingTimeoutMs(15 * 1000)
                .withWebsocketCredentialsProvider(credentialsProvider)
                .withKeepAliveSecs(60)
                .withSocketOptions(socketOptions)
                .withTimeoutMs(30 * 1000)
                .withCleanSession(false)
                .withConnectionEventCallbacks(getMqttClientConnectionEvents(siteId, eventLoopGroup))
                .build();

    } catch (final Exception e) {
        log.error("Exception while creating AwsIotMqtt Connection", e);
        throw e;
    }

}

We receive error code: 5134 and error message "string: The connection was closed unexpectedly. for MQTT topic: ". in onConnectionInterrupted() callback method.

We tried to call mqttSecondaryConnection.connect().get(10, TimeUnit.SECONDS); as reconnect() in onConnectionInterrupted() callback method but we received:

java.util.concurrent.ExecutionException: software.amazon.awssdk.crt.CrtRuntimeException: MqttClientConnection.mqtt_connect: aws_mqtt_client_connection_connect failed (aws_last_error: AWS_ERROR_MQTT_ALREADY_CONNECTED(5132), The requested operation is invalid as the connection is already open.) AWS_ERROR_MQTT_ALREADY_CONNECTED(5132) at java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:396) ~[?:?] at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:2096) ~[?:?] at ...

Can anybody help, how to reconnect in this case ?

Expected Behavior

The connection should resume automatically.

Current Behavior

The connection does not resume in this case.

Reproduction Steps

Create the fresh connection after 24 hours to see the connection closure after 24 hours.

Possible Solution

No response

Additional Information/Context

No response

SDK version used

JDK17

Environment details (OS name and version, etc.)

IOT Core

@cyber274 cyber274 added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Dec 31, 2024
@bretambrose
Copy link
Contributor

bretambrose commented Jan 1, 2025

The client will always reconnect in the scenario you have described. If you have CRT logs showing otherwise, please attach them. Also, this issue would best belong in the v2 Java SDK repository.

@cyber274
Copy link
Author

cyber274 commented Jan 2, 2025

Created new issue in v2 Java SDK repository with the error log. aws/aws-iot-device-sdk-java-v2#607
Can you please have a look.

@cyber274
Copy link
Author

cyber274 commented Jan 2, 2025

The client will always reconnect in the scenario you have described.

Also, Can you explain this line "The client will always reconnect in the scenario you have described." Did you mean, the client reconnects automatically or the client needs to fire some code to reconnect ?

@bretambrose
Copy link
Contributor

"the client reconnects automatically"

@jmklix jmklix added p3 This is a minor priority issue and removed needs-triage This issue or PR still needs to be triaged. labels Jan 2, 2025
@jmklix
Copy link
Member

jmklix commented Jan 2, 2025

Closing this in favor of the other issue that you opened

@jmklix jmklix closed this as completed Jan 2, 2025
Copy link

github-actions bot commented Jan 2, 2025

This issue is now closed. Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.

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. p3 This is a minor priority issue
Projects
None yet
Development

No branches or pull requests

3 participants