From 52a8ca25b2c9bb779382771bb32621a2dce7ded1 Mon Sep 17 00:00:00 2001 From: Moritz Lell Date: Wed, 30 Oct 2019 19:28:09 +0100 Subject: [PATCH] Prevent "Exception ... ignored" in `redo-log ... | head` When STDOUT is piped to another program and that program closes the pipe, this program is supposed to terminate. However, Python 3 prints an error message because prior to exiting, STDOUT is automatically flushed which does not work due to it being closed by the other side. This commit includes code taken from the Python documentation for this case. The output stream is redirected to /dev/null --- redo/cmd_log.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/redo/cmd_log.py b/redo/cmd_log.py index fdcc7b8..3f86511 100644 --- a/redo/cmd_log.py +++ b/redo/cmd_log.py @@ -277,7 +277,15 @@ def main(): sys.exit(200) except IOError as e: if e.errno == errno.EPIPE: - pass + # this happens for example if calling `redo-log | head`, so stdout + # is piped into another program that closes the pipe before reading + # all our output. + # from https://docs.python.org/3/library/signal.html#note-on-sigpipe: + # Python flushes standard streams on exit; redirect remaining output + # to devnull to avoid another BrokenPipeError at shutdown + devnull = os.open(os.devnull, os.O_WRONLY) + os.dup2(devnull, sys.stdout.fileno()) + sys.exit(141) # =128+13: "Terminated by SIGPIPE (signal 13)" else: raise