pyinvoke / invoke

Pythonic task management & command execution.
http://pyinvoke.org
BSD 2-Clause "Simplified" License
4.36k stars 367 forks source link

Problem with python3 invoke library using runuser #705

Open dcalap opened 4 years ago

dcalap commented 4 years ago

I have this basic python script test.py using invoke library:

#!/usr/bin/python3

from invoke import run

run('whoami')

And when I try to run it using runuser as root it fails.

Command executed:

runuser -l david -c '/home/david/test.py'

Stacktrace:

Traceback (most recent call last):
  File "/home/david/test.py", line 5, in <module>
    run('whoami')
  File "/home/david/.local/lib/python3.6/site-packages/invoke/__init__.py", line 48, in run
    return Context().run(command, **kwargs)
  File "/home/david/.local/lib/python3.6/site-packages/invoke/context.py", line 94, in run
    return self._run(runner, command, **kwargs)
  File "/home/david/.local/lib/python3.6/site-packages/invoke/context.py", line 101, in _run
    return runner.run(command, **kwargs)
  File "/home/david/.local/lib/python3.6/site-packages/invoke/runners.py", line 363, in run
    return self._run_body(command, **kwargs)
  File "/home/david/.local/lib/python3.6/site-packages/invoke/runners.py", line 422, in _run_body
    return self.make_promise() if self._asynchronous else self._finish()
  File "/home/david/.local/lib/python3.6/site-packages/invoke/runners.py", line 474, in _finish
    raise ThreadException(thread_exceptions)
invoke.exceptions.ThreadException: 
Saw 1 exceptions within threads (OSError):

Thread args: {'kwargs': {'echo': None,
            'input_': <_io.TextIOWrapper name='<stdin>' mode='r' encoding='ANSI_X3.4-1968'>,
            'output': <_io.TextIOWrapper name='<stdout>' mode='w' encoding='ANSI_X3.4-1968'>},
 'target': <bound method Runner.handle_stdin of <invoke.runners.Local object at 0x7f7a767b5e10>>}

Traceback (most recent call last):

  File "/home/david/.local/lib/python3.6/site-packages/invoke/util.py", line 233, in run
    super(ExceptionHandlingThread, self).run()

  File "/usr/lib/python3.6/threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)

  File "/home/david/.local/lib/python3.6/site-packages/invoke/runners.py", line 813, in handle_stdin
    with character_buffered(input_):

  File "/usr/lib/python3.6/contextlib.py", line 81, in __enter__
    return next(self.gen)

  File "/home/david/.local/lib/python3.6/site-packages/invoke/terminals.py", line 176, in character_buffered
    or not stdin_is_foregrounded_tty(stream)

  File "/home/david/.local/lib/python3.6/site-packages/invoke/terminals.py", line 139, in stdin_is_foregrounded_tty
    return os.getpgrp() == os.tcgetpgrp(stream.fileno())

OSError: [Errno 25] Inappropriate ioctl for device

Any idea?

mcepl commented 1 year ago

Me too. I have 19 failed tests with this error when packaging invoke 2.1.0 for openSUSE/Factory.

[   18s] FAILED tests/runners.py::Runner_::character_buffered_stdin::setcbreak_called_on_tty_stdins
[   18s] FAILED tests/runners.py::Runner_::character_buffered_stdin::setcbreak_not_called_if_process_not_foregrounded
[   18s] FAILED tests/runners.py::Runner_::character_buffered_stdin::tty_stdins_have_settings_restored_by_default
[   18s] FAILED tests/runners.py::Runner_::character_buffered_stdin::tty_stdins_have_settings_restored_on_KeyboardInterrupt
[   18s] FAILED tests/runners.py::Local_::pty::when_pty_True_we_use_pty_fork_and_os_exec
[   18s] FAILED tests/runners.py::Local_::pty::pty_uses_WEXITSTATUS_if_WIFEXITED - OSE...
[   18s] FAILED tests/runners.py::Local_::pty::pty_uses_WTERMSIG_if_WIFSIGNALED - OSEr...
[   18s] FAILED tests/runners.py::Local_::pty::WTERMSIG_result_turned_negative_to_match_subprocess
[   18s] FAILED tests/runners.py::Local_::pty::pty_is_set_to_controlling_terminal_size
[   18s] FAILED tests/runners.py::Local_::pty::spurious_OSErrors_handled_gracefully - ...
[   18s] FAILED tests/runners.py::Local_::pty::other_spurious_OSErrors_handled_gracefully
[   18s] FAILED tests/runners.py::Local_::pty::non_spurious_OSErrors_bubble_up - OSErr...
[   18s] FAILED tests/runners.py::Local_::pty::stop_mutes_errors_on_pty_close - OSErro...
[   18s] FAILED tests/runners.py::Local_::pty::fallback::can_be_overridden_by_kwarg - ...
[   18s] FAILED tests/runners.py::Local_::pty::fallback::can_be_overridden_by_config
[   18s] FAILED tests/runners.py::Local_::pty::fallback::overridden_fallback_affects_result_pty_value
[   18s] FAILED tests/runners.py::Local_::shell::defaults_to_bash_or_cmdexe_when_pty_True
[   18s] FAILED tests/runners.py::Local_::shell::may_be_overridden_when_pty_True - OSE...
[   18s] FAILED tests/runners.py::Local_::env::uses_execve_for_pty_True - OSError: [Er...

Complete logs

a-detiste commented 4 months ago

I got somewhat the same failures on Debian

/usr/lib/python3.11/unittest/mock.py:1201: OSError
=========================== short test summary info ============================
FAILED tests/runners.py::Local_::pty::when_pty_True_we_use_pty_fork_and_os_exec
FAILED tests/runners.py::Local_::pty::pty_uses_WEXITSTATUS_if_WIFEXITED - OSE...
FAILED tests/runners.py::Local_::pty::pty_uses_WTERMSIG_if_WIFSIGNALED - OSEr...
FAILED tests/runners.py::Local_::pty::WTERMSIG_result_turned_negative_to_match_subprocess
FAILED tests/runners.py::Local_::pty::pty_is_set_to_controlling_terminal_size
FAILED tests/runners.py::Local_::pty::spurious_OSErrors_handled_gracefully - ...
FAILED tests/runners.py::Local_::pty::other_spurious_OSErrors_handled_gracefully
FAILED tests/runners.py::Local_::pty::non_spurious_OSErrors_bubble_up - OSErr...
FAILED tests/runners.py::Local_::pty::stop_mutes_errors_on_pty_close - OSErro...
FAILED tests/runners.py::Local_::pty::fallback::can_be_overridden_by_kwarg - ...
FAILED tests/runners.py::Local_::pty::fallback::can_be_overridden_by_config
FAILED tests/runners.py::Local_::pty::fallback::overridden_fallback_affects_result_pty_value
FAILED tests/runners.py::Local_::shell::defaults_to_bash_or_cmdexe_when_pty_True
FAILED tests/runners.py::Local_::shell::may_be_overridden_when_pty_True - OSE...
FAILED tests/runners.py::Local_::env::uses_execve_for_pty_True - OSError: [Er...