From 1899a7c3049f1a029c268a3a8152d145a0005e72 Mon Sep 17 00:00:00 2001 From: "sentry[bot]" <39604003+sentry[bot]@users.noreply.github.com> Date: Sat, 26 Sep 2026 13:30:05 +0000 Subject: [PATCH] fix: gracefully handle BrokenPipeError when writing to closed pipes --- codecov_cli/main.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/codecov_cli/main.py b/codecov_cli/main.py index 2568ef996..46f1db637 100644 --- a/codecov_cli/main.py +++ b/codecov_cli/main.py @@ -1,5 +1,7 @@ import logging import pathlib +import signal +import sys import typing import click @@ -93,7 +95,23 @@ def cli( def run(): - cli(obj={}) + # Restore default SIGPIPE handling on POSIX so the OS terminates the + # process cleanly when a pipe reader closes early, before Python raises + # BrokenPipeError inside Click's echo(). + if hasattr(signal, "SIGPIPE"): + signal.signal(signal.SIGPIPE, signal.SIG_DFL) + + try: + cli(obj={}) + except BrokenPipeError: + # Swallow broken-pipe errors that occur when stdout/stderr is closed + # by the consuming process (e.g. piped into `head`). Flush remaining + # output silently and exit with a non-zero code. + try: + sys.stderr.flush() + except BrokenPipeError: + pass + sys.exit(1) if __name__ == "__main__":