subprocess.Popen can pass incomplete environment when posix_spawn is used · Issue #113119 · python/cpython (original) (raw)

Bug report

Bug description:

Subprocess created with posix_spawn can have different environment when compared to a process created with stardard fork/exec.

The reason is that posix_spawn uses os.environ (when no env was given), which is not affected by os.putenv and os.unsetenv. This is different from execv, which uses process environment (available through environ).

This can be easily reproduced with test_putenv_unsetenv modified to call subprocess.run with close_fds=False (which makes it use the posix_spawn call):

import subprocess import sys import os

name = "PYTHONTESTVAR" value = "testvalue" code = f'import os; print(repr(os.environ.get({name!r})))'

os.putenv(name, value) proc = subprocess.run([sys.executable, '-c', code], check=True, stdout=subprocess.PIPE, text=True, close_fds=False) assert proc.stdout.rstrip() == repr(value)

os.unsetenv(name) proc = subprocess.run([sys.executable, '-c', code], check=True, stdout=subprocess.PIPE, text=True, close_fds=False) assert proc.stdout.rstrip() == repr(None)

I found it when I patched Python with #113118 (on Solaris, but this is pretty platform independent, I think).

CPython versions tested on:

3.9, 3.11

Operating systems tested on:

Other

Linked PRs