Commit 225e2544 authored by Iustin Pop's avatar Iustin Pop
Browse files

Handle EPIPE errors while writing to the terminal



This handles EPIPE errors in two places: ToStream (to catch logging
done in GenericMain itself) and in GenericMain (to cover also plain
print statements).
Signed-off-by: default avatarIustin Pop <iustin@google.com>
Reviewed-by: default avatarMichael Hanselmann <hansmi@google.com>
parent 20d317d4
...@@ -27,6 +27,7 @@ import textwrap ...@@ -27,6 +27,7 @@ import textwrap
import os.path import os.path
import time import time
import logging import logging
import errno
from cStringIO import StringIO from cStringIO import StringIO
from ganeti import utils from ganeti import utils
...@@ -1939,6 +1940,12 @@ def GenericMain(commands, override=None, aliases=None): ...@@ -1939,6 +1940,12 @@ def GenericMain(commands, override=None, aliases=None):
ToStderr("Aborted. Note that if the operation created any jobs, they" ToStderr("Aborted. Note that if the operation created any jobs, they"
" might have been submitted and" " might have been submitted and"
" will continue to run in the background.") " will continue to run in the background.")
except IOError, err:
if err.errno == errno.EPIPE:
# our terminal went away, we'll exit
sys.exit(constants.EXIT_FAILURE)
else:
raise
return result return result
...@@ -2795,13 +2802,20 @@ def _ToStream(stream, txt, *args): ...@@ -2795,13 +2802,20 @@ def _ToStream(stream, txt, *args):
@param txt: the message @param txt: the message
""" """
if args: try:
args = tuple(args) if args:
stream.write(txt % args) args = tuple(args)
else: stream.write(txt % args)
stream.write(txt) else:
stream.write('\n') stream.write(txt)
stream.flush() stream.write('\n')
stream.flush()
except IOError, err:
if err.errno == errno.EPIPE:
# our terminal went away, we'll exit
sys.exit(constants.EXIT_FAILURE)
else:
raise
def ToStdout(txt, *args): def ToStdout(txt, *args):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment