Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Not releasing socket address

1 view
Skip to first unread message

Nathan Baum

unread,
Aug 20, 2005, 11:17:48 PM8/20/05
to
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?

Christophe Rhodes

unread,
Aug 21, 2005, 5:16:15 AM8/21/05
to
Nathan Baum <natha...@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

Tim Bradshaw

unread,
Aug 22, 2005, 9:28:17 AM8/22/05
to
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

0 new messages