Ad Space — Top Banner

BrokenPipeError

Python Programming Language

Severity: Moderate

What Does This Error Mean?

BrokenPipeError means your Python script tried to write data to a pipe or socket, but the other end was already closed. Imagine sending a letter through a tube — if someone cuts the tube while you are still pushing letters in, the letters have nowhere to go. This commonly happens when piping Python output to another command like 'head' or 'less', and that command exits before your script finishes printing.

Affected Models

  • Python 3.x
  • Linux and macOS
  • Command-line scripts with piped output

Common Causes

  • Piping Python script output to a command like 'head -n 10' — head closes the pipe after 10 lines, but your script keeps trying to print
  • Writing to a network socket after the remote side has disconnected
  • A subprocess started by your script exited before it finished reading all the data you sent to its stdin
  • Writing to sys.stdout when the terminal or redirected output destination was closed
  • A client disconnected from a server in the middle of receiving a response

How to Fix It

  1. For command-line scripts piped to tools like 'head' or 'less', wrap your main output code in a try/except that catches BrokenPipeError and calls sys.exit(0) to exit cleanly.

    Also set sys.stdout = open(os.devnull, 'w') in the except block to suppress any further error messages from Python itself.

  2. Add signal handling for SIGPIPE on Unix systems: import signal, then signal.signal(signal.SIGPIPE, signal.SIG_DFL). This tells Python to handle broken pipes silently the same way C programs do.

    SIGPIPE is not available on Windows. Wrap this in a try/except ImportError block to make your code cross-platform.

  3. For network code, catch BrokenPipeError when writing to sockets. Log the disconnection and close the socket cleanly on your end.

    Always close sockets in a finally block or use a 'with' statement to ensure cleanup happens even if an error occurs.

  4. When writing to a subprocess's stdin, check that the subprocess is still running before writing. Use proc.poll() — if it returns a value, the process has exited.

    Also close proc.stdin after you finish writing. This signals the subprocess that input is complete, preventing hangs.

  5. Use a try/except/finally structure around all I/O operations that write to external destinations. Log the error, clean up resources, and exit gracefully rather than crashing with a traceback.

    For servers, catching BrokenPipeError per connection means one client disconnecting does not affect other clients.

When to Call a Professional

BrokenPipeError is always a code-level issue you can fix yourself. The most common fix for command-line scripts is to catch the error and exit cleanly. For network code, add proper disconnect handling.

Frequently Asked Questions

Why does Python print 'BrokenPipeError' even after the script finished successfully?

Python prints this when it tries to flush or close stdout on exit, but the pipe is already gone. This is a cosmetic issue — your script actually ran fine. The fix is to handle the signal at startup: signal.signal(signal.SIGPIPE, signal.SIG_DFL) on Linux/macOS, or wrap the output in try/except BrokenPipeError.

Does this error happen on Windows too?

Rarely, and in a different way. Windows does not use SIGPIPE signals. On Windows, a broken pipe when writing to a subprocess raises BrokenPipeError, but piping to head or less is less common on Windows. The network-related causes (writing to a closed socket) do happen on Windows too.

Is BrokenPipeError a sub-class of OSError?

Yes. BrokenPipeError is a sub-class of ConnectionError, which is a sub-class of OSError. You can catch it specifically with 'except BrokenPipeError' or more broadly with 'except OSError'. Catching the specific type gives you cleaner, more readable code.