xamarin / mqtt

Minimalist and intuitive MQTT broker and client written entirely in C#

Home Page:http://xamarin.github.io/mqtt

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MqttServer application crash if URL is accessed through browser

andersbusk opened this issue · comments

I have a simple implementation of the server as such:
MqttConfiguration config = new MqttConfiguration() { AllowWildcardsInTopicFilters = true, ConnectionTimeoutSecs = 10, KeepAliveSecs = 10, WaitTimeoutSecs = 10, Port = port, MaximumQualityOfService = MqttQualityOfService.AtLeastOnce }; _server = MqttServer.Create(config, null, _authProv);

I'm starting the server after doing this and everything works perfectly. But if I access the site through a browser (https://localhost:port) I will receive this error:

System.NullReferenceException HResult=0x80004003 Message=Object reference not set to an instance of an object. Source=System.Net.Mqtt.Server StackTrace: at System.Net.Mqtt.Sdk.ServerPacketListener.<NotifyErrorAsync>d__25.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult() at System.Net.Mqtt.Sdk.ServerPacketListener.<HandleConnectionExceptionAsync>d__21.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult() at System.Net.Mqtt.Sdk.ServerPacketListener.<<ListenFirstPacket>b__17_1>d.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__139_1(Object state) at System.Threading.QueueUserWorkItemCallbackDefaultContext.Execute() at System.Threading.ThreadPoolWorkQueue.Dispatch() at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

Hosting it on a remote system I receive another error when accessing the IP and port of this server. Yet again all works if nobody accesses the URL.

Unhandled exception. Unhandled exception. Unhandled exception. System.ArgumentOutOfRangeException: Index and count must refer to a location within the buffer. (Parameter 'bytes') at System.Text.UTF8Encoding.GetString(Byte[] bytes, Int32 index, Int32 count) at System.Net.Mqtt.Sdk.ByteExtensions.GetString(Byte[] bytes, Int32 index, Int32& nextIndex) at System.Net.Mqtt.Sdk.Formatters.PublishFormatter.Read(Byte[] bytes) at System.Net.Mqtt.Sdk.Formatters.Formatter1.<>c__DisplayClass4_0.b__0()
at System.Threading.Tasks.Task1.InnerInvoke() at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state) --- End of stack trace from previous location where exception was thrown --- at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread) --- End of stack trace from previous location where exception was thrown --- at System.Net.Mqtt.Sdk.Formatters.Formatter1.FormatAsync(Byte[] bytes)
at System.Net.Mqtt.Sdk.PacketManager.GetPacketAsync(Byte[] bytes)
at System.Net.Mqtt.Sdk.PacketChannel.<.ctor>b__7_0(Byte[] bytes)
at System.Threading.Tasks.Task.<>c.b__139_1(Object state)
at System.Threading.QueueUserWorkItemCallbackDefaultContext.Execute()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
System.ArgumentOutOfRangeException: Index and count must refer to a location within the buffer. (Parameter 'bytes')
at System.Text.UTF8Encoding.GetString(Byte[] bytes, Int32 index, Int32 count)
at System.Net.Mqtt.Sdk.ByteExtensions.GetString(Byte[] bytes, Int32 index, Int32& nextIndex)
at System.Net.Mqtt.Sdk.Formatters.PublishFormatter.Read(Byte[] bytes)
at System.Net.Mqtt.Sdk.Formatters.Formatter1.<>c__DisplayClass4_0.<FormatAsync>b__0() at System.Threading.Tasks.Task1.InnerInvoke()
at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location where exception was thrown ---
at System.Net.Mqtt.Sdk.Formatters.Formatter1.FormatAsync(Byte[] bytes) at System.Net.Mqtt.Sdk.PacketManager.GetPacketAsync(Byte[] bytes) at System.Net.Mqtt.Sdk.PacketChannel.<.ctor>b__7_0(Byte[] bytes) at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__139_1(Object state) at System.Threading.QueueUserWorkItemCallbackDefaultContext.Execute() at System.Threading.ThreadPoolWorkQueue.Dispatch() Unhandled exception. Unhandled exception. System.ArgumentOutOfRangeException: Index and count must refer to a location within the buffer. (Parameter 'bytes') at System.Text.UTF8Encoding.GetString(Byte[] bytes, Int32 index, Int32 count) at System.Net.Mqtt.Sdk.ByteExtensions.GetString(Byte[] bytes, Int32 index, Int32& nextIndex) at System.Net.Mqtt.Sdk.Formatters.PublishFormatter.Read(Byte[] bytes) at System.Net.Mqtt.Sdk.Formatters.Formatter1.<>c__DisplayClass4_0.b__0()
at System.Threading.Tasks.Task1.InnerInvoke() at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state) --- End of stack trace from previous location where exception was thrown --- at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread) --- End of stack trace from previous location where exception was thrown --- at System.Net.Mqtt.Sdk.Formatters.Formatter1.FormatAsync(Byte[] bytes)
at System.Net.Mqtt.Sdk.PacketManager.GetPacketAsync(Byte[] bytes)
at System.Net.Mqtt.Sdk.PacketChannel.<.ctor>b__7_0(Byte[] bytes)
at System.Threading.Tasks.Task.<>c.b__139_1(Object state)
at System.Threading.QueueUserWorkItemCallbackDefaultContext.Execute()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
at System.Net.Mqtt.Sdk.ServerPacketListener.NotifyErrorAsync(Exception exception)
at System.Net.Mqtt.Sdk.ServerPacketListener.b__18_1(Exception ex)
at System.Threading.Tasks.Task.<>c.b__139_1(Object state)
at System.Threading.QueueUserWorkItemCallbackDefaultContext.Execute()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Net.Mqtt.Sdk.ServerPacketListener.NotifyErrorAsync(Exception exception)
at System.Net.Mqtt.Sdk.ServerPacketListener.HandleConnectionExceptionAsync(Exception exception)
at System.Net.Mqtt.Sdk.ServerPacketListener.b__17_1(Exception ex)
at System.Threading.Tasks.Task.<>c.b__139_1(Object state)
at System.Threading.QueueUserWorkItemCallbackDefaultContext.Execute()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Net.Mqtt.Sdk.ServerPacketListener.NotifyErrorAsync(Exception exception)
at System.Net.Mqtt.Sdk.ServerPacketListener.b__19_1(Exception ex)
at System.Threading.Tasks.Task.<>c.b__139_1(Object state)
at System.Threading.QueueUserWorkItemCallbackDefaultContext.Execute()
at System.Threading.ThreadPoolWorkQueue.Dispatch()`

The implementation is made through a console application in dotnetcore 3.0. I want to avoid the server crashing when somebody accesses the URL as the server works just fine until then. I cannot catch the exception as it happens in a thread inside the Nuget Package of the MqttServer.

Please tell me if I can provide anything else.

HI
can you share me the whole code
i am trying to implement a mqtt server but not able