MQTTnet
MQTTnet copied to clipboard
MqttClient hangs on ConnectAsync
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:
- 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 :)
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?
Hi, here is the sample: https://github.com/fdundjer/MqttPlayground Deactivate Wifi and Cellular, so no connection at all. Type IP: 176.222.222.222 and port 1883 in input fields.
it's a huge bug... any news?
I have the same issue, but when Server is down then ManagedMqttClient is hang up ... When it will be fixed ? Is there any news ?
@chkr1011 Seems like it use too many threads ...
"Thread Pool Worker"
"Thread Pool Worker"09-29 01:00:17.671 I/system_server( 1285): libdebuggerd_client: started dumping process 11552
09-29 01:00:17.671 D/AppEyeBinderBlock( 1285): read binder transaction begin
09-29 01:00:17.671 I/system_server( 1285): libdebuggerd_client: started dumping process 11552
09-29 01:00:17.672 I//system/bin/tombstoned( 763): registered intercept for pid 11552 and type kDebuggerdJavaBacktrace
09-29 01:00:17.675 E/mono (11552): Full thread dump:
"Timer-Scheduler" at <unknown> <0xffffffff>
at (wrapper managed-to-native) System.Threading.WaitHandle.Wait_internal (intptr*,int,bool,int) <0x00007>
at System.Threading.WaitHandle.WaitOneNative (System.Runtime.InteropServices.SafeHandle,uint,bool,bool) [0x00019] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/corlib/System.Threading/WaitHandle.cs:90
at System.Threading.WaitHandle.InternalWaitOne (System.Runtime.InteropServices.SafeHandle,long,bool,bool) [0x00014] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/referencesource/mscorlib/system/threading/waithandle.cs:259
at System.Threading.WaitHandle.WaitOne (long,bool) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/referencesource/mscorlib/system/threading/waithandle.cs:248
at System.Threading.WaitHandle.WaitOne (int,bool) [0x00019] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/referencesource/mscorlib/system/threading/waithandle.cs:215
at System.Threading.WaitHandle.WaitOne (int) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/referencesource/mscorlib/system/threading/waithandle.cs:236
at System.Threading.Timer/Scheduler.SchedulerThread () [0x0003c] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/corlib/System.Threading/Timer.cs:293
at System.Threading.ThreadHelper.ThreadStart_Context (object) [0x00014] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/referencesource/mscorlib/system/threading/thread.cs:74
at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext,System.Threading.ContextCallback,object,bool) [0x00071] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/referencesource/mscorlib/system/threading/executioncontext.cs:968
at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext,System.Threading.ContextCallback,object,bool) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/referencesource/mscorlib/system/threading/executioncontext.cs:910
at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext,System.Threading.ContextCallback,object) [0x0002b] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/referencesource/mscorlib/system/threading/executioncontext.cs:899
at System.Threading.ThreadHelper.ThreadStart () [0x00008] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/referencesource/mscorlib/system/threading/thread.cs:111
at (wrapper runtime-invoke) object.runtime_invoke_void__this__ (object,intptr,intptr,intptr) [0x00067] in <0381c8f952fb47759ee29160e807b17d>:0
"Thread Pool Worker"
"Thread Pool Worker"
"<threadpool thread>"
"Thread Pool I/O Selector"09-29 01:00:17.687 I/TrafficMonitor( 1285): expired arrive. level:-1
09-29 01:00:17.688 I/TrafficMonitor( 1285): update:rxPkts:2,txPkts:0,rxBytes:402,txBytes:0
09-29 01:00:17.689 I/TrafficMonitor( 1285): start expired. level:-1
09-29 01:00:17.689 I/TrafficMonitor( 1285): gettimer:interval=5000
09-29 01:00:17.689 I/TrafficMonitor( 1285): during_ms:5002,period_ms:2000
09-29 01:00:17.689 I/TrafficMonitor( 1285): count:523,rx_sum:2,tx_sum:0,rxBytes:402,during_ms:5002,rx_speed:80.36785285885647,tx_speed:0.0,rto:0.0
"<unnamed thread>" at <unknown> <0xffffffff>
at System.Collections.Generic.List`1<Xamarin.Forms.WeakEventManager/Subscription>.EnsureCapacity (int) [0x00036] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/Common/src/CoreLib/System/Collections/Generic/List.cs:426
at System.Collections.Generic.List`1<Xamarin.Forms.WeakEventManager/Subscription>.AddWithResize (Xamarin.Forms.WeakEventManager/Subscription) [0x00007] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/Common/src/CoreLib/System/Collections/Generic/List.cs:231
at System.Collections.Generic.List`1<Xamarin.Forms.WeakEventManager/Subscription>.Add (Xamarin.Forms.WeakEventManager/Subscription) [0x00034] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/Common/src/CoreLib/System/Collections/Generic/List.cs:222
at Xamarin.Forms.WeakEventManager.AddEventHandler (string,object,System.Reflection.MethodInfo) [0x00034] in D:\a\1\s\Xamarin.Forms.Core\WeakEventManager.cs:116
at Xamarin.Forms.WeakEventManager.AddEventHandler (System.EventHandler,string) [0x00021] in D:\a\1\s\Xamarin.Forms.Core\WeakEventManager.cs:34
at Xamarin.Forms.ImageSource.add_SourceChanged (System.EventHandler) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\ImageSource.cs:150
at Xamarin.Forms.ImageElement.OnImageSourceChanged (Xamarin.Forms.BindableObject,object,object) [0x00014] in D:\a\1\s\Xamarin.Forms.Core\ImageElement.cs:25
at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty,Xamarin.Forms.BindableObject/BindablePropertyContext,object,bool,Xamarin.Forms.Internals.SetValueFlags,bool) [0x00120] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:463
at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty,object,Xamarin.Forms.Internals.SetValueFlags,Xamarin.Forms.BindableObject/SetValuePrivateFlags) [0x00173] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:397
at Xamarin.Forms.BindingExpression.ApplyCore (object,Xamarin.Forms.BindableObject,Xamarin.Forms.BindableProperty,bool) [0x00214] in D:\a\1\s\Xamarin.Forms.Core\BindingExpression.cs:155
at Xamarin.Forms.BindingExpression.Apply (object,Xamarin.Forms.BindableObject,Xamarin.Forms.BindableProperty) [0x0006b] in D:\a\1\s\Xamarin.Forms.Core\BindingExpression.cs:72
at Xamarin.Forms.Binding.Apply (object,Xamarin.Forms.BindableObject,Xamarin.Forms.BindableProperty,bool) [0x0006d] in D:\a\1\s\Xamarin.Forms.Core\Binding.cs:138
at Xamarin.Forms.BindableObject.ApplyBindings (bool,bool) [0x00041] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:480
at Xamarin.Forms.BindableObject.SetInheritedBindingContext (Xamarin.Forms.BindableObject,object) [0x0005a] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:209
at Xamarin.Forms.Element.SetChildInheritedBindingContext (Xamarin.Forms.Element,object) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\Element.cs:475
at Xamarin.Forms.Element.<OnBindingContextChanged>b__82_0 (Xamarin.Forms.BindableObject,object) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\Element.cs:306
at Xamarin.Forms.BindableObjectExtensions.PropagateBindingContext<T_REF> (Xamarin.Forms.BindableObject,System.Collections.Generic.IList`1<T_REF>,System.Action`2<Xamarin.Forms.BindableObject, object>) [0x0002c] in D:\a\1\s\Xamarin.Forms.Core\BindableObjectExtensions.cs:28
at Xamarin.Forms.Element.OnBindingContextChanged () [0x00000] in D:\a\1\s\Xamarin.Forms.Core\Element.cs:304
at Xamarin.Forms.VisualElement.OnBindingContextChanged () [0x00006] in D:\a\1\s\Xamarin.Forms.Core\VisualElement.cs:769
at Xamarin.Forms.View.OnBindingContextChanged () [0x0000c] in D:\a\1\s\Xamarin.Forms.Core\View.cs:158
at Xamarin.Forms.Grid.OnBindingContextChanged () [0x00006] in D:\a\1\s\Xamarin.Forms.Core\Grid.cs:149
at Xamarin.Forms.BindableObject.BindingContextPropertyChanged (Xamarin.Forms.BindableObject,object,object) [0x0000f] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:500
at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty,Xamarin.Forms.BindableObject/BindablePropertyContext,object,bool,Xamarin.Forms.Internals.SetValueFlags,bool) [0x00120] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:463
at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty,object,Xamarin.Forms.Internals.SetValueFlags,Xamarin.Forms.BindableObject/SetValuePrivateFlags) [0x00173] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:397
at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty,object,bool,bool) [0x00042] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:334
at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty,object) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:311
at Xamarin.Forms.BindableObject.set_BindingContext (object) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:41
at Xamarin.Forms.Platform.Android.ShellFlyoutRecyclerAdapter/ElementViewHolder.set_Element (Xamarin.Forms.Element) [0x0004d] in D:\a\1\s\Xamarin.Forms.Platform.Android\Renderers\ShellFlyoutRecyclerAdapter.cs:272
at Xamarin.Forms.Platform.Android.ShellFlyoutRecyclerAdapter.OnBindViewHolder (Android.Support.V7.Widget.RecyclerView/ViewHolder,int) [0x0002a] in D:\a\1\s\Xamarin.Forms.Platform.Android\Renderers\ShellFlyoutRecyclerAdapter.cs:77
at Android.Support.V7.Widget.RecyclerView/Adapter.n_OnBindViewHolder_Landroid_support_v7_widget_RecyclerView_ViewHolder_I (intptr,intptr,intptr,int) [0x00012] in <844c913cb32f4510879031be59eb1dce>:0
at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.63 (intptr,intptr,intptr,int) [0x0001d] in <0623d8432cef4751a77985c521d75c2f>:0
at (wrapper native-to-managed) Android.Runtime.DynamicMethodNameCounter.63 (intptr,intptr,intptr,int) [0x00035] in <0623d8432cef4751a77985c521d75c2f>:0
at <unknown> <0xffffffff>
at (wrapper managed-to-native) Java.Interop.NativeMethods.java_interop_jnienv_call_nonvirtual_void_method_a (intptr,intptr&,intptr,intptr,intptr,intptr) <0x00007>
at Java.Interop.JniEnvironment/InstanceMethods.CallNonvirtualVoidMethod (Java.Interop.JniObjectReference,Java.Interop.JniObjectReference,Java.Interop.JniMethodInfo,Java.Interop.JniArgumentValue*) [0x00079] in <3ffd030629264cf9a5adda85ea742eb9>:0
at Java.Interop.JniPeerMembers/JniInstanceMethods.InvokeNonvirtualVoidMethod (string,Java.Interop.IJavaPeerable,Java.Interop.JniArgumentValue*) [0x00021] in <3ffd030629264cf9a5adda85ea742eb9>:0
at Android.Views.ViewGroup.Layout (int,int,int,int) [0x00069] in <0623d8432cef4751a77985c521d75c2f>:0
at Xamarin.Forms.Platform.Android.ShellRenderer.OnElementSizeChanged (object,System.EventArgs) [0x00092] in D:\a\1\s\Xamarin.Forms.Platform.Android\Renderers\ShellRenderer.cs:315
at Xamarin.Forms.VisualElement.SetSize (double,double) [0x00029] in D:\a\1\s\Xamarin.Forms.Core\VisualElement.cs:1047
at Xamarin.Forms.VisualElement.set_Bounds (Xamarin.Forms.Rectangle) [0x0005d] in D:\a\1\s\Xamarin.Forms.Core\VisualElement.cs:357
at Xamarin.Forms.VisualElement.Layout (Xamarin.Forms.Rectangle) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\VisualElement.cs:724
at Xamarin.Forms.Platform.Android.AppCompat.Platform.LayoutRootPage (Xamarin.Forms.Page,int,int) [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.Android\AppCompat\Platform.cs:405
at Xamarin.Forms.Platform.Android.AppCompat.Platform.Xamarin.Forms.Platform.Android.IPlatformLayout.OnLayout (bool,int,int,int,int) [0x0000c] in D:\a\1\s\Xamarin.Forms.Platform.Android\AppCompat\Platform.cs:232
at Xamarin.Forms.Platform.Android.PlatformRenderer.OnLayout (bool,int,int,int,int) [0x0001a] in D:\a\1\s\Xamarin.Forms.Platform.Android\PlatformRenderer.cs:75
at Android.Views.ViewGroup.n_OnLayout_ZIIII (intptr,intptr,bool,int,int,int,int) [0x00010] in <0623d8432cef4751a77985c521d75c2f>:0
at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.38 (intptr,intptr,bool,int,int,int,int) [0x0002f] in <0623d8432cef4751a77985c521d75c2f>:0
at (wrapper native-to-managed) Android.Runtime.DynamicMethodNameCounter.38 (intptr,intptr,int,int,int,int,int) [0x00045] in <0623d8432cef4751a77985c521d75c2f>:0
"Thread Pool Worker"
"Thread Pool Worker" at <unknown> <0xffffffff>
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback () <0x00007>
at (wrapper runtime-invoke) <Module>.runtime_invoke_bool (object,intptr,intptr,intptr) [0x0002b] in <0381c8f952fb47759ee29160e807b17d>:0
"Finalizer"
"Thread Pool Worker"
"Debugger agent"
"Thread Pool Worker"
"Thread Pool Worker" at <unknown> <0xffffffff>
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback () <0x00007>
at (wrapper runtime-invoke) <Module>.runtime_invoke_bool (object,intptr,intptr,intptr) [0x0002b] in <0381c8f952fb47759ee29160e807b17d>:0
"<threadpool thread>"
"Thread Pool Worker"09-29 01:00:17.888 D/AppEyeBinderBlock( 1285): read binder transaction end
@chkr1011 I have figured out that hanging up come in Debug mode for Xamarin application, seems like debugger use some thread that with addition with failed connection to broker hanging up
When I run application without debug mode, just tap on icon, it starts properly ...
Also when I add _managedMqttClient.ConnectingFailedHandler than even in Debug mode application works properly after connection is failed ...
@redradist What dies "add" the connection failed handler? Do you have also some code in the handler or only attached?
@chkr1011 Seems like this my issue was due to Rider IDE ... On Visual Studio issue is not reproducible
If you interested there is ticket for it https://youtrack.jetbrains.com/issue/RIDER-52622
@fdundjer Sorry for the long delay. Is this issue still relevant? I am not sure how to solve this because this library only makes use of sockets. Probably it is required to check availability of network via Xamarin APIs first?
@fdundjer Sorry for the long delay. Is this issue still relevant? I am not sure how to solve this because this library only makes use of sockets. Probably it is required to check availability of network via Xamarin APIs first?
Though not a Xamarin specific issue, I'm having an issue with the ConnectAsync method just swallowing the thread if the broker is not available at that time. No exception is returned so I cannot handle this behaviour in code. It's a very straight forward setup using node-red as a temp broker. If that node-red broker isn't running the client just hangs forever which is something I definitely don't want when this is turned into production code with a real broker
var factory = new MqttFactory();
_mqttClient = factory.CreateMqttClient();
var options = new MqttClientOptionsBuilder().WithTcpServer("localhost", 1883).Build();
await _mqttClient.ConnectAsync(options);
@ReikoLJ have a look at https://github.com/chkr1011/MQTTnet/pull/1318
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.