Skip to content

Commit

Permalink
fix: gracefully handle SIGPIPE (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
branchvincent committed Sep 2, 2022
1 parent aee0dae commit ac59662
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cleo/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions cleo/io/outputs/stream_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down

0 comments on commit ac59662

Please sign in to comment.