From cbdeb2db36bd3649a500c610c4f1f9f3264778b0 Mon Sep 17 00:00:00 2001 From: Branch Vincent Date: Wed, 24 Aug 2022 23:06:27 -0400 Subject: [PATCH] fix: gracefully handle SIGPIPE --- cleo/application.py | 6 ++++++ cleo/io/outputs/stream_output.py | 1 + 2 files changed, 7 insertions(+) diff --git a/cleo/application.py b/cleo/application.py index 44a79709..9b17d1a1 100644 --- a/cleo/application.py +++ b/cleo/application.py @@ -325,6 +325,12 @@ def run( try: exit_code = self._run(io) + except BrokenPipeError: + # If we are piped to another process, it may close early and send a + # SIGPIPE: https://docs.python.org/3/library/signal.html#note-on-sigpipe + devnull = os.open(os.devnull, os.O_WRONLY) + os.dup2(devnull, sys.stdout.fileno()) + exit_code = 0 except Exception as e: if not self._catch_exceptions: raise diff --git a/cleo/io/outputs/stream_output.py b/cleo/io/outputs/stream_output.py index 71d16aa6..a5140028 100644 --- a/cleo/io/outputs/stream_output.py +++ b/cleo/io/outputs/stream_output.py @@ -77,6 +77,7 @@ def _write(self, message: str, new_line: bool = False) -> None: message += "\n" self._stream.write(message) + self._stream.flush() def _has_color_support(self) -> bool: # Follow https://no-color.org/