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 potential deadlock when closing while reconnecting #1393

Merged
merged 1 commit into from
Oct 14, 2021

Conversation

timtay-microsoft
Copy link
Member

@timtay-microsoft timtay-microsoft commented Oct 14, 2021

If a user calls open/close while already closing/opening, the deviceIO layer will make the first call finish before running the second call.

If a user calls close while the client is reconnecting, the close call will notify the reconnection thread to stop, and will wait until reconnection has finished (failed or success) before closing the client.

If a user calls open while the client is reconnecting, the open call will return without doing anything because the SDK is still considered "open"

If a reconnection event happens while the user is closing, the reconnection event is ignored.

#1393

@@ -452,28 +455,17 @@ void registerMultiplexingConnectionStateCallback(IotHubConnectionStatusChangeCal
@Override
public void execute(IotHubConnectionStatus status, IotHubConnectionStatusChangeReason statusChangeReason, Throwable throwable, Object callbackContext)
{
synchronized (this.stateLock)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This lock statement was the cause of the issue. The reconnection thread would first grab the reconnectionLock in IotHubTransport.java, then attempt to notify this DeviceIO layer that the transport is reconnecting, so it would need to grab this stateLock, too.

But if the user had initiated a close call already, then this lock was grabbed already by the user's thread. Once the user's thread has grabbed this stateLock, it then attempts to grab the reconnectionLock in IothubTransport.java.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to synchronize on this lock in this callback since it is either called by the user's thread that called open/close (which already grabbed this lock in this deviceIO layer), or it is called by the reconnection thread. When it is called by the reconnection thread, we know that there isn't a close call happening since we have the reconnectionLock in the IotHubTransport layer preventing exactly that.

@timtay-microsoft timtay-microsoft marked this pull request as ready for review October 14, 2021 17:54
@timtay-microsoft
Copy link
Member Author

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 4 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants