question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MqttClient hangs on ConnectAsync

See original GitHub issue

Describe the bug

IMqttClient instance hangs on ConnectAsync when trying to connect to a MQTT server while internet connection is off.

Which project is your bug related to?

  • Client

Basic Information

MQTTnet version: 3.0.11 Xamarin.Forms version: 4.7.0.1179 Platform: Android Device: Samsung SM-G937F, Android 10 - API 29

To Reproduce

Steps to reproduce the behavior:

  1. Make sure that internet connection is off on a mobile device
  2. Start application
  3. Try to connect to a mqtt server
  4. Application will hang on ConnectAsync method

Expected behavior

  • Client should cancel the operation and throw exception after cancellation is requested

Code example

var serverIpAddress = "176.222.222.222";
var serverPort = 1883;
try
{
    if (!Uri.TryCreate($"http://{serverIpAddress}:{serverPort}", UriKind.Absolute, out var uri) ||
        !IPAddress.TryParse(uri.Host, out _))
    {
        return;
    }

    var clientOptions = new MqttClientOptionsBuilder()
        .WithClientId(Guid.NewGuid().ToString())
        .WithCredentials(string.Empty, string.Empty)
        .WithTcpServer(uri.Host, uri.Port)
        .Build();

    var client = _mqttFactory.CreateMqttClient();

    // Timeout is one second
    using (var cancellationToken = new CancellationTokenSource(TimeSpan.FromSeconds(1)))
    {
        // Client hangs on a line below
        await client.ConnectAsync(clientOptions, cancellationToken.Token).ConfigureAwait(false);
    }
}
catch (Exception e)
{
    // ignored
}

ps. Let me know if you need example application/code 😃

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11

github_iconTop GitHub Comments

1reaction
chkr1011commented, Aug 1, 2020

Do you have more details or a full solution which has this issue? I also use MQTTnet in a Xamarin application and I cannot reproduce the issue.

What exactly do you mean with “internet connection is off”? Is Wifi AND Cellular just deactivated? Or is no WiFi in range etc?

0reactions
chkr1011commented, Aug 14, 2022

I assume this issue is fixed. If the issue still exists in the latest version 4 please reopen the ticket and try connecting with apps like MQTTnetApp to see if the library is the root cause of this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

MqttClient hangs on ConnectAsync · Issue #966
Steps to reproduce the behavior: Make sure that internet connection is off on a mobile device; Start application; Try to connect to a...
Read more >
Not able to connect with broker using MQTTNet library .net ...
So, I tested hivemq broker with mqtt.fx and works. Only in the code the return is connected = false. Any ideas? the error...
Read more >
[paho-dev] Synchronous connect() hangs
I have tried to implement an Mqtt client basically following the examples given in the source code and was able to get the...
Read more >
The Ultimate Guide on How to Use MQTT with Node.js
Learn how to use MQTT in Node.js with the help of use cases and advanced examples covering implementation of QoS, Topic Alias, Debugging, ......
Read more >
asyncio-mqtt
Idiomatic asyncio wrapper around paho-mqtt.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found