pythonpython-2.xbroken-pipe

How to handle a broken pipe (SIGPIPE) in python?


I've written a simple multi-threaded game server in python that creates a new thread for each client connection. I'm finding that every now and then, the server will crash because of a broken-pipe/SIGPIPE error. I'm pretty sure it is happening when the program tries to send a response back to a client that is no longer present.

What is a good way to deal with this? My preferred resolution would simply close the server-side connection to the client and move on, rather than exit the entire program.

PS: This question/answer deals with the problem in a generic way; how specifically should I solve it?


Solution

  • Read up on the try: statement.

    try:
        # do something
    except socket.error, e:
        # A socket error
    except IOError, e:
        if e.errno == errno.EPIPE:
            # EPIPE error
        else:
            # Other error