Skip to content
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

Exit gracefully when output pipe is broken #312

Open
akhayyat opened this issue May 19, 2015 · 2 comments · Fixed by borgbackup/borg#233
Open

Exit gracefully when output pipe is broken #312

akhayyat opened this issue May 19, 2015 · 2 comments · Fixed by borgbackup/borg#233

Comments

@akhayyat
Copy link

Consider an Attic command with long output, e.g. attic list <repo>::<archive> for an archive with many files.

When the output of such a command is piped to something like less, then you exit less without reaching the end of the output, you get the following backtrace:

$ attic list <repo>::<archive> | less  # and quit less immediately
Traceback (most recent call last):
  File ".../attic", line 3, in <module>
    main()
  File ".../attic/lib/python3.4/site-packages/attic/archiver.py", line 730, in main
    exit_code = archiver.run(sys.argv[1:])
  File ".../attic/lib/python3.4/site-packages/attic/archiver.py", line 720, in run
    return args.func(args)
  File ".../attic/lib/python3.4/site-packages/attic/archiver.py", line 298, in do_list
    remove_surrogates(item[b'path']), extra))
BrokenPipeError: [Errno 32] Broken pipe
Exception ignored in: <_io.TextIOWrapper name='<stdout>' encoding='UTF-8'>
BrokenPipeError: [Errno 32] Broken pipe

From the user's point-of-view, there should not be an error here. It'd be nice if this scenario is handled more gracefully.

@ThomasWaldmann
Copy link
Contributor

$ python3 -c 'for i in range(1000000): print(i)' | less
Traceback (most recent call last):
  File "<string>", line 1, in <module>
BrokenPipeError: [Errno 32] Broken pipe

I pressed "q" in less after paging down once, but likely before the loop ended.

Python 3.4.0

@akhayyat
Copy link
Author

# pipe.py
import errno

try:
    for i in range(1000000):
        print(i)
except IOError as e:
    if e.errno == errno.EPIPE:
        pass

Then, I can press q in less with no errors:

$ python3 pipe.py | less

anarcat added a commit to anarcat/borg that referenced this issue Oct 1, 2015
the logging level varies: most is logging.info(), in some place
logging.warning() or logging.error() are used when the condition is
clearly an error or warning. in other cases, we keep using print, but
force writing to sys.stderr, unless we interact with the user.

there were 77 calls to print before this commit, now there are 7, most
of which in the archiver module, which interacts directly with the
user. in one case there, we still use print() only because logging is
not setup properly yet during argument parsing.

it could be argued that commands like info or list should use print
directly, but we have converted them anyways, without ill effects on
the unit tests

unit tests still use print() in some places

this switches all informational output to stderr, which should help
with, if not fix jborg/attic#312 directly
anarcat added a commit to anarcat/borg that referenced this issue Oct 3, 2015
the logging level varies: most is logging.info(), in some place
logging.warning() or logging.error() are used when the condition is
clearly an error or warning. in other cases, we keep using print, but
force writing to sys.stderr, unless we interact with the user.

there were 77 calls to print before this commit, now there are 7, most
of which in the archiver module, which interacts directly with the
user. in one case there, we still use print() only because logging is
not setup properly yet during argument parsing.

it could be argued that commands like info or list should use print
directly, but we have converted them anyways, without ill effects on
the unit tests

unit tests still use print() in some places

this switches all informational output to stderr, which should help
with, if not fix jborg/attic#312 directly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants