ConnectAsync fails when many Tasks are active
See original GitHub issueDescribe 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:
- Create 100 Tasks
- Connect to an MQTT broker
- 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:
- Created 3 years ago
- Comments:5
Top 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 >
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

I’m glad I could help.
Good to know that this is solved. Thank you @OidaTiftla. By the way, cool user name 😅