logging.StreamHandler produces BrokenPipeError when cut short by head utility
未關閉
還沒有人認領這個 Issue。
pending
stdlib
type-bug
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 36k
- 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