I'm trying to ensure that a Windows service program (running on top of .NET) is properly releasing its network connections.
When running the service locally I know that it will create a lot of HTTP connections to localhost on port 57300. I'm using netstat
to monitor whether they are released properly.
I was suprised to see that many connections to this port are owned by the "System Idle process" (PID=0).
Here we can see that only three of those connections are owned by the service program (PID=5012). All the others are owned by PID 0.
My main questions are: Why is this happening? and Do I need to care?
But I'd also like to know:
Does this mean that the service program did release the connection properly, or not?
Will such connections be reused if needed?
Do such a connection "reserve a slot" in the .NET ServicePointManager?
The PID column shows the process identifier (PID) associated with the TCP connection. The PID is the information you're. after, but few people can identify a process by its PID. To get the name of the process, you can run the following Tasklist command: tasklist /v /fo List /fi "pid eq xxx"
Under Active Connections, look at the PID (Process ID) column, and record any PID numbers that are repeated for many IP addresses. A PID with many connections may be using a lot of bandwidth. Open the Windows Task Manager by pressing CTRL-ALT-DELETE on your keyboard. Click Start Task Manager.
To free up a TCP/IP port that your application application has previously bound to, either call close() on the listening socket, or exit the application.
After a TCP connection closes, it goes into the TIME_WAIT state for a fixed period. This is to ensure that any packets related to the connection that might still be queued up in the network won't interfere with new connections.
Since this has to happen even if the original process has exited, I'm guessing that Windows automatically transfers ownership to the system process.
So, I believe the answers to your last four questions are:
No, you probably don't need to worry about this.
Yes, the service program released the connection properly.
The TIME_WAIT connections will be closed early if the system runs out of TCBs. In the default configuration this will happen before you run out of ports, so in effect, yes, the connections will be reused if they are needed.
I'm not familiar with the service point manager, but there's no reason for it to track connections in the TIME_WAIT state, so probably no.
On Windows XP, the default value for the TIME_WAIT delay was two minutes. I can't find more recent information, but it seems likely that it hasn't been changed since then.
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