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.

Multiple connections in multithreaded environment fail

See original GitHub issue

Tried to use multiple NATS clients in multithreaded environment, but connections are failing with Invalid connection or Connection Request Time out when number of items scheduled in thread pool is above 6 (the number varies between 6 and 13).

This is my test code:

class Program
{
  static void Main(string[] args)
  {
    for (int i = 1; i < 16; i++)
    {
      QueueAndExecute(i);
    }

    Console.ReadLine();
  }

  private static void QueueAndExecute(int instances)
  {
    var finished = new CountdownEvent(1);

    for (var i = 0; i < instances; i++)
    {
      var instance = i + 1;

      finished.AddCount();
      ThreadPool.QueueUserWorkItem(state =>
      {
        var clientId = Guid.NewGuid().ToString("N");

        try
        {
          using (var c = new StanConnectionFactory().CreateConnection("test-cluster", clientId))
          {
            var bytes = Encoding.UTF8.GetBytes("this is test");

            c.Publish("test-channel", bytes);
            Console.WriteLine($"{instance}/{instances}: published");
          }
        }
        catch (Exception e)
        {
          Console.WriteLine($"{instance}/{instances}: {e.Message}");
        }

        finished.Signal();
      });
    }

    Console.WriteLine($"queued {instances}");
    finished.Signal();
    finished.Wait();
  }
}

Which produces following output:

queued 1
1/1: published
queued 2
2/2: published
1/2: published
queued 3
3/3: published
2/3: published
1/3: published
queued 4
1/4: published
3/4: published
2/4: published
4/4: published
queued 5
2/5: published
3/5: published
1/5: published
4/5: published
5/5: published
queued 6
3/6: published
1/6: published
2/6: published
6/6: published
5/6: published
4/6: published
queued 7
7/7: Invalid connection.
4/7: Invalid connection.
5/7: Invalid connection.
6/7: Connection Request Timed out.
2/7: Connection Request Timed out.
1/7: Connection Request Timed out.
3/7: published
queued 8
1/8: Invalid connection.
2/8: Invalid connection.
7/8: Invalid connection.
4/8: Invalid connection.
5/8: Invalid connection.
3/8: Invalid connection.
6/8: Invalid connection.
8/8: Invalid connection.
queued 9
1/9: Invalid connection.
2/9: Invalid connection.
3/9: Invalid connection.
4/9: Invalid connection.
5/9: Invalid connection.
6/9: Invalid connection.
7/9: Invalid connection.
8/9: Invalid connection.
9/9: Invalid connection.
queued 10
1/10: Invalid connection.
2/10: Invalid connection.
3/10: Invalid connection.
4/10: Invalid connection.
5/10: Invalid connection.
6/10: Invalid connection.
7/10: Invalid connection.
8/10: Invalid connection.
9/10: Invalid connection.
10/10: Invalid connection.
queued 11
1/11: Invalid connection.
2/11: Invalid connection.
3/11: Invalid connection.
4/11: Invalid connection.
5/11: Invalid connection.
6/11: Invalid connection.
7/11: Invalid connection.
8/11: Invalid connection.
9/11: Invalid connection.
10/11: Invalid connection.
11/11: Invalid connection.
queued 12
1/12: Invalid connection.
2/12: Invalid connection.
3/12: Invalid connection.
4/12: Invalid connection.
5/12: Invalid connection.
6/12: Invalid connection.
7/12: Invalid connection.
8/12: Invalid connection.
9/12: Invalid connection.
10/12: Invalid connection.
11/12: Invalid connection.
12/12: published
queued 13
1/13: Invalid connection.
2/13: Invalid connection.
3/13: Invalid connection.
4/13: Invalid connection.
5/13: Invalid connection.
6/13: Invalid connection.
7/13: Invalid connection.
8/13: Invalid connection.
9/13: Invalid connection.
10/13: Invalid connection.
11/13: Invalid connection.
12/13: Invalid connection.
13/13: Invalid connection.
queued 14
1/14: Invalid connection.
2/14: Invalid connection.
3/14: Invalid connection.
4/14: Invalid connection.
5/14: Invalid connection.
6/14: Invalid connection.
7/14: Invalid connection.
8/14: Invalid connection.
9/14: Invalid connection.
10/14: Invalid connection.
11/14: Invalid connection.
12/14: Invalid connection.
13/14: Invalid connection.
14/14: Invalid connection.
queued 15
1/15: Invalid connection.
2/15: Invalid connection.
3/15: Invalid connection.
4/15: Invalid connection.
5/15: Invalid connection.
6/15: Invalid connection.
7/15: Invalid connection.
8/15: Invalid connection.
9/15: Invalid connection.
10/15: Invalid connection.
11/15: Invalid connection.
12/15: Invalid connection.
13/15: Invalid connection.
14/15: Invalid connection.
15/15: published

I have nats-streaming-server version 0.10.2 running on my local machine (windows 10, 64-bit). Thread safety of creating connections seems to not be mentioned in documentation anywhere so I assume this should be allowed. When I lock around CreateConnection everything works fine.

Originally submitted by @mabzd as https://github.com/nats-io/csharp-nats/issues/224

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
senzacionalecommented, Apr 13, 2019

Any info about this issue?

0reactions
ColinSullivan1commented, Nov 26, 2019

@danielwertheim, thanks for testing! I’d consider this to be resolved… @senzacionale or others - if this continues to be an issue please re-open.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multiple connections in multithreaded environment fail #224
Tried to use multiple NATS clients in multithreaded environment, but connections are failing with Invalid connection or Connection Request Time ...
Read more >
Multithreading and database connection(s)
Single connection - in this case, the connection is created 1 times and is divided among all requests, that if a large number...
Read more >
Too Many Connections in a multi-threaded environment
In my new project, (in a multi-threaded environment using C++ under Linux), when a number of threads are making new database connections, the...
Read more >
Advantages and Disadvantages of a Multithreaded/ ...
Testing a multithreaded application is more difficult than testing a single-threaded application because defects are often timing-related and more difficult to ...
Read more >
Multithreading Issues in Java: Your options to find and fix
Identify and fix Java Multithreading Issues with Seagence - a tool designed expressly to find and help you fix multithreading issues.
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