I have a .net 4.0 WCF application that listens on net.tcp port 667. (Windows 7 machine)
At some point the application exits ungracefully (the user kills the process, for example).
Now a strange thing happens: The port remains open. When the user re-starts the application, it fails to listen on that port since it's already in use.
The strange thing is that even though the owning process was killed, the OS does not close the port, not even after several hours.
Here are some observations:
<non-existent>
, the PID belongs to the old (killed) process, and state is LISTENING
. Local address is my machine and there are both IPV4
and IPV6
listeners on that port. netstat -a -b -n -o
the executable involved is displayed as System
and local address is 0.0.0.0
. Other info is the same as TcpView.The only way I found to close that port was a system reboot...
Is there a way to configure the WCF net.tcp Service Host listener to avoid lingering after the process exists ungracefully?
No, at least not that you should use, but there is a way to tell it to reuse the socket address when it restarts, which makes that unnecessary.
Is there a way to programatically close that port? If there is, my application could first close that port before trying to listen to it.
No. Only the application that opened a port can close it.
Is there a utility that could close such "daemon" ports? (since TcpView cannot do that)
No, see above.
Is this an OS bug? Shouldn't the OS keep track of such "daemon" listeners and close them once the process exists?
All the process's resources including TCP ports should be relased when the process exits. There is an exception in the case of TCP 'ESTABLISHED' ports, which hang around for a couple of minutes for TCP security reasons.
It sounds like there is a sub-process that inherited the socket that is still active.
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