log-search: Prevent BrokenPipeError exceptions when run with `head`.

See https://docs.python.org/3/library/signal.html#note-on-sigpipe
This commit is contained in:
Alex Vandiver 2023-07-11 14:20:09 +00:00 committed by Tim Abbott
parent be960f4142
commit 3bf83e8830
1 changed files with 6 additions and 0 deletions

View File

@ -226,6 +226,12 @@ def main() -> None:
filter_types=filter_types, filter_types=filter_types,
use_color=use_color, 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: except KeyboardInterrupt:
sys.exit(signal.SIGINT + 128) sys.exit(signal.SIGINT + 128)