mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-08-03 20:54:24 -07:00
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
This commit is contained in:
parent
5aef65edcd
commit
ca0797fc4e
1 changed files with 19 additions and 12 deletions
|
@ -120,23 +120,29 @@ class ActionModule(ActionBase):
|
||||||
|
|
||||||
# save the attributes on the existing (duped) stdin so
|
# save the attributes on the existing (duped) stdin so
|
||||||
# that we can restore them later after we set raw mode
|
# that we can restore them later after we set raw mode
|
||||||
fd = self._connection._new_stdin.fileno()
|
fd = None
|
||||||
if isatty(fd):
|
try:
|
||||||
old_settings = termios.tcgetattr(fd)
|
fd = self._connection._new_stdin.fileno()
|
||||||
tty.setraw(fd)
|
except ValueError:
|
||||||
|
# someone is using a closed file descriptor as stdin
|
||||||
# flush the buffer to make sure no previous key presses
|
pass
|
||||||
# are read in below
|
if fd is not None:
|
||||||
termios.tcflush(self._connection._new_stdin, termios.TCIFLUSH)
|
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:
|
while True:
|
||||||
try:
|
try:
|
||||||
key_pressed = self._connection._new_stdin.read(1)
|
if fd is not None:
|
||||||
if key_pressed == '\x03':
|
key_pressed = self._connection._new_stdin.read(1)
|
||||||
raise KeyboardInterrupt
|
if key_pressed == '\x03':
|
||||||
|
raise KeyboardInterrupt
|
||||||
|
|
||||||
if not seconds:
|
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")
|
display.warning("Not waiting from prompt as stdin is not interactive")
|
||||||
break
|
break
|
||||||
# read key presses and act accordingly
|
# read key presses and act accordingly
|
||||||
|
@ -154,6 +160,7 @@ class ActionModule(ActionBase):
|
||||||
else:
|
else:
|
||||||
raise AnsibleError('user requested abort!')
|
raise AnsibleError('user requested abort!')
|
||||||
|
|
||||||
|
|
||||||
except AnsibleTimeoutExceeded:
|
except AnsibleTimeoutExceeded:
|
||||||
# this is the exception we expect when the alarm signal
|
# this is the exception we expect when the alarm signal
|
||||||
# fires, so we simply ignore it to move into the cleanup
|
# fires, so we simply ignore it to move into the cleanup
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue