From ca0797fc4eb1f143e25ec214aac4168017cac292 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Wed, 2 Mar 2016 11:20:28 -0500 Subject: [PATCH] avoid issues when stdin is a closed file this seems to happen when nohup is involved, so the check tty does not get a chance to fail, it just works with pipes fixes http://github.com/ansible/ansible-modules-core/issues/3166 --- lib/ansible/plugins/action/pause.py | 33 +++++++++++++++++------------ 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/lib/ansible/plugins/action/pause.py b/lib/ansible/plugins/action/pause.py index 97fa9ac3207..afe4f1d34bb 100644 --- a/lib/ansible/plugins/action/pause.py +++ b/lib/ansible/plugins/action/pause.py @@ -120,23 +120,29 @@ class ActionModule(ActionBase): # save the attributes on the existing (duped) stdin so # that we can restore them later after we set raw mode - fd = self._connection._new_stdin.fileno() - if isatty(fd): - old_settings = termios.tcgetattr(fd) - tty.setraw(fd) - - # flush the buffer to make sure no previous key presses - # are read in below - termios.tcflush(self._connection._new_stdin, termios.TCIFLUSH) - + fd = None + try: + fd = self._connection._new_stdin.fileno() + except ValueError: + # someone is using a closed file descriptor as stdin + pass + if fd is not None: + if isatty(fd): + old_settings = termios.tcgetattr(fd) + tty.setraw(fd) + + # flush the buffer to make sure no previous key presses + # are read in below + termios.tcflush(self._connection._new_stdin, termios.TCIFLUSH) while True: try: - key_pressed = self._connection._new_stdin.read(1) - if key_pressed == '\x03': - raise KeyboardInterrupt + if fd is not None: + key_pressed = self._connection._new_stdin.read(1) + if key_pressed == '\x03': + raise KeyboardInterrupt if not seconds: - if not isatty(fd): + if fd is None or not isatty(fd): display.warning("Not waiting from prompt as stdin is not interactive") break # read key presses and act accordingly @@ -154,6 +160,7 @@ class ActionModule(ActionBase): else: raise AnsibleError('user requested abort!') + except AnsibleTimeoutExceeded: # this is the exception we expect when the alarm signal # fires, so we simply ignore it to move into the cleanup