dispatch: handle IOError when writing to stderr
authorGregory Szorc <gregory.szorc@gmail.com>
Sun, 14 Jan 2018 20:06:56 -0800
changeset 35653 48fe4f56a3b4
parent 35652 40da2d7b4871
child 35654 59c842a3d1e1
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
mercurial/dispatch.py
tests/test-basic.t
--- a/mercurial/dispatch.py	Sun Jan 14 19:30:48 2018 -0800
+++ b/mercurial/dispatch.py	Sun Jan 14 20:06:56 2018 -0800
@@ -96,10 +96,16 @@
             err = e
             status = -1
     if util.safehasattr(req.ui, 'ferr'):
-        if err is not None and err.errno != errno.EPIPE:
-            req.ui.ferr.write('abort: %s\n' %
-                              encoding.strtolocal(err.strerror))
-        req.ui.ferr.flush()
+        try:
+            if err is not None and err.errno != errno.EPIPE:
+                req.ui.ferr.write('abort: %s\n' %
+                                  encoding.strtolocal(err.strerror))
+            req.ui.ferr.flush()
+        # There's not much we can do about an I/O error here. So (possibly)
+        # change the status code and move on.
+        except IOError:
+            status = -1
+
     sys.exit(status & 255)
 
 def _initstdio():
--- a/tests/test-basic.t	Sun Jan 14 19:30:48 2018 -0800
+++ b/tests/test-basic.t	Sun Jan 14 20:06:56 2018 -0800
@@ -34,15 +34,7 @@
   [255]
 #endif
 
-#if devfull no-chg
-  $ hg status >/dev/full 2>&1
-  [1]
-
-  $ hg status ENOENT 2>/dev/full
-  [1]
-#endif
-
-#if devfull chg
+#if devfull
   $ hg status >/dev/full 2>&1
   [255]