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

When the client reconnects, resubscribing will result in duplicate subscriptions #204

Open
xuzimianxzm opened this issue Oct 24, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@xuzimianxzm
Copy link

🐛 Bug Report

🔬 How To Reproduce

Steps to reproduce the behavior:

When the client reconnects, I try to wait for the automatic disconnection to complete in the Disconnect event, and then try to subscribe again to the topic that was subscribed before the connection was lost. However, duplicate subscriptions will occur at this time, and the number of duplicate subscriptions to the same topic will double after each reconnection. Even if I have explicitly called the Client's unsubscribe topic method, it does not work.

Code sample

The following is the code for restoring topic subscription after automatic reconnection:

 if (sender is not IHiveMQClient client) return;

            logger.LogInformation("The connection has been disconnected. Try to reconnecting");

            while (!client.IsConnected())
            {
                logger.LogInformation("Wait mqtt client auto reconnecting...");
                Thread.Sleep(5000);
            }

            logger.LogInformation("The mqtt client has reconnected.");

            client.UnsubscribeAsync(client.Subscriptions)
                .ConfigureAwait(false)
                .GetAwaiter()
                .GetResult();

            var newsSubscribeResult = client.SubscribeAsync(_subscribeOptions!)
                .ConfigureAwait(false)
                .GetAwaiter()
                .GetResult();

When I consulted the HiveMQClient source code, I found that the subscription topics related information is stored in this variable, as follows:
9079a7730f76d4625313992d9361414

Each time you subscribe, the topics and corresponding handlers that have completed the subscription will be put into this collection:
181b27aa5db5c78c042c2a13f1a3b0d

However, when canceling a subscription, there is no attempt to remove the unsubscribed topics and handlers. You can see that the Subscriptions collection of this HiveMQClient object does not have any call to remove elements:
9079a7730f76d4625313992d9361414

Therefore, based on the above analysis, I took the following temporary corrective measures, that is, manually "Subscriptions.Clear();" to ensure that there will be no repeated subscriptions to Topics:
image

@xuzimianxzm xuzimianxzm added the bug Something isn't working label Oct 24, 2024
@pglombardo
Copy link
Collaborator

Hi @xuzimianxzm - thanks for letting us know. I'll take a closer look at this and respond back soon!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants