Skip to content

Commit

Permalink
Add more logic in the shell detection code to handle a case where the…
Browse files Browse the repository at this point in the history
… parent PID is zero. This can happen when run from within a docker container with a non-interactive shel. (#1735)

Signed-off-by: Jean-Christophe Morin <jean_christophe_morin@hotmail.com>
  • Loading branch information
JeanChristopheMorinPerso committed May 12, 2024
1 parent f63031e commit 71d990b
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/rez/system.py
Expand Up @@ -79,22 +79,26 @@ def shell(self):
import subprocess as sp
shell = None

# check parent process via ps
try:
args = ['ps', '-o', 'args=', '-p', str(os.getppid())]
proc = sp.Popen(args, stdout=sp.PIPE)
output = proc.communicate()[0]
shell = os.path.basename(output.strip().split()[0]).replace('-', '')
except Exception:
pass
parent_pid = os.getppid()
if parent_pid != 0:
# When run from inside docker without an interactive shell,
# the parent pid will be 0, which will cause "ps" to
# print an error message: "process ID out of range".
try:
args = ['ps', '-o', 'args=', '-p', str(parent_pid)]
proc = sp.Popen(args, stdout=sp.PIPE)
output = proc.communicate()[0]
shell = os.path.basename(output.strip().split()[0]).replace('-', '')
except Exception:
pass

# check $SHELL
if shell not in shells:
shell = os.path.basename(os.getenv("SHELL", ''))

# traverse parent procs via /proc/(pid)/status
if shell not in shells:
pid = str(os.getppid())
pid = str(parent_pid)
found = False

while not found:
Expand Down

0 comments on commit 71d990b

Please sign in to comment.