I've got an ASP.NET Core 2 app that is leaking threads. How can I determine why the threads never die?
If I run the app for 10 minutes in production, IIS starts spitting out 502.3 Bad Gateway errors. I loaded up Process Explorer and looked at the app->Properties->Threads, and I see an ever-increasing thread count:

Nearly all the threads are stuck with the following native stack trace:
These native stack traces don't tell me much, other than my web app is leaking threads. While Process Explorer can show me .NET stack traces for full .NET framework apps, it doesn't show me .NET stack traces for .NET Core apps.
So, how does one go about debugging such an issue?
I'm unable to repro this in my local dev environment. It appears to be related to high traffic or to my production server environment.
Things I've tried so far:
I setup Remote Debugging on my server and was able to connect to the app in unhealthy state from my dev box.
Once connected to this app in Visual Studio, I was able to pause and inspect the threads. I see they are waiting on a database resource. That opens up another question, but it is off-topic for this particular StackOverflow question.
I'm going to mark this as resolved. Lesson here is you can use Remote Debugger + Visual Studio Threads window to shed light on the thread leak problems.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With