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

I have a script on a server, and then more services on various clients. Everything works great in the process of what I am trying to accomplish, but when the socket is closed on one of the remote machines, The server side script exits with a "Broke n Pipe" error. For now I just have a cron job to make sure the process is running every 2 minutes and starting it back up if it isn't, but I would like the process to always be running listening for connections on it's port. While I was testing this, I did a clean shutdown of where the socket was closed on the client side and turned off; broken pipe. Just killing the service on the client side resulted in a broken pipe error as well. Can anyone give any insight on how to keep the process up and running, or more details on the broken pipe error? Been googling but not coming up with anything good. <edit> I am seeing different messages saying the socket was not closed properly, but The client side (c# code) I have watched it go thru debug mode and close the socket. I will see if I can find anything about ignoring a broken pipe error. Like I saw $SIG{PIPE}= 'IGNORE'; if I want to ignore the error. I will look into just logging it, but keeping the process alive?

Replies are listed 'Best First'.
Re: IO::Socket "broken pipe"
by pc88mxer (Vicar) on Feb 14, 2008 at 16:27 UTC
    You'll get a broken pipe message if one process shuts down their side of the connection and then the other process tries a write operation on the connection. The writer will get the broken pipe exception.

    If you are doing "clean" shutdowns there must be something amiss with your protocol - your client and server are not "on the same page" so to speak. A work-around is to catch/ignore the SIGPIPE exception in your server with:

    $SIG{PIPE} = 'IGNORE';

    If your server is only serving one client at a time, you can assume that the client caused the exception and you can have the SIGPIPE handler take the appropriate action. If your server can server multiple clients at a time, you won't be able to tell which client caused the exception.

Re: IO::Socket "broken pipe"
by rgiskard (Hermit) on Feb 14, 2008 at 16:31 UTC
    Check out thread 393313 (since it's similar to your question). One suggestion is to add a subroutine to $SIG{PIPE} to reset your connection, but without seeing any code, it's somewhat difficult to say what you should/shouldn't do.
    Update a little code goes a long way
      The $SIG{PIPE} suggestion worked guys. Thank you for your help. I am now catching the broken pipe error gracefully and continuing to listen for connections on the server side.
Re: IO::Socket "broken pipe"
by zentara (Cardinal) on Feb 14, 2008 at 18:51 UTC
    You havn't shown your code, but read "perldoc IO::Socket" and look for the "connected" method. Usually you can write your socket code to test for connection, like:
    if( $sock->connected){ ....... }
    That way you can avoid the error, by closing and cleaning up the $sock if it is gone.

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum