4 ms·
The suppression of SIGPIPE was done in the Go code, not in the Python code. Does the POSIX standard mandate that programs receiving EPIPE/SIGPIPE die silently?
by nneonneo 7y ago
The suppression of SIGPIPE was done in the Go code, not in the Python code.
Does the POSIX standard mandate that programs receiving EPIPE/SIGPIPE die silently? I don’t know of such a rule, and there’s plenty of programs that violate this. Python is a bit too verbose with the errors (with a full trace back and two copies of the error) so suppressing those errors somehow seems like a good idea for a general-purpose command line tool.
- userbinator 7y agoDoes the POSIX standard mandate that programs receiving EPIPE/SIGPIPE die silently? https://pubs.opengroup.org/onlinepubs/7908799/xsh/signal.h.html https://pubs.opengroup.org/onlinepubs/7908799/xsh/signal.h.h... Yes. The default action of SIGPIPE is to terminate the process.
- deleted 7y ago[deleted]
- loeg 7y agoThe standard just specifies that (a) SIGPIPE exists, (b) when it should be raised by the OS, and (c) what the default action is. It does not specify that programs that mask or block or handle SIGPIPE/EPIPE must be silent when they do so.
- loeg 7y ago> The suppression of SIGPIPE was done in the Go code, not in the Python code. What? Your earlier comment I was responding to mentioned only Python, not Go. And Python suppresses SIGPIPE: https://github.com/python/cpython/blob/master/Python/pylifecycle.c#L2382-L2387 https://github.com/python/cpython/blob/master/Python/pylifec... , https://github.com/python/cpython/blob/master/Doc/library/signal.rst#note-on-sigpipe https://github.com/python/cpython/blob/master/Doc/library/si... > Does the POSIX standard mandate that programs receiving EPIPE/SIGPIPE die silently? Not to my knowledge.
- nneonneo 7y agoPython "knows what it's doing" in the sense that it guarantees that it checks return values from write() and has an exception mechanism to ensure that errors aren't accidentally ignored. Therefore, they can get away with ignoring SIGPIPE because they have the appropriate error checking in place. If you check the linked article, you'll see that it discusses both Python and Go, and mentions suppressing SIGPIPE in the Go code. My original comment did not mention Python for point #2; it was more of a general admonition. (And, like all Internet advice - there are always exceptions if you know what you're doing - it was more of a way to head off newbies who might see signal(SIGPIPE, SIG_IGN) and think it's a good idea!)
- Too 7y agoUseful to know is the exception is printed to stderr, not stdout. So if you continue the pipe after head you still get expected results. cat doesn't die 100% silently either, check $PIPESTATUS after piping cat into shorter head and you will see its exit code is non zero.