MqttClient hangs on ConnectAsync
See original GitHub issueDescribe 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:
- Make sure that internet connection is off on a mobile device
- Start application
- Try to connect to a mqtt server
- 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:
- Created 3 years ago
- Comments:11
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

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?
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.