Pewarta Proxy

📋 Server Request & Proxy Logs

Loading active proxy settings...
Developed by Khairul Adha (adhexa.id)
Mengambil log server...

⚙️ Pengaturan Proxy Target

Skip to content

subprocess.Popen can pass incomplete environment when posix_spawn is used #113119

Closed

Description

@kulikjak

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

Activity

  1. self-assigned this
    on Dec 15, 2023
  2. added a commit that references this issue on Dec 17, 2023
  3. gpshead commented on Dec 19, 2023

    @gpshead
    Member

    reopened due to @ned-deily reporting on a parallel issue #113117 about this change:

    This breaks macOS framework builds such as those used by the python.org installer:

    $ ./configure --enable-framework && make -j
    [...]
    ./Modules/posixmodule.c:7170:19: error: use of undeclared identifier 'environ'
            envlist = environ;
                      ^
    ./Modules/posixmodule.c:7238:31: error: use of undeclared identifier 'environ'
        if (envlist && envlist != environ) {
                                  ^
    

    Note the comments elsewhere in posixmodule.c such as at line 1582:

    #elif defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
        /* environ is not accessible as an extern in a shared object on OSX; use
           _NSGetEnviron to resolve it. The value changes if you add environment
           variables between calls to Py_Initialize, so don't cache the value. */
        e = *_NSGetEnviron();
    
    
  4. added a commit that references this issue on Dec 19, 2023
  5. kulikjak commented on Dec 19, 2023

    @kulikjak
    ContributorAuthor

    Sorry for that, and thank you for the fix. Since CI was green, I didn't look into it much more and missed that.

  6. added a commit that references this issue on Dec 26, 2023
  7. added 2 commits that reference this issue on Feb 11, 2024
  8. added 2 commits that reference this issue on Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

type-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions