logging.StreamHandler produces BrokenPipeError when cut short by head utility
未关闭
还没有人认领这个 Issue。
pending
stdlib
type-bug
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- PR 合并指标
- PR 指标待抓取
描述
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
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
报告的路径是 Lib/logging/init.py,尤其是 StreamHandler.emit 和 flush;在 Linux 上将 test.py 管道传给 head -n1,以重现该问题。比较链接的 PR gh-134569 中提出的修复及其测试;完成的标准是短管道不再输出 logging traceback 或最后的 BrokenPipeError。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- tooling
- Issue 类型
- 缺陷
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 活跃度
- 停滞
- 描述清晰度
- 描述清楚
- 新手友好度
- 25/100