MQTTnet: DisconnectInternalAsync keep waiting and doesn't end

Describe the bug

The inner task TrySendKeepAliveMessagesAsync Get Timeout Exception and then disconnect keep running that program can’t do any other operations.

Which project is your bug related to?

  • Client

To Reproduce

Steps to reproduce the behavior:

  1. Using this version of MQTTnet 3.0.2
  2. Program is busy running

Expected behavior

Disconnect should finish it’s own and tell the disconnect handler

Screenshots

the mqttnetLog shown like this, it nerver go to the finalize block ‘disconneted’. I had wait ten minutes but no more logs , The program try client.SubscribeAsync but does not work :

2020-04-27 09:44:25.7520|Hrst.AIDetect.WebApiServer.MqttJobHandler|INFO|<mqttNetLog>[2020-04-27T01:44:25.7519515Z] [] [24] [MqttClient.MqttChannelAdapter] [Verbose]: RX (91 bytes) <<< Publish: [Topic=Work/abc123] [Payload.Length=76] [QoSLevel=AtMostOnce] [Dup=False] [Retain=False] [PacketIdentifier=]| 2020-04-27 09:44:25.7520|Hrst.AIDetect.WebApiServer.MqttJobHandler|INFO|Receive the topic:Work/abc123,{“devid”:“abc123”,“unit”:“”,“type”:0,“option”:7,“data”:{“soc”:80,“temp”:45}}| 2020-04-27 09:44:33.4840|Hrst.AIDetect.WebApiServer.MqttJobHandler|INFO|<mqttNetLog>[2020-04-27T01:44:33.4839523Z] [] [24] [MqttClient.MqttChannelAdapter] [Verbose]: RX (91 bytes) <<< Publish: [Topic=Work/abc123] [Payload.Length=76] [QoSLevel=AtMostOnce] [Dup=False] [Retain=False] [PacketIdentifier=]| 2020-04-27 09:44:33.4840|Hrst.AIDetect.WebApiServer.MqttJobHandler|INFO|Receive the topic:Work/abc123,{“devid”:“abc123”,“unit”:“”,“type”:0,“option”:7,“data”:{“soc”:80,“temp”:45}}| 2020-04-27 09:44:48.5917|Hrst.AIDetect.WebApiServer.MqttJobHandler|INFO|<mqttNetLog>[2020-04-27T01:44:48.5916000Z] [] [27] [MqttClient.MqttChannelAdapter] [Verbose]: TX (2 bytes) >>> PingReq| 2020-04-27 09:45:18.5923|Hrst.AIDetect.WebApiServer.MqttJobHandler|INFO|<mqttNetLog>[2020-04-27T01:45:18.5922783Z] [] [27] [MqttClient] [Warning]: Timeout while waiting for packet of type ‘MqttPingRespPacket’.| 2020-04-27 09:45:18.5959|Hrst.AIDetect.WebApiServer.MqttJobHandler|INFO|<mqttNetLog>[2020-04-27T01:45:18.5931456Z] [] [27] [MqttClient] [Warning]: MQTT communication exception while sending/receiving keep alive packets. MQTTnet.Exceptions.MqttCommunicationTimedOutException: Exception of type ‘MQTTnet.Exceptions.MqttCommunicationTimedOutException’ was thrown. at MQTTnet.PacketDispatcher.MqttPacketAwaiter`1.WaitOneAsync(TimeSpan timeout) at MQTTnet.Client.MqttClient.SendAndReceiveAsync[TResponsePacket](MqttBasePacket requestPacket, CancellationToken cancellationToken) at MQTTnet.Client.MqttClient.TrySendKeepAliveMessagesAsync(CancellationToken cancellationToken)| 2020-04-27 09:45:18.5992|Hrst.AIDetect.WebApiServer.MqttJobHandler|INFO|<mqttNetLog>[2020-04-27T01:45:18.5991942Z] [] [27] [MqttClient] [Verbose]: Disconnecting [Timeout=00:00:30]|

Code example

the 3.0.2 mqttClient.cs private async Task DisconnectInternalAsync(Task sender, Exception exception, MqttClientAuthenticateResult authenticateResult) { var clientWasConnected = IsConnected;

        InitiateDisconnect();

        IsConnected = false;

        try
        {
            if (_adapter != null)
            {
                _logger.Verbose("Disconnecting [Timeout={0}]", Options.CommunicationTimeout);
                await _adapter.DisconnectAsync(Options.CommunicationTimeout, CancellationToken.None).ConfigureAwait(false);
            }

            await WaitForTaskAsync(_packetReceiverTask, sender).ConfigureAwait(false);
            await WaitForTaskAsync(_keepAlivePacketsSenderTask, sender).ConfigureAwait(false);

            _logger.Verbose("Disconnected from adapter.");
        }
        catch (Exception adapterException)
        {
            _logger.Warning(adapterException, "Error while disconnecting from adapter.");
        }
        finally
        {
            Dispose();
            _cleanDisconnectInitiated = false;

            _logger.Info("Disconnected.");

            var disconnectedHandler = DisconnectedHandler;
            if (disconnectedHandler != null)
            {
                await disconnectedHandler.HandleDisconnectedAsync(new MqttClientDisconnectedEventArgs(clientWasConnected, exception, authenticateResult)).ConfigureAwait(false);
            }
        }
    }

And my ClientOption is like this: private MqttClientOptions InitOption() { var tlsOptions = new MqttClientTlsOptions { UseTls = false, IgnoreCertificateChainErrors = true, IgnoreCertificateRevocationErrors = true, AllowUntrustedCertificates = true }; var options = new MqttClientOptions { ClientId = _serverUser.ClientId, ProtocolVersion = MQTTnet.Formatter.MqttProtocolVersion.V311, ChannelOptions = new MqttClientTcpOptions { Server = _filesUrl.MqttServerIp, Port = _filesUrl.MqttServerPort, TlsOptions = tlsOptions }, Credentials = new MqttClientCredentials { Username = _serverUser.Name, Password = _mqttPwd }, CommunicationTimeout = TimeSpan.FromSeconds(30), KeepAlivePeriod = TimeSpan.FromSeconds(60), CleanSession = true, }; return options; }

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 19

Most upvoted comments