How to prevent SIGPIPEs (or handle them properly)

前端 未结 10 1598
名媛妹妹
名媛妹妹 2020-11-22 07:27

I have a small server program that accepts connections on a TCP or local UNIX socket, reads a simple command and (depending on the command) sends a reply.

The problem

相关标签:
10条回答
  • 2020-11-22 08:10

    You cannot prevent the process on the far end of a pipe from exiting, and if it exits before you've finished writing, you will get a SIGPIPE signal. If you SIG_IGN the signal, then your write will return with an error - and you need to note and react to that error. Just catching and ignoring the signal in a handler is not a good idea -- you must note that the pipe is now defunct and modify the program's behaviour so it does not write to the pipe again (because the signal will be generated again, and ignored again, and you'll try again, and the whole process could go on for a long time and waste a lot of CPU power).

    0 讨论(0)
  • 2020-11-22 08:10

    What's the best practice to prevent the crash here?

    Either disable sigpipes as per everybody, or catch and ignore the error.

    Is there a way to check if the other side of the line is still reading?

    Yes, use select().

    select() doesn't seem to work here as it always says the socket is writable.

    You need to select on the read bits. You can probably ignore the write bits.

    When the far end closes its file handle, select will tell you that there is data ready to read. When you go and read that, you will get back 0 bytes, which is how the OS tells you that the file handle has been closed.

    The only time you can't ignore the write bits is if you are sending large volumes, and there is a risk of the other end getting backlogged, which can cause your buffers to fill. If that happens, then trying to write to the file handle can cause your program/thread to block or fail. Testing select before writing will protect you from that, but it doesn't guarantee that the other end is healthy or that your data is going to arrive.

    Note that you can get a sigpipe from close(), as well as when you write.

    Close flushes any buffered data. If the other end has already been closed, then close will fail, and you will receive a sigpipe.

    If you are using buffered TCPIP, then a successful write just means your data has been queued to send, it doesn't mean it has been sent. Until you successfully call close, you don't know that your data has been sent.

    Sigpipe tells you something has gone wrong, it doesn't tell you what, or what you should do about it.

    0 讨论(0)
  • 2020-11-22 08:11

    Another method is to change the socket so it never generates SIGPIPE on write(). This is more convenient in libraries, where you might not want a global signal handler for SIGPIPE.

    On most BSD-based (MacOS, FreeBSD...) systems, (assuming you are using C/C++), you can do this with:

    int set = 1;
    setsockopt(sd, SOL_SOCKET, SO_NOSIGPIPE, (void *)&set, sizeof(int));
    

    With this in effect, instead of the SIGPIPE signal being generated, EPIPE will be returned.

    0 讨论(0)
  • 2020-11-22 08:11

    Linux manual said:

    EPIPE The local end has been shut down on a connection oriented socket. In this case the process will also receive a SIGPIPE unless MSG_NOSIGNAL is set.

    But for Ubuntu 12.04 it isn't right. I wrote a test for that case and I always receive EPIPE withot SIGPIPE. SIGPIPE is genereated if I try to write to the same broken socket second time. So you don't need to ignore SIGPIPE if this signal happens it means logic error in your program.

    0 讨论(0)
提交回复
热议问题