Skip to content

gh-134568: added handler for BrokenPipeError to logging.StreamHandler.emit #134569

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Lib/logging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1155,6 +1155,10 @@ def emit(self, record):
self.flush()
except RecursionError: # See issue 36272
raise
except BrokenPipeError:
Copy link
Member

@picnixz picnixz May 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can add a "See issue XXXX" as above for the RecursionError maybe. However, shouldn't we give the possibility to handle the error? maybe BrokenPipeError is coming from somewhere else? Here, you're redirecting sys.stdout but what if it's because of self.stream? (I don't know which is why I ask)

devnull = os.open(os.devnull, os.O_WRONLY)
os.dup2(devnull, sys.stdout.fileno())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this have weird side-effects? Any future stdout writes won't show up.

sys.exit(1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, why are we exiting the process?

except Exception:
self.handleError(record)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:class:`logging.StreamHandler` now handles :exc:`BrokenPipeError` when the output is cut short by piping to the ``head`` utility.
Loading