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.

ConnectAsync fails when many Tasks are active

See original GitHub issue

Describe the bug

For load tests we created an application that contains of a lot of Tasks (via Task.Run) which publish MQTT messages. Unfortunately ConnectAsync does not succeed to have a stable connection when a lot of Tasks are active. Note that the CPU load was very low since the Tasks didn’t do very much.

This may seem like a “toy code” problem. But our production application has the same problems, too. As soon as a few dozen Tasks are running an MQTT connection is not possible anymore.

Which project is your bug related to?

  • Client (3.0.11.0 .NET 4.5.2)

To Reproduce

Steps to reproduce the behavior:

  1. Create 100 Tasks
  2. Connect to an MQTT broker
  3. Connect won’t succeed

Expected behavior

ConnectAsync would be successful

### Code example
 Please provide full code examples below where possible to make it easier for the developers to check your issues.
 
```csharp
            // LOGGING
            MqttNetGlobalLogger.LogMessagePublished += (s, e) =>
            {
                var trace = $">> [{e.TraceMessage.Timestamp:O}] [{e.TraceMessage.ThreadId}] [{e.TraceMessage.Source}] [{e.TraceMessage.Level}]: {e.TraceMessage.Message}";
                if (e.TraceMessage.Exception != null)
                {
                    trace += Environment.NewLine + e.TraceMessage.Exception.ToString();
                }

                Console.WriteLine(trace);
            };

            // CREATE TASKS (which do almost nothing)
            bool run = true;

            for (int i = 0; i < 100; i++)
            {
                Task.Run(() =>
                {
                    while (run)
                    {
                        Thread.Sleep(100);
                    }
                });
            }

            Thread.Sleep(10000);

            // CONNECTING
            var options = new MqttClientOptionsBuilder()
                .WithClientId(Guid.NewGuid().ToString())
                .WithTcpServer("localhost",1883)
                .WithCleanSession()
                .Build();

            var result = mqttClient.ConnectAsync(options).Result;  // THIS WILL NOT SUCCEED

            Thread.Sleep(10000);
            run = false;

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
OidaTiftlacommented, Aug 3, 2020

I’m glad I could help.

0reactions
SeppPennercommented, Aug 5, 2020

Good to know that this is solved. Thank you @OidaTiftla. By the way, cool user name 😅

Read more comments on GitHub >

github_iconTop Results From Across the Web

ConnectAsync fails when many Tasks are active · Issue #968
Describe the bug For load tests we created an application that contains of a lot of Tasks (via Task.Run) which publish MQTT messages....
Read more >
ConnectAsync() never finishes in C# Visual Studio
ConnectAsync () (RFCOMM) sometimes finishes and sometimes it takes an extraordinarily long time to fail ("No more data is available. (Exception ...
Read more >
A method that calls multiple async tasks with error handling ...
According to my understanding your piece of software can finish in one of the following states: Succeeded; Failed; Timed out.
Read more >
C# Socket BeginConnect give a connection n seconds to ...
You're trying to connect to a machine and port that isn't listening for a connection. Confirm that the IP address/port is actively listening, ......
Read more >
C# Async/Await Interview Questions And Answers (2023)
How does the Task.WhenAll method work when handling multiple async tasks, and what potential issues could arise from its usage in C#?. Answer....
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