Moved ping reset before wireMessage.onDispatched() #37
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.
Note, this PR is related to "Fixed issue where the pinger would attempt to ping while the socket is closing" #9
This PR fixes a crash when resuming an app that disconnected the mqtt client before going to the background:
The issue happens when the app goes to background, and the mqtt client is disconnected. In that case ClientImplementation.disconnect() sends a last MESSAGE_TYPE.DISCONNECT message, which on wireMessage.onDispatched() does a ClientImplementation._disconnected() call. In _disconnected(), sendPinger.cancel() is executed, effectively disabling keepalives.
However, _process_queue() always sends a sendPinger.reset() after dispatching messages to reset the keepAlive mechanism. So a new keepAlive is scheduled after the app goes to background and the client is disconnected.
If the client comes back to foreground, the sendPinger._doPing() will immediately execute on a disconnected client as it was scheduled before going to the background, and is still waiting on the JS message queue. The result is a crash.
The solution is to reset the pinger's keepAlive mechanism before processing the message queue. In that case the keepAlive is still pushed ahead in time on every message that is sent, and the mechanism is not reset after disconnecting.