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/