Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Leaking" Thread Handles

I have a WCF application hosted in IIS (written in C#/.Net 4). Over time, the Handle Count of the process increases in a more or less linear fashion (increasing as high as 30,000 before the process recycles). According to SysInternals Process Explorer, the bulk of the handles that the process has are of type Thread. However, according to Performance Monitor, the number of threads is remaining more or less constant (around 40).

Clearly, I am doing something wrong and am leaking Thread Handles. However, I'm unclear exactly what a Thread Handle is in this context. I would have assumed that it is a handle to a thread, but since the number of threads is remaining consistent, I don't see how the handle count is ever increasing. And, I can't think of any way to keep a handle to a thread, while the thread itself goes away. Furthermore, I am not explicitely creating new threads (I am using the ThreadPool in places).

Clearly, I am missing something. But what?

like image 764
David Mullin Avatar asked Dec 11 '12 19:12

David Mullin


1 Answers

One can have handles to terminated threads. So the threads get created, terminate, but the handle remains.

Start Process Monitor (procmon.exe) and set it to listen to "Process and Thread Activity" (disable files, registry and network). Determine, who is creating threads by double-clicking thread create events and looking at the stack.

That should answer the question who is creating the threads. He is responsible for closing the handles.

like image 81
usr Avatar answered Sep 30 '22 14:09

usr