jhanna has asked for the wisdom of the Perl Monks concerning the following question:

I have a opensource smtp server project (see here) and I'm trying to figure how to help it die gracefully. After connects fork children to handle the connection. If I kill the perl server and then try to restart it right away I get: bind: Address already in use So I figure one of the child handlers must still be out there somewhere. After a few minutes you can restart the server and everything works normally. What can I do to make the server die more gracefully? I do have this:
sub CLOSE { ## called on INT to clean up a bit close(SERVER); close $client; die 'Exiting on INT'; } $SIG{INT} = \&CLOSE;
Should that be enough?

Replies are listed 'Best First'.
Re: forking server & bind: Address already in use
by Fastolfe (Vicar) on Jan 11, 2001 at 22:45 UTC
    You are probably not setting the 'reuse' flag with your listening server. Whenever a network socket is bound to a port, used, and then closed, that port is still "reserved" and cannot be used again for a small period of time. This is to keep any residual packets that might still arrive for that application to hit and be discarded, instead of making their way into the next application that happens to use that port erroneously.

    If you're using IO::Socket (which you really should be using), set the 'Reuse' argument to a true value, otherwise you'll have to call setsockopt directly:

    setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, pack("l", 1));
    See your system documentation for 'setsockopt' for more information about valid arguments. See perlipc for additional information about using network sockets in Perl.
      Hail the wisdom of the master!

      That's the tip I was hoping for. Everything I know about sockets comes from perlipc, but I don't remember reading about Reuse... There's always more to read.

      Thanks!
      john

        Actually, I just checked and I'm using REUSE_ADDR, but REUSE_PORT... maybe that will help.

        j

(jeffa) Re: forking server & bind: Address already in use
by jeffa (Bishop) on Jan 11, 2001 at 23:22 UTC
    Also, don't forget to write the process ID's of the parent and the children to a file(s). This makes it easy to track down your processes and kill them accordingly.

    Jeff

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    F--F--F--F--F--F--F--F--
    (the triplet paradiddle)