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

fix(iot-dev): fix bug where client opening with retry isn't cancelled by close #1760

Merged
merged 1 commit into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ private void stopReconnectThreads()
*/
void close()
{
// Notify the transport layer that the user wants to close the connection. This
// will end any "open with retry" operations. Once any running open calls have ended,
// the state lock will be released and this close operation can proceed
this.transport.setClosing(true);
synchronized (this.stateLock)
{
if (state == IotHubConnectionStatus.DISCONNECTED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.microsoft.azure.sdk.iot.device.transport.https.exceptions.UnauthorizedException;
import com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection;
import com.microsoft.azure.sdk.iot.device.transport.mqtt.exceptions.MqttUnauthorizedException;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import javax.net.ssl.SSLContext;
Expand Down Expand Up @@ -119,6 +120,7 @@ public class IotHubTransport implements IotHubListener
private final boolean useIdentifiableThreadNames;

// Flag set when close() starts. Acts as a signal to any running reconnection logic to not try again.
@Setter
private boolean isClosing;

// Used to store the CorrelationCallbackMessage, context, and start time for a correlationId
Expand Down Expand Up @@ -477,9 +479,16 @@ public void open(boolean withRetry) throws TransportException, IotHubClientExcep
int connectionAttempt = 0;
long startTime = System.currentTimeMillis();

// this loop either ends in throwing an exception when retry expires, or by a break statement upon a successful openConnection() call
// this loop either ends in throwing an exception when retry expires,
// a break statement upon a successful openConnection() call,
// or the user attempts to close the client
while (true)
{
if (this.isClosing)
{
throw new TransportException("Client was closed while attempting to open the connection");
}

RetryPolicy retryPolicy = isMultiplexing ? multiplexingRetryPolicy : this.getDefaultConfig().getRetryPolicy();

try
Expand Down
Loading