[lug] Restart failure binding to/listening on recently used port

Chris Riddoch riddochc at gmail.com
Sun Oct 11 12:57:33 MDT 2009


My google-fu is failing me, and I suspect I'm not the first to run
into this issue.  I've thrown together a little shell script to
demonstrate the problem:

socket at cinnamon:~/tmp/> cat listen-test.sh
#!/bin/sh
port=10000

echo "Starting at: " `date`
socat tcp-listen:$port gopen:read_data.txt &
sleep 1
socat ~/out.ps tcp:localhost:$port
echo "Finished runnng socat... killing anyway, just in case"
pkill socat
sleep 3
echo "Remaining socat processes:"
pgrep socat
echo "Waiting a while before starting again..."
sleep 2
echo "Trying to start listening on the same port at " `date`
socat tcp-listen:$port gopen:read_data_2.txt

socket at cinnamon:~/tmp/> ./listen-test.sh
Starting at:  Sun Oct 11 12:51:40 MDT 2009
Finished runnng socat... killing anyway, just in case
Remaining socat processes:
Waiting a while before starting again...
Trying to start listening on the same port at  Sun Oct 11 12:51:46 MDT 2009
2009/10/11 12:51:46 socat[6749] E bind(3, {AF=2 0.0.0.0:10000}, 16):
Address already in use
socket at cinnamon:~/tmp/>

The amount of time I have to wait before I can bind a server to the
same port can vary from a few seconds to more than a minute.
Strangely, the faster the system I'm using, the longer it seems to
take.

Is there a /proc or /sys switch I can fiddle with that affects this?
It *seems* that the kernel isn't cleaning up after listening processes
very quickly.  I've run into this frequently, when developing scripts
that listen on ports, and need to restart them for testing.  I've
experienced this in many languages, on several distros, with rather
high frequency.  What gives?

-- 
Chris Riddoch



More information about the LUG mailing list