Skip to content

fix: gracefully handle BrokenPipeError when writing to closed pipes - #797

Open
sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix/broken-pipe-error
Open

sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix/broken-pipe-error

Conversation

@sentry

@sentry sentry Bot commented Sep 26, 2026

Copy link
Copy Markdown

This PR addresses an issue where the CLI would crash with a BrokenPipeError when its output (specifically error messages from click.echo()) was piped to another command that closed its input pipe prematurely (e.g., codecov do-upload --report-type test_results 2>&1 | head -n 0).

The root cause was that the CLI did not explicitly handle BrokenPipeError when writing to stdout or stderr in such scenarios, leading to an unhandled exception.

The solution implements two complementary approaches:

  1. POSIX SIGPIPE handling: On POSIX systems, a SIGPIPE handler is installed (signal.signal(signal.SIGPIPE, signal.SIG_DFL)). This allows the operating system to terminate the process cleanly when a pipe's reading end is closed, preventing Python from raising a BrokenPipeError in many cases.
  2. BrokenPipeError exception handling: The main cli() call in codecov_cli/main.py is wrapped in a try/except BrokenPipeError block. This catches any BrokenPipeError that might still occur (e.g., on Windows or in specific edge cases), silently flushes any remaining output, and exits with a non-zero status code (1) to indicate an issue, without crashing the application.

This ensures a more robust and user-friendly experience when interacting with the CLI in piped environments.

Fixes CLI-HC

@sentry <feedback>: Autofix iterates on these changes
@sentry stop iterating: Autofix stops iterating on this run

This PR was automatically generated by Sentry. You can adjust this setting at any time.

@codecov

codecov Bot commented Sep 26, 2026 •

Copy link
Copy Markdown

❌ 1 Tests Failed:

Tests completed Failed Passed Skipped
723 1 722 1
View the full list of 1 ❄️ flaky test(s)
api.temp.calculator.test_calculator::test_divide

Flake rate in main: 100.00% (Passed 0 times, Failed 1726 times)

Stack Traces | 0.001s run time
def
                test_divide():
                > assert Calculator.divide(1, 2) == 0.5
                E assert 1.0 == 0.5
                E + where 1.0 = <function Calculator.divide at 0x104c9eb90>(1, 2)
                E + where <function Calculator.divide at 0x104c9eb90> = Calculator.divide
                .../temp/calculator/test_calculator.py:30: AssertionError

To view more test analytics, go to the Test Analytics Dashboard
📋 Got 3 mins? Take this short survey to help us improve Test Analytics.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants