Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Troubleshoot Handle Leak in C# Windows Service

I have a .NET 4 Windows Service written in C# running on Windows Server 2008, which when monitored with PerfMon, appears to consume 2,500 handles per hour. The "Handle Count" counter climbs ever upwards.

When I look at the handles using ProcExp with the "Show Unnamed Handles and Mappings" option selected, there are thousands of Event and Semaphore handles listed. Most seem unnamed. I looked in the source code, and there are no explicit uses of AutoResetEvent or ManualResetEvent.

The Private Bytes counter climbs and falls. I haven't watched the process for more than a couple of hours but will leave it being monitored overnight.

The service runs on a remote server, so I can't directly attach a debugger, and will probably have to dump the process and analyse it with WinDBG or similar.

Is it possible to locate one of the handles and find out what it points to and/or what might "own" it? If so how?

In response to comments, here's a summary of what the service does.

  • hosts some WCF services
  • hosts an instance of Quartz.NET scheduler
  • hosts a work item queue (quartz jobs push work items onto a queue rather than do long running work themselves)
  • hosts a work item executor (work items are dequeued on a timer and executed away from Quartz)
  • hosts a custom cache manager (which does a lot of ADO.NET work)
like image 715
IanT8 Avatar asked Nov 13 '22 12:11

IanT8


1 Answers

The fundamental concept behind troubleshooting is isolation. Isolation in terms of processes and state.

The first thing you need to do is to reproduce the problem in an environment that you control. Try setting this up on your dev machine, if there are services that would take too long to set up in a dev environment consider mocking them out.

Once you are in control of the environment you can start paring back the code being executed by mocking it or stubbing it out.

like image 181
Slugart Avatar answered Nov 16 '22 03:11

Slugart