[BUG] Environment could not be retrieved if there is an incorrect PYTHONHOME env
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 15.7k
- Forks
- 5.6k
- Avg merge
- 2d 44m
- Merged PRs (30d)
- 80
Description
Description
Environment could not be retrieved if there is an incorrect PYTHONHOME env
Setup
(Please provide relevant configs and/or SLS files (be sure to remove sensitive info. There is no general set-up of Salt.)
Please be as specific as possible and give set-up details.
- on-prem machine
- VM (Virtualbox, KVM, etc. please specify)
- VM running on a cloud service, please be explicit and add details
- container (Kubernetes, Docker, containerd, etc. please specify)
- or a combination, please be explicit
- jails if it is FreeBSD
- classic packaging
- onedir packaging
- used bootstrap to install
Steps to Reproduce the behavior
(Include debug logs if possible and relevant)
- Set incorrect PYTHONHOME env.
su testuser
cd ~
echo "export PYTHONHOME=/not/exist" > .profile
- Try cmd.run with runas. At this point, I confirmed that the results were different than expected.
salt 'mytest' cmd.run 'printenv' runas=testuser
- I could see the following error in the minion log.
2023-11-12 00:22:50,398 [salt.loaded.int.module.cmdmod:576 ][ERROR ][136097] Environment could not be retrieved for user 'testuser': stderr="Python path configuration:\n PYTHONHOME = '/not/exist'\n PYTHONPATH = (not set)\n program name = '/opt/saltstack/salt/bin/python3.10'\n isolated = 0\n environment = 1\n user site = 1\n import site = 1\n sys._base_executable = '/opt/saltstack/salt/bin/python3.10'\n sys.base_prefix = '/not/exist'\n sys.base_exec_prefix = '/not/exist'\n sys.platlibdir = 'lib'\n sys.executable = '/opt/saltstack/salt/bin/python3.10'\n sys.prefix = '/not/exist'\n sys.exec_prefix = '/not/exist'\n sys.path = [\n '/not/exist/lib/python310.zip',\n '/not/exist/lib/python3.10',\n '/not/exist/lib/python3.10/lib-dynload',\n ]\nFatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding\nPython runtime state: core initialized\nModuleNotFoundError: No module named 'encodings'\n\nCurrent thread 0x00007fd5d750e740 (most recent call first):\n <no Python frame>\n" stdout=''
Expected behavior
The error "Environment could not have been saved for user" should not occur and should operate normally.
Screenshots
If applicable, add screenshots to help explain your problem.
Versions Report
salt --versions-report
(Provided by running salt --versions-report. Please also mention any differences in master/minion versions.)Salt Version:
Salt: 3006.2
Python Version:
Python: 3.10.12 (main, Aug 3 2023, 21:47:10) [GCC 11.2.0]
Dependency Versions:
cffi: 1.14.6
cherrypy: unknown
dateutil: 2.8.1
docker-py: Not Installed
gitdb: Not Installed
gitpython: Not Installed
Jinja2: 3.1.2
libgit2: Not Installed
looseversion: 1.0.2
M2Crypto: Not Installed
Mako: Not Installed
msgpack: 1.0.2
msgpack-pure: Not Installed
mysql-python: Not Installed
packaging: 22.0
pycparser: 2.21
pycrypto: Not Installed
pycryptodome: 3.9.8
pygit2: Not Installed
python-gnupg: 0.4.8
PyYAML: 6.0.1
PyZMQ: 23.2.0
relenv: 0.13.3
smmap: Not Installed
timelib: 0.2.4
Tornado: 4.5.3
ZMQ: 4.3.4
System Versions:
dist: rhel 8.8 Ootpa
locale: utf-8
machine: x86_64
release: 4.18.0-477.15.1.el8_8.x86_64
system: Linux
version: Red Hat Enterprise Linux 8.8 Ootpa
Additional context
PYTHONHOME seems to need an unset when running env_cmd.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing the env_cmd path used by cmd.run with runas=testuser, focusing on how the user's environment is retrieved and how PYTHONHOME is passed. Reproduce the case with PYTHONHOME=/not/exist and verify that environment retrieval succeeds without the reported error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devops
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100