Fix race condition between disconnect and close #1094
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.