python io_error ai_generated partial

BrokenPipeError: [Errno 32] Broken pipe

ID: python/brokenpipeerror

Also available as: JSON · Markdown
80%Fix Rate
85%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
311 active

Root Cause

Writing to a pipe/socket that was closed by the reader. Common: piping Python output to head/grep.

generic

Workarounds

  1. 90% success For CLI tools, handle SIGPIPE: signal.signal(signal.SIGPIPE, signal.SIG_DFL)
    import signal
    signal.signal(signal.SIGPIPE, signal.SIG_DFL)

    Sources: https://docs.python.org/3/library/signal.html#note-on-sigpipe

  2. 85% success For network code, handle the error gracefully — the client disconnected
    try:
        send_response(data)
    except BrokenPipeError:
        logging.info('Client disconnected before response completed')

    Sources: https://docs.python.org/3/library/exceptions.html#BrokenPipeError

Dead Ends

Common approaches that don't work:

  1. Increase buffer size 75% fail

    Buffer size doesn't matter — the reader has disconnected

  2. Wrap every print in try/except 60% fail

    Verbose and error-prone — use signal handling instead

Error Chain

Frequently confused with: