From: Nathan Baum
Subject: Not releasing socket address
Date: 
Message-ID: <de8ria$sn7$1@newsg3.svr.pol.co.uk>
I have a program listening on a socket via SBCL's BSD socket inteface. 
No matter if the program completes successfully or via an error, Linux 
still thinks the address is in use for a couple of minutes after it exits.

(use-package 'sb-bsd-sockets)

(let ((server-socket (make-instance 'inet-socket :type :stream
                                                  :protocol :tcp)))
   (unwind-protect
        (progn
	 (socket-bind server-socket #(127 0 0 1) 4201)
	 (socket-listen server-socket 8)
	 (multiple-value-bind (sock peer) (socket-accept server-socket)
	   (unwind-protect
		(let ((stream (socket-make-stream sock :input t
                                                        :output t)))
		  (format stream "Hello ~A~%" peer))
	     (socket-close sock))))
	 (socket-close server-socket)))

(quit)

This works just fine, no errors from Lisp. But when I run it again, it 
can't use the address. Is it something I'm doing wrong? Or, is there a 
command I can use at the shell to tell Linux to release the address 
immediately?

From: Christophe Rhodes
Subject: Re: Not releasing socket address
Date: 
Message-ID: <sq8xyvhcps.fsf@cam.ac.uk>
Nathan Baum <···········@btinternet.com> writes:

> I have a program listening on a socket via SBCL's BSD socket inteface. 
> No matter if the program completes successfully or via an error, Linux 
> still thinks the address is in use for a couple of minutes after it exits.

You probably want the SO_REUSEADDR flag on your server socket.

  (setf (sb-bsd-sockets:sockopt-reuse-address socket) t)

probably works.

Christophe
From: Tim Bradshaw
Subject: Re: Not releasing socket address
Date: 
Message-ID: <1124717297.215172.142520@g49g2000cwa.googlegroups.com>
Nathan Baum wrote:
> I have a program listening on a socket via SBCL's BSD socket inteface.
> No matter if the program completes successfully or via an error, Linux
> still thinks the address is in use for a couple of minutes after it exits.

This is normal - there are places in the TCP protocol where you have to
wait for a fairly long time because the far end of the connection may
not have received your final ACK, and may therefore think that you
didn't get the final FIN, and therefore may resend it. You can't get
around this because there has to be some point during the close of a
connection at which you send (what you hope is) the last packet out
into the dark and there is no way of knowing whether it got there, so
you have to wait. Until that timeout's expired you can't reuse the
socket safely.  This state is called TIME_WAIT and netstat (or
equivalent) should show you things in this state with suitable options.

> Is it something I'm doing wrong? Or, is there a
> command I can use at the shell to tell Linux to release the address
> immediately?

You can use the SO_REUSEADDR option which says that it is OK to reuse a
socket even though there may be things in TIME_WAIT.  This is not
*completely* safe but it usually is.

--tim