-
Notifications
You must be signed in to change notification settings - Fork 230
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
reconnection problem when use same client id #173
Comments
Ensure the original client is not set to autoReconnect, otherwise both clients will take turn to connect and kick off the other client. I just tried it with client not set to autoReconnect, and the new client with the same clientID is able to receive the subscription message. @mochi-co , it is more logical to have OnDisconnect before OnConnect. But tricky given the current structure. BTW, in looking into this, I found cl.WritePacket is now no longer called by a single thread, so there can be multiple writes to the connection simultaneously. |
@thedevop Neither client has set to autoReconnect, but both of their Clean Session are set,you can try it again with Clean Session on. |
Thanks @werbenhu. I was able to reproduce the issue. Below is a description of the issue:
In fact, if a 3rd session is created with the same clientID, it will now exhibit a different issue, both 2nd and 3rd session connections will exist on the broker, and broker state only thinks there is 3rd session. @mochi-co , there are few potential solutions to this, for example, add an additional state, or move the clean up into cl.State.endOnce in cl.Stop, etc... |
@thedevop i fixed it by the this rp, check client's stopCause if is ErrSessionTakenOver , if yes don't delete. |
Thanks @werbenhu , please see my comments there. The more I think about the order or OnConnect (new connection) / OnDisconnect (previous session), the current state is accurate. The new connection (this is before establishing a session) does occur before disconnect of the previous session. There is another event OnSessionEstablished which occurs after OnDisconnect (previous session). |
@thedevop i tried OnSessionEstablished(), it is also called after OnDisconnect(). the old and new connections are in two goroutines. new one call inheritClientSession() -> old one end cl.Read() and call OnDisconnect -> new one call OnSessionEstablished() So OnDisconnect needs to be before cl.Read(). |
I think we've seen the same issue too so can test it out when this is out in a release |
I'm going to have a think about the simplest way to solve this issue |
@mochi-co I just tested it on mosquitto, it will not send LWT in the case of reconnection by a same client id, maybe it is possible to consider not calling OnDisconnect(), just add an OnReconnect() interface? |
I had quite a deep look into this over the last few days as this kind of behaviour is usually indicative of a more ambigious problem. I can confirm that the issue is typically caused as @thedevop described. Simply:
Additionally, the expire block then deletes the client by id, which likely causes some more unusual behaviour. To correct this, I have implemented the following:
Finally, I want to thank you all very much for your work investigating and discussing this issue! It made my life much easier than it would have been otherwise, and I really appreciate you taking the time discuss it and help make this project better! The PR for these changes is #180 and has been released as v2.2.4 - Please let me know if the latest release resolves the issue. |
@mochi-co , you may want make the entire expire block to check |
expire := (cl.Properties.ProtocolVersion == 5 && cl.Properties.Props.SessionExpiryIntervalFlag && cl.Properties.Props.SessionExpiryInterval == 0)
|| (cl.Properties.ProtocolVersion < 5 && cl.Properties.Clean) I misread your point, you are absolutely correct. I'll try to remember to fix this, or anyone can issue a PR and I'll merge it in in for the next release 👍🏻 |
Hello. I've updated to 2.2.4 and am still seeing a problem with reconnecting with the same client id. I test with a simple nodered flow with a single publisher and single subscriber on the same topic. The subscriber only receives messages every 2nd deploy of the nodered flow - every other deploy there is a disconnected from the broker and then the reconnection nolonger receives the messages for the subscription. Could i switch on verbose logging on Mochi or something to show you more info on what happens? |
|
could it be out in a release please so we can test it too? |
* Skip expire cleanup for isTakenOver session * Set prev connection to isTakenOver on CleanSession #173.
Sorry for delay! Try https://github.com/mochi-co/mqtt/releases/tag/v2.2.5 and let me know! Thanks again @thedevop |
Yes v2.2.5 works now for us with client reconnects. Thanks! |
BUG: If there is already a connection with a client ID, and another connection uses the same client ID to connect again, the new client cannot receive the subscription message, and the hook should call OnDisconnect() first when the new connection comes. , and then call OnConnect()
The text was updated successfully, but these errors were encountered: