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
bpo-42005: Fix CLI of cProfile and profile to catch BrokenPipeError #22643
bpo-42005: Fix CLI of cProfile and profile to catch BrokenPipeError #22643
Conversation
a12e713
to
ce36ff1
Compare
Please open a new issue: https://bugs.python.org/issue39828 is now closed. |
Ignore my comment. This PR was added to https://bugs.python.org/issue39828 because you used " bpo-39828" in the commit message. But you already used a new issue: https://bugs.python.org/issue42005 |
Actually it's not in the commit message, I only referenced bpo-39828 for context in the PR body. Sorry it accidentally confused the bot. |
Lib/cProfile.py
Outdated
runctx(code, globs, None, options.outfile, options.sort) | ||
except BrokenPipeError as exc: | ||
# Prevent "Exception ignored" during interpreter shutdown. | ||
sys.stdout = sys.stderr = io.StringIO() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Setting stderr to a new stringIO can hide other errors written into stderr at exit.
BrokenPipeError is more likely related to stdout than stderr. I understand that your intent is to ignore the warning displayed on stderr when sys.stdout file is closed.
There is now sys.unraisablehook which can be overidden temporarily to not log the exception on stderr when stdout is closed.
Since json.tool already has a similar code pattern, maybe it would be worth it to provide a helper function somewhere. I would prefer a private function.
By the way, it might be better to delete sys.stdout or set it to None, rather than creating a StringIO. So if the code after that tries to write tons of lines into sys.stdout, the writes are just ignored/lost, rather than growing memory (which will never be consumed anyway).
ce36ff1
to
03387c4
Compare
@vstinner Thanks for the review.
Absolutely.
In fact setting
Are you suggesting something like def run_with_broken_pipe_awareness(func, *args, **kwargs):
try:
return func(*args, **kwargs)
except BrokenPipeError as exc:
sys.stdout = None
sys.exit(exc.errno) ? If I were to add such a private helper, where should I place it? I can't seem to find a place in the code base where cross-module helpers live.
I erroneously thought it needs to be set to something that still supports |
This PR is stale because it has been open for 30 days with no activity. |
03387c4
to
fa36b03
Compare
I rebased on top of master. Other than that I think I'm just waiting for reviewer(s). |
(cherry picked from commit 3554fa4) Co-authored-by: Zhiming Wang <i@zhimingwang.org>
GH-24262 is a backport of this pull request to the 3.9 branch. |
(cherry picked from commit 3554fa4) Co-authored-by: Zhiming Wang <i@zhimingwang.org>
GH-24263 is a backport of this pull request to the 3.8 branch. |
Thanks @zmwangx: I tested your PR and it works as expected, I merged it and backported the fix to 3.8 and 3.9.
Previously, it wrote a long traceback:
|
Thanks! |
Catch
BrokenPipeError
in the CLI ofcProfile
andprofile
to reduce noise when piping throughhead
.Prior art of catching
BrokenPipeError
in a PSL CLI: bpo-39828 #18779 targetingjson.tool
.A test can be added but I've held off for now since it would be rather unwieldy and of pretty limited value.
https://bugs.python.org/issue42005