Skip to content

Commit

Permalink
Fix the hang due to CRT resources not being cleaned up correctly in s…
Browse files Browse the repository at this point in the history
…amples (#299)
  • Loading branch information
TwistedTwigleg authored Jul 26, 2022
1 parent 4b1bdc8 commit 427f913
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ public void onConnectionResumed(boolean sessionPresent) {
// (see sampleConnectAndDisconnect for implementation)
cmdUtils.sampleConnectAndDisconnect(connection);

// Close the connection now that we are completely done with it.
connection.close();

} catch (CrtRuntimeException | InterruptedException | ExecutionException ex) {
onApplicationFailure(ex);
}
Expand Down
4 changes: 4 additions & 0 deletions samples/BasicPubSub/src/main/java/pubsub/PubSub.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ public void onConnectionResumed(boolean sessionPresent) {

CompletableFuture<Void> disconnected = connection.disconnect();
disconnected.get();

// Close the connection now that we are completely done with it.
connection.close();

} catch (CrtRuntimeException | InterruptedException | ExecutionException ex) {
onApplicationFailure(ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ public void onConnectionResumed(boolean sessionPresent) {
// (see sampleConnectAndDisconnect for implementation)
cmdUtils.sampleConnectAndDisconnect(connection);

// Close the connection now that we are completely done with it.
connection.close();

} catch (CrtRuntimeException | InterruptedException | ExecutionException ex) {
onApplicationFailure(ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ public void onConnectionResumed(boolean sessionPresent) {

CompletableFuture<Void> disconnected = connection.disconnect();
disconnected.get();

// Close the connection now that we are completely done with it.
connection.close();

} catch (CrtRuntimeException | InterruptedException | ExecutionException ex) {
System.out.println("Exception encountered: " + ex.toString());
}
Expand Down
4 changes: 4 additions & 0 deletions samples/Jobs/src/main/java/jobs/JobsSample.java
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ public void onConnectionResumed(boolean sessionPresent) {

CompletableFuture<Void> disconnected = connection.disconnect();
disconnected.get();

// Close the connection now that we are completely done with it.
connection.close();

} catch (CrtRuntimeException | InterruptedException | ExecutionException ex) {
System.out.println("Exception encountered: " + ex.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ public void onConnectionResumed(boolean sessionPresent) {
CompletableFuture<Void> disconnected = connection.disconnect();
disconnected.get();
System.out.println("Disconnected.");

// Close the connection now that we are completely done with it.
connection.close();
}
} catch (CrtRuntimeException | InterruptedException | ExecutionException ex) {
onApplicationFailure(ex);
Expand Down
3 changes: 3 additions & 0 deletions samples/RawConnect/src/main/java/rawconnect/RawConnect.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ public void onConnectionResumed(boolean sessionPresent) {
// Connect and disconnect using the connection we created
// (see sampleConnectAndDisconnect for implementation)
cmdUtils.sampleConnectAndDisconnect(connection);

// Close the connection now that we are completely done with it.
connection.close();
}
}
} catch (CrtRuntimeException | InterruptedException | ExecutionException ex) {
Expand Down
4 changes: 4 additions & 0 deletions samples/Shadow/src/main/java/shadow/ShadowSample.java
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@ public void onConnectionResumed(boolean sessionPresent) {

CompletableFuture<Void> disconnected = connection.disconnect();
disconnected.get();

// Close the connection now that we are completely done with it.
connection.close();

} catch (CrtRuntimeException | InterruptedException | ExecutionException ex) {
System.out.println("Exception encountered: " + ex.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,10 @@ public MqttClientConnection buildPKCS11MQTTConnection(MqttClientConnectionEvents
buildConnectionSetupCAFileDefaults(builder);
buildConnectionSetupConnectionDefaults(builder, callbacks);

return builder.build();
MqttClientConnection conn = builder.build();
builder.close();
return conn;

} catch (CrtRuntimeException ex) {
return null;
}
Expand Down Expand Up @@ -226,7 +229,9 @@ public MqttClientConnection buildWebsocketX509MQTTConnection(MqttClientConnectio
X509CredentialsProvider provider = x509builder.build();
builder.withWebsocketCredentialsProvider(provider);

return builder.build();
MqttClientConnection conn = builder.build();
builder.close();
return conn;

} catch (CrtRuntimeException ex) {
return null;
Expand All @@ -245,7 +250,9 @@ public MqttClientConnection buildWebsocketMQTTConnection(MqttClientConnectionEve
builder.withWebsockets(true);
builder.withWebsocketSigningRegion(getCommandRequired(m_cmd_signing_region, ""));

return builder.build();
MqttClientConnection conn = builder.build();
builder.close();
return conn;

} catch (CrtRuntimeException ex) {
return null;
Expand All @@ -261,7 +268,10 @@ public MqttClientConnection buildDirectMQTTConnection(MqttClientConnectionEvents
buildConnectionSetupCAFileDefaults(builder);
buildConnectionSetupConnectionDefaults(builder, callbacks);
buildConnectionSetupProxyDefaults(builder);
return builder.build();

MqttClientConnection conn = builder.build();
builder.close();
return conn;
}
catch (CrtRuntimeException ex) {
return null;
Expand All @@ -279,7 +289,10 @@ public MqttClientConnection buildDirectMQTTConnectionWithCustomAuthorizer(MqttCl
getCommandOrDefault(m_cmd_custom_auth_authorizer_name, null),
getCommandOrDefault(m_cmd_custom_auth_authorizer_signature, null),
getCommandOrDefault(m_cmd_custom_auth_password, null));
return builder.build();

MqttClientConnection conn = builder.build();
builder.close();
return conn;
}
catch (CrtRuntimeException | UnsupportedEncodingException ex) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ public void onConnectionResumed(boolean sessionPresent) {
// (see sampleConnectAndDisconnect for implementation)
cmdUtils.sampleConnectAndDisconnect(connection);

// Close the connection now that we are completely done with it.
connection.close();

} catch (CrtRuntimeException | InterruptedException | ExecutionException ex) {
onApplicationFailure(ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ public void onConnectionResumed(boolean sessionPresent) {
// Connect and disconnect using the connection we created
// (see sampleConnectAndDisconnect for implementation)
cmdUtils.sampleConnectAndDisconnect(connection);

// Close the connection now that we are completely done with it.
connection.close();
}
} catch (CrtRuntimeException | InterruptedException | ExecutionException ex) {
onApplicationFailure(ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ public void onConnectionResumed(boolean sessionPresent) {
// (see sampleConnectAndDisconnect for implementation)
cmdUtils.sampleConnectAndDisconnect(connection);

// Close the connection now that we are completely done with it.
connection.close();

} catch (CrtRuntimeException | InterruptedException | ExecutionException ex) {
onApplicationFailure(ex);
}
Expand Down

0 comments on commit 427f913

Please sign in to comment.