Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ps command may return exit 1 when no processes are shown #15

Closed
sirn opened this issue Oct 14, 2018 · 2 comments
Closed

Ps command may return exit 1 when no processes are shown #15

sirn opened this issue Oct 14, 2018 · 2 comments

Comments

@sirn
Copy link

sirn commented Oct 14, 2018

On some systems such as FreeBSD, running ps without an assigned ttys, without login shell and without displaying header may cause ps command to have no output and exit 1 (e.g. via ssh user@host ps -o pid= or as part of Ansible script). When this happens, shellingham may fail with subprocess.CalledProcessError:

In particular, I got this error when I tried to run pipenv over Ansible on FreeBSD:

Traceback (most recent call last):
  File "/usr/local/bin/pipenv", line 7, in <module>
    from pipenv import cli
  File "/usr/local/lib/python3.6/site-packages/pipenv/__init__.py", line 23, in <module>
    from .cli import cli
  File "/usr/local/lib/python3.6/site-packages/pipenv/cli/__init__.py", line 3, in <module>
    from .command import cli
  File "/usr/local/lib/python3.6/site-packages/pipenv/cli/command.py", line 7, in <module>
    import crayons
  File "/usr/local/lib/python3.6/site-packages/pipenv/patched/crayons.py", line 49, in <module>
    is_powershell = "powershell" in shellingham.detect_shell()[0]
  File "/usr/local/lib/python3.6/site-packages/pipenv/vendor/shellingham/__init__.py", line 22, in detect_shell
    shell = get_shell(pid, max_depth=max_depth)
  File "/usr/local/lib/python3.6/site-packages/pipenv/vendor/shellingham/posix/__init__.py", line 54, in get_shell
    mapping = _get_process_mapping()
  File "/usr/local/lib/python3.6/site-packages/pipenv/vendor/shellingham/posix/__init__.py", line 15, in _get_process_mapping
    mapping = impl.get_process_mapping()
  File "/usr/local/lib/python3.6/site-packages/pipenv/vendor/shellingham/posix/ps.py", line 18, in get_process_mapping
    'ps', '-ww', '-o', 'pid=', '-o', 'ppid=', '-o', 'args=',
  File "/usr/local/lib/python3.6/subprocess.py", line 336, in check_output
    **kwargs).stdout
  File "/usr/local/lib/python3.6/subprocess.py", line 418, in run
    output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['ps', '-ww', '-o', 'pid=', '-o', 'ppid=', '-o', 'args=']' returned non-zero exit status 1.
*** Error code 1

Stop.

$ ssh user@freebsdhost "ps -o pid= || echo $?"
1

The above command is alternating 1 and 0 for some reason, I think because sometimes ps see a ps process, but I have been able to reproduce this quite reliably with Vagrant (run vagrant up then vagrant provision):

Vagrant.configure("2") do |config|
  config.vm.box = "generic/freebsd11"

  config.vm.provision :shell, privileged: false, inline: <<-EOF
    ps -ww -o pid=
    echo $?
  EOF
end

Thanks!

@uranusjr
Copy link
Member

I guess we don’t really need to fail with a CalledProcessError here. If ps fails, we can’t detect shell anyway, so it’s better to swallow the process return code and just raise ShellDetectionFailure.

@uranusjr
Copy link
Member

Workaround before Pipenv updates: Mount the procfs. Shellingham uses procfs to obtain process information if it is available, but on FreeBSD it is not enabled by default. How to do this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants