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

Moved ping reset before wireMessage.onDispatched() #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tvanlaerhoven
Copy link

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:

E AndroidRuntime: Process: be.tadaam.edp, PID: 4237
E AndroidRuntime: com.facebook.react.common.JavascriptException: Error: INVALID_STATE_ERR, js engine: hermes, stack:
E AndroidRuntime: send@1:208833
E AndroidRuntime: _doPing@1:1059789

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.

@Meninblack007
Copy link

@tvanlaerhoven hey. is your patch working fine? I am facing this issue rarely but would love to get it fixed.

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