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
This commit is contained in:
Moritz Lell 2019-10-30 19:28:09 +01:00
commit 52a8ca25b2

View file

@ -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