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.

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:closed
  • Created 4 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
JanEggerscommented, Jun 18, 2020

@SeppPenner yes this was fixed with #833

1reaction
ps-webercommented, Aug 12, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

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