Mercurial > hg
comparison mercurial/dispatch.py @ 35653:48fe4f56a3b4
dispatch: handle IOError when writing to stderr
Previously, attempts to write to stderr in dispatch.run() may lead to
an exception being thrown. This would likely be handled by Python's
default exception handler, which would print the exception and exit
1.
Code in this function is already catching IOError for stdout failures
and converting to exit code 255 (-1 & 255 == 255). Why we weren't
doing the same for stderr for the sake of consistency, I don't know.
I do know that chg and hg diverged in behavior here (as the changed
test-basic.t shows).
After this commit, we catch I/O failure on stderr and change the
exit code to 255. chg and hg now behave consistently. As a bonus,
Rust hg also now passes this test.
I'm skeptical at changing the exit code due to failures this late
in the process. I think we should consider preserving the current
exit code - assuming it is non-0. And, we may want to preserve the
exit code completely if the I/O error is EPIPE (and potentially
other special error classes). There's definitely room to tweak
behavior. But for now, let's at least prevent the uncaught exception.
Differential Revision: https://phab.mercurial-scm.org/D1860
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Sun, 14 Jan 2018 20:06:56 -0800 |
parents | 7906354cbc68 |
children | a2b3b5c5a25a |
comparison
equal
deleted
inserted
replaced
35652:40da2d7b4871 | 35653:48fe4f56a3b4 |
---|---|
94 req.ui.fout.flush() | 94 req.ui.fout.flush() |
95 except IOError as e: | 95 except IOError as e: |
96 err = e | 96 err = e |
97 status = -1 | 97 status = -1 |
98 if util.safehasattr(req.ui, 'ferr'): | 98 if util.safehasattr(req.ui, 'ferr'): |
99 if err is not None and err.errno != errno.EPIPE: | 99 try: |
100 req.ui.ferr.write('abort: %s\n' % | 100 if err is not None and err.errno != errno.EPIPE: |
101 encoding.strtolocal(err.strerror)) | 101 req.ui.ferr.write('abort: %s\n' % |
102 req.ui.ferr.flush() | 102 encoding.strtolocal(err.strerror)) |
103 req.ui.ferr.flush() | |
104 # There's not much we can do about an I/O error here. So (possibly) | |
105 # change the status code and move on. | |
106 except IOError: | |
107 status = -1 | |
108 | |
103 sys.exit(status & 255) | 109 sys.exit(status & 255) |
104 | 110 |
105 def _initstdio(): | 111 def _initstdio(): |
106 for fp in (sys.stdin, sys.stdout, sys.stderr): | 112 for fp in (sys.stdin, sys.stdout, sys.stderr): |
107 util.setbinary(fp) | 113 util.setbinary(fp) |