TCP port reserved by the child process after the death of the parent process (.net 3.5)

I implemented a small WCF service in .net 3.5, on which clients connect over TCP to port 4321. This service can spawn other processes (via System.Diagnostics.Process). Of course, when the service is killed or out of order or something else, unresolved processes remain. The problem is that if I try to restart the service while these processes are still running, I get the following exception:

CommunicationException: A listener already exists on the IP endpoint 0.0.0.0-00-00321. Make sure that you are not trying to use this endpoint several times in your application and that there are no other applications listening to this endpoint.

In fact, when I run netstat, I see that there is a process that listens on TCP port 4321:

TCP 0.0.0.0-00-00321 MTL-WKS-AG196: 0 LISTENING 97308

The process identifier that you see here (97308) is the service that I first started (which should no longer exist since it was killed). The only way to free a port is to kill all processes that were created during the life of the service.

I know little about ports and processes, but I understand that a child process "inherits" the ports that the parent process listens to. Is this more or less what is happening?

Is there any way to undo this behavior? Without access to the code of the generated process?

Also, I really don't think this is possible, but is there a way to tell Windows to kill child processes when their parent process is killed?

Thanks!

+6
source share
1 answer

Obviously, BCL allows all handles to be inherited by the child process (port in this case). See Stephen Cleary's answer to the MSDN forum

I am currently having the same issue and using a temporary workaround. I turned on the Net.Tcp network sharing service and included it in your wcf service app.config. (More info here )

Will try to implement Stephen’s suggestions as a more permanent solution when I get time :)

+1
source

Source: https://habr.com/ru/post/886874/


All Articles