Fix BrokenPipeError traceback when piping CLI output - #7354
Open
mvanhorn wants to merge 1 commit into
Open
Conversation
Member
|
Thanks for you contribution. Unfortunately we've got quite the backlog so it may take us a while to get round to this |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a
BrokenPipeErrorhandler to the central CLI decorator (cli_functionincylc/flow/terminal.py) so that piping a Cylc command into a consumer that closes the pipe early exits quietly instead of dumping a Python traceback.Fixes #7247
Why this matters
As reported in #7247, running something like:
prints an ugly
BrokenPipeError: [Errno 32] Broken pipetraceback to stderr. It is harmless (the command still works) but looks broken, and it has existed since at least 8.3.5. The reporter confirmed it reproduces every time withgrep -q '\w'. The root cause is that the central CLI wrapper, which already converts "known" errors into clean exits, did not handleBrokenPipeErrorraised when writing to a closed stdout pipe.Putting the fix in
cli_functionmeans every Cylc subcommand benefits, not justcylc config.Changes
cylc/flow/terminal.py: addexcept BrokenPipeErrorto thecli_functionwrapper. It follows the standard-library-recommended pattern of redirecting the remaining stdout toos.devnull(to avoid a secondBrokenPipeErrorwhen Python flushes buffered stdout at interpreter shutdown) and then exits with status1. The redirect is guarded so it degrades gracefully if stdout is not backed by a real file descriptor. A broken pipe is never user-actionable, so the traceback is suppressed at all verbosity levels (distinct from the "unknown error" path, which intentionally shows a traceback).tests/unit/test_terminal.py: addtest_cli_broken_pipeverifying the wrapper convertsBrokenPipeErrorinto a cleanSystemExit(1)with no traceback on stderr, at both normal and debug verbosity.changes.d/7247.fix.md: towncrier changelog fragment.CONTRIBUTING.md: add contributor name (CLA).Testing
pytest tests/unit/test_terminal.py— all 16 cases pass (the 8 existingtest_clicases forCylcError/ParsecError/Exception/SystemExitare unaffected, plus the 2 new broken-pipe cases).flake8 cylc/flow/terminal.py tests/unit/test_terminal.py— clean.cli_functionpiped into a reader that closes early, the unpatched code prints a multi-lineTraceback ... BrokenPipeErrorto stderr; the patched code exits cleanly with no traceback.Check List
CONTRIBUTING.mdand added my name as a Code Contributor.setup.cfg(andconda-environment.ymlif present). — N/A, no dependency changes.changes.d/7247.fix.md).