8

I have a Windows service that - when it starts - opens some WCF services to listen on the 8000 port. It happens that this service crashes sometimes. When it does, the TCP connection is not released, thus causing my service to throw an exception if I try to start it again:

AddressAlreadyInUseException: There is already a listener on IP endpoint 0.0.0.0:8000

Some observations:

  • When running CurrPorts or netstat -ano, I can see that the 8000 port is still in use (in LISTENING state) and is owned by the Process ID XXX that corresponded to my service process ID. But my service has already crashed, and does not appear in the Task Manager anymore. Thus I can't kill the process to free the port! Of course, running taskkill /PID XXX returns:

    ERROR: The process "XXX" not found.

  • When running CurrPorts or netstat -b, I can see that the process name involved in creating the listening port is System, and not as MyService.exe (whereas it is MyService.exe when my service is running).

  • I tried to use CurrPorts to close the connection, but I always get the following error message:

    Failed to close one or more TCP connections. Be aware that you must run this tool as admin in order to close TCP connections.

    CurrPorts Screenshot

    (Useless to say, I do run CurrPorts as Administrator...)

  • TCPView is not much help either: the process name associated to the 8000 port is <non-existent>, and doing "End process" or "Close connection" has no effect.

  • I tried to see if there was not a child process associated with the PID XXX using Process Explorer, but no luck here.

  • If I close my service properly (before it crashes), the TCP connection is correctly released. This is normal, as I close the WCF service hosts in the OnStop() event of my service.


The only way I found to release the connection is to restart the server (which is not convenient in a production environment as you can guess). Waiting does not help, the TCP connection is never released.

How can I close the connection without restarting the Windows server?


PS: found some questions extremely similar to mine.

Community
  • 1
  • 1
Otiel
  • 18,404
  • 16
  • 78
  • 126
  • What says netstat -a -b -o? – user1764961 May 24 '13 at 10:28
  • you've said `this service crashes sometimes` and `the TCP connection is not released`. Firstly why is your service crashing? Secondly, is the tcp connection ever released (ie if you wait 1, 5 or longer minutes?) – wal May 24 '13 at 12:39
  • its not irrelevant or at least I dont think so. I have had wcf services that crash and they dont hold onto the tcp connections so it is worth investigating why your crashes are. if you gracefully shut down your service does it hold onto the tcp connection? why is your whole process crashing on a database exception (do you want it do that?) - i understand you probably dont want to go down this path of answering those questions but these exceptions/crashes are clearly causing some issues upstream – wal May 24 '13 at 13:08
  • how/why does that kind of Exception cause your entire process to crash/exit? (is that correct). you also didnt answer my q about shutting service down gracefully (what happens to your tcp conns then) – wal May 24 '13 at 13:29
  • It should not matter whether your service crashes or not as WCF/.NET is responsible of cleaning up the socket either way. Because WCF itself do not crash. It simply catches your exception, cleanup and then rethrow it. That's the reason to why the client always get a Fault error. – jgauffin May 24 '13 at 13:30
  • `isn't it normal for a process to crash when such exception occurs` <-- i wouldnt say its normal i would say its up to you to decide what happens. either handle the exception gracefully or if its unexpected and you do not have any graceful handler then yes it is 'normal' to tear down the process. – wal May 24 '13 at 13:36
  • call `OnStop()` in your in your `CurrentDomain.UnhandledException` (ie close your wcf service in the event of a crash) - sry this doesnt answer your question (hence the comment) but may work around your problem. i've seen others with this issue eg http://stackoverflow.com/questions/5106579/addressalreadyinuseexception-port-is-not-released and not much help I find it strange that @jgauffin says `it should not matter whether your service crashes or not` when it appears quite clear it does matter in this instance – wal May 24 '13 at 13:45
  • @wal: I will try that. I will delete my comments answering your questions also, as I have edited my question to insert those remarks. – Otiel May 24 '13 at 13:52
  • Actually WCF DOES crash from time to time. I had a similar program and problem (A Windows Service with self-hosted WCF). If it's possible for you, change your service to Web API (self-hosted). That solved my problem. – Kaveh Shahbazian Jun 10 '13 at 06:31

2 Answers2

2

I had the same problem and eventually figured out that the port was being held open by a child process that had been sublaunched by my process. Not sure why none of the system tools could tell me that. Ending the child process frees up the port.

0

I suggest you trying socket.ExclusiveAddressUse=false; Althought isn't usually intended to solve this kind of probelms.

Another work-around - locate the other service process and kill it manually in your very first code lines.