sarugaku / shellingham

Tool to Detect Surrounding Shell

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fails when run on macOS outside a tty/pty

ids1024 opened this issue · comments

I noticed this on Github Actions. I haven't reproduced it locally since I don't have a Mac, and it doesn't seem to occur on Linux.

ps normally lists processes on the same tty. It seems on Linux when run outside a tty, it just lists processes regardless of the connected terminal. But based on what I saw on Github actions, it seems this instead lists no processes.

Probably can be fixed with an appropriate argument to ps, though some care may be required to avoid breaking anything else and to be compatible with all ps implementations.

How can I set up a reproduction to investigate this?

I've opened a PR (#36) with a Github actions workflow testing this. Which is failing on macOS but passing on Linux.

Edit: https://github.com/ids1024/shellingham/actions/runs/280891996

Or, or I guess on Linux, ps not run in a terminal lists all processes that aren't in a terminal, so this is failing for me as well, since I don't have any shells that aren't connected to a tty:

import os
import fcntl
import termios
import subprocess
import shellingham

fd = os.open("/dev/tty", os.O_RDWR | os.O_NOCTTY)
fcntl.ioctl(fd, termios.TIOCNOTTY, '')
os.close(fd)

subprocess.call(["ps"])
print(shellingham.detect_shell())
Traceback (most recent call last):
  File "test.py", line 12, in <module>
    print(shellingham.detect_shell())
  File "/home/ian/src/shellingham/shellingham/__init__.py", line 25, in detect_shell
    raise ShellDetectionFailure()
shellingham._core.ShellDetectionFailure

on Linux, ps not run in a terminal lists all processes that aren't in a terminal

Hmm. I wonder why that’d work on GitHub Actions though? Shellingham tests the shell is in the current process’s parent chain, so maybe it’s detecting the login shell in this case? If so, this arguably be attributed to a Linux quirk and unfixable in shellingham.

Nope, there is legistimately a shell detected, persuambly the one used to run the commands you specify in run. But why does not the macOS worker have the same thing? I guess GitHub Actions is just implemented differently for different platforms, and there’s no real solution here.

It's a pain to debug with actually having a Mac to try things on locally, but it seemed when I just ran ps on a macOS GitHub action runner, it produced no output. So it seems the behavior of ps on macOS is different when there's not a connected tty.

Using something like ps -e to list all processes would probably fix it. If that's portable. Glancing at the ps(1) man page on Linux, it seems there's a fair bit of variation between different implementations and standards.

For whatever reason this is fixed by #49

Edit: I just realised this is because #49 changes the GitHub actions to instead run the tests using pytest.