Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion honcho/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@ def write(self, message):
prefix = '{time} {name}| '.format(time=time_formatted, name=name)
if self.colour and self._colours_supported and message.colour:
prefix = _colour_string(message.colour, prefix)
print(prefix + line, file=self.output, flush=True)
try:
print(prefix + line, file=self.output, flush=True)
except BrokenPipeError:
# Consumer of a pipe closed early (e.g. `honcho start | head`).
# Swallow so the manager can still terminate child processes
# instead of aborting mid-loop (issue #255).
return


def _ansi(code):
Expand Down
13 changes: 13 additions & 0 deletions tests/test_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,16 @@ def test_write_flushes_output(self):
p = Printer(output=out, prefix=False)
p.write(fake_message("paranoid android\n"))
assert out.flushcount == 1


def test_write_broken_pipe_is_swallowed(self):
class BrokenPipeOutput(FakeOutput):
def write(self, data):
raise BrokenPipeError()

out = BrokenPipeOutput()
p = Printer(output=out, prefix=False)
# Must not raise — manager loop relies on write being safe when the
# downstream pipe has gone away (#255).
p.write(fake_message("still here\n"))