Sporadic "MqttCommunicationException: Cannot access a disposed object" on MqttClient.DisconnectAsync()
See original GitHub issue.NET Version: Framework 4.7.2 MQTTnet nuget Version: 3.0.5
I have a sporadically occurring problem when calling the Method IMqttClient.DisconnectAsync()
.
The messages are transmitted over unencrypted TCP.
The MQTT server is a Mosquitto 1.6.3. The log messages just say that the client has disconnected, as expected.
My Code is roughly:
IMqttClient _mqttClient;
void Init()
{
var factory = new MqttFactory();
_mqttClient = factory.CreateMqttClient();
}
async Task SendMessage(MqttApplicationMessage message)
{
try
{
await _mqttClient.ConnectAsync(options);
await _mqttClient.PublishAsync(message);
}
finally
{
await _mqttClient.DisconnectAsync();
}
}
I get the following stacktrace:
MqttCommunicationException: Cannot access a disposed object.
Object name: 'System.Net.Sockets.NetworkStream'.
--------------------------------------------------------------------------------
*** Stacktrace ***
at MQTTnet.Adapter.MqttChannelAdapter.WrapException(Exception exception)
at MQTTnet.Adapter.MqttChannelAdapter.<SendPacketAsync>d__34.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MQTTnet.Client.MqttClient.<DisconnectAsync>d__34.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at MQTTnet.Client.MqttClient.<DisconnectAsync>d__34.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
... My Code ...
--------------------------------------------------------------------------------
*** Inner Exception ***
ObjectDisposedException: Cannot access a disposed object.
Object name: 'System.Net.Sockets.NetworkStream'.
----------------------------------------------------------------------------
*** Stacktrace ***
at System.Net.Sockets.NetworkStream.EndWrite(IAsyncResult asyncResult)
at System.IO.Stream.<>c.<BeginEndWriteAsync>b__53_1(Stream stream, IAsyncResult asyncResult)
at System.Threading.Tasks.TaskFactory`1.FromAsyncTrimPromise`1.Complete(TInstance thisRef, Func`3 endMethod, IAsyncResult asyncResult, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MQTTnet.Implementations.MqttTcpChannel.<WriteAsync>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MQTTnet.Internal.MqttTaskTimeout.<WaitAsync>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (1 by maintainers)
Top Results From Across the Web
Cannot access a disposed object" on MqttClient. ...
The MQTT server is a Mosquitto 1. ... Sporadic "MqttCommunicationException: Cannot access a disposed object" on ... DisconnectAsync() #728.
Read more >Cannot access a disposed object" on MqttClient.DisconnectAsync()
Sporadic "MqttCommunicationException: Cannot access a disposed object" on MqttClient.DisconnectAsync(). MQTTnet. 31 July 2019 Posted by ps-weber.
Read more >Cannot access a disposed object. A common cause of this ...
This may occur if you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using...
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
@SeppPenner yes this was fixed with #833
Sorry, I forgot to add the “async Task” when trying to come up with a minimal example. I edited the issue to match my actual code.