python / python/cpython

logging.StreamHandler produces BrokenPipeError when cut short by head utility

Open
#134,568 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

pending stdlib type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

The following code:

# test.py
import sys
import logging

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler(sys.stdout))
logger.debug('foo')
logger.debug('bar')

when piped to head -n1, produces:

# python test.py | head -n1
foo
--- Logging error ---
Traceback (most recent call last):
  File "/src/cpython/Lib/logging/__init__.py", line 1155, in emit
    self.flush()
    ~~~~~~~~~~^^
  File "/src/cpython/Lib/logging/__init__.py", line 1137, in flush
    self.stream.flush()
    ~~~~~~~~~~~~~~~~~^^
BrokenPipeError: [Errno 32] Broken pipe
Call stack:
  File "test.py", line 8, in <module>
    logger.debug('bar')
Message: 'bar'
Arguments: ()
Exception ignored while flushing sys.stdout:
BrokenPipeError: [Errno 32] Broken pipe

This is because logging.StreamHandler.emit does not handle BrokenPipeError when flushing the stream.
I will fix this with the approach recommended by Note on SIGPIPE.

CPython versions tested on:

CPython main branch, 3.13

Operating systems tested on:

Linux

Linked PRs
  • gh-134569

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The reported path is Lib/logging/init.py, especially StreamHandler.emit and flush; reproduce the issue with test.py piped to head -n1 on Linux. Compare the proposed fix in linked PR gh-134569 and its tests; done means the short pipeline no longer emits the logging traceback or final BrokenPipeError.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.