0

I converted our application from C# on Windows to .Net Core running on Linux with Ryder IDE framework.

Our app uses several third-party frameworks such as ServiceStack, RabbitMq and Mailkit libraries.

When I open the Debug Output window I see that the system is starting and stopping threads at the rate of over 10 a second.

Started Thread 18213
Exited Thread 18213
Started Thread 18214
Exited Thread 18214
Started Thread 18215
Exited Thread 18215
Started Thread 18216
Exited Thread 18216
Started Thread 18217
Exited Thread 18217
Started Thread 18218

As there are 100's of classes I have no idea where to start to see which method is started and stopping threads at this rate.

How can I track this down?

  • Is there a method that I could overload on the thread pool that would allow us to set a breakpoint when these threads start and stop.

  • I've attempted to enable settings > Build, Execution > Debugger > Process exceptions outside of my code And I see a lot of exceptions being thrown in the RabbitMQ library, but I have no idea if this relates to the Thread cycling issue.

  • I enabled the System.Treading.ThreadStartExcepton and it wasn't hit.

Any help would be appreciated.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
mbalsam
  • 611
  • 1
  • 6
  • 16
  • It would be awesome if you could provide a [mcve] – jazb May 16 '19 at 02:41
  • The application is over 100K lines long. I have no idea how i could do that. Suggestions? – mbalsam May 16 '19 at 02:47
  • 1
    @mbalsam that's why MCVEs are needed. If you need the entire application to debug or troubleshoot an issue, finding and fixing problems becomes orders of magnitude harder. Isolate *small* parts into a separate application (a simple console app should do) and check how they behave. I suspect there's nothing wrong and no exceptions at all, just async/multithreaded libraries doing their job – Panagiotis Kanavos May 16 '19 at 10:43
  • 1
    If you want to see what's going on in your application, profile it and check what's going on. IntelliTrace can record thread events already. Visual Studio's profiler can capture and visualize task, thread events etc. The [Concurrency Visualizer](https://learn.microsoft.com/en-us/visualstudio/profiling/common-patterns-for-poorly-behaved-multithreaded-applications?view=vs-2019) extension can visualize all that, show which task run on which core, which task or thread was blocked by which etc. – Panagiotis Kanavos May 16 '19 at 10:47
  • 2
    Since you use Rider, you should check [dotTrace](https://www.jetbrains.com/profiler/). The landing page shows a visualization of thread events similar to Concurrency Visualizer's views – Panagiotis Kanavos May 16 '19 at 10:53
  • @PanagiotisKanavos has a point - good pieces of advice – Igor Cova May 16 '19 at 11:03
  • I get that MCVE's are helpful in a perfect world, of unlimited time and resources.. What i'm looking for is for a way to isolate the issue to a portion of the app. Then i would be able to create a MCVE of the problem. – mbalsam May 16 '19 at 13:45
  • Thanks @PanagiotisKanavos the Concurrent Visualize is exactly what im looking for!! – mbalsam May 16 '19 at 13:47

2 Answers2

1

Welcome to thread pool .Net Core and async-await in general.

What is the problem do you have for here?

Those outputs Started Thread and Exited Thread are not bugs in your code it's by design of Asp.Net Core framework

A thread pool is a pool of worker threads that is available on demand as needed. The code examples in this article show how to use the thread pool in .NET Core using C#.

Igor Cova
  • 3,126
  • 4
  • 31
  • 57
1

Unfortunately there's no mechanism for tracking what is starting or stopping threads in a .NET application. If you examine a thread in a debugger, you can see the call-stack of the thread, including (at the base of the stack) the method which was called to start the thread doing work. This might give you some information as to what the thread was supposed to be doing.

The reality is that if it's not your code, you probably can't do much about how these threads are being managed. It's likely you're seeing the normal behaviour of the .NET thread pool. Unless you're seeing a problem with a large number of threads or very high thread turnover, best to not pull this thread.

Paul Turner
  • 38,949
  • 15
  • 102
  • 166
  • Those events are available to profilers and Intellitrace. They are already captured and visualized by Visual Studio's profiler, dotTrace and the IntelliTrace window – Panagiotis Kanavos May 16 '19 at 10:54