From 3bf83e8830252297e6590d2fc48360d7c37b419c Mon Sep 17 00:00:00 2001 From: Alex Vandiver Date: Tue, 11 Jul 2023 14:20:09 +0000 Subject: [PATCH] log-search: Prevent BrokenPipeError exceptions when run with `head`. See https://docs.python.org/3/library/signal.html#note-on-sigpipe --- scripts/log-search | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/scripts/log-search b/scripts/log-search index 75bf63a8ae..26026bb46d 100755 --- a/scripts/log-search +++ b/scripts/log-search @@ -226,6 +226,12 @@ def main() -> None: filter_types=filter_types, use_color=use_color, ) + except BrokenPipeError: + # 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(1) except KeyboardInterrupt: sys.exit(signal.SIGINT + 128)