Fix race condition between disconnect and close#1094
Open
MortronMeymo wants to merge 1 commit intoeclipse-paho:developfrom
Open
Fix race condition between disconnect and close#1094MortronMeymo wants to merge 1 commit intoeclipse-paho:developfrom
MortronMeymo wants to merge 1 commit intoeclipse-paho:developfrom
Conversation
If close happened immediately after disconnect the following happened: 1. disconnect sets comm state to DISCONNECTING and starts a new thread to actually do the disconnect. 2. close sees this state and only sets closePending to true. 3. The new thread from 1 does some cleanup and sends the MQTT disconnect message and waits for it to be sent. 4. The disconnect thread calls shutdownConnection(...) which simply returns and does nothing if closePending is true. So if 2 happens before 4 no one executes the shutdownConnection logic and the client is left in a unusable but also not cleaned up state. Triggering this race condition is extremly easy if someone is just trying to properly cleanup a connected client with the following code: client.disconnect(); client.close(); Simply removing the check for closePending in the start of shutdownConnection fixes this. Removing this check should also not introduce new issues, since the close() itself does not cause a call to shutdownConnection. Which means that if there is another race condition here then such a condition can already be triggered by other means as well. There is a small gap where in theory the user code could attempt to create a new connection, but that will fail, since the connect call also checks the closePending flag. Closes: eclipse-paho#1093 Signed-off-by: Morten Mey <morten.mey@esrlabs.com>
Author
|
Closes #1093 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
If close happened immediately after disconnect the following happened:
So if 2 happens before 4 no one executes the shutdownConnection logic and the client is left in a unusable but also not cleaned up state.
Triggering this race condition is extremly easy if someone is just trying to properly cleanup a connected client with the following code:
client.disconnect();
client.close();
Simply removing the check for closePending in the start of shutdownConnection fixes this.
Removing this check should also not introduce new issues, since the close() itself does not cause a call to shutdownConnection. Which means that if there is another race condition here then such a condition can already be triggered by other means as well. There is a small gap where in theory the user code could attempt to create a new connection, but that will fail,
since the connect call also checks the closePending flag.
Closes: 1093
Please make sure that the following boxes are checked before submitting your Pull Request, thank you!
used to sign the CLA) Hint: use the -s argument when committing.
you are fixing straight away that you add some Description about the bug and how this will fix it.