
Thank you for your response
Are you certain of this part? The usual problem with this kind of program is that the system holds the socket open for a minute or so in case there are any packets in flight for the connection (the lower level network protocols not being 100% reliable).
I'm not certain, but here's what I'm seeing. The process tries n times to acquire the socket, pausing for a second or so between attempts. While running a "child" process I will run a fresh process so that the two processes are competing for the socket, but neither of them are getting it. In half a dozen such test cases the fresh process grabs the socket on the very next attempt after the child process is interrupted. But if I interrupt the fresh process, the child process continues to fail to acquire the socket.
And the workaround is to set SO_REUSEADDR before binding the port; in Haskell,
setSocketOption socket ReuseAddr 1
I'm using the Network Module which sets ReuseAddr, according to its documentation. --Jonathan