This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author eryksun
Recipients docs@python, eryksun, gregory.p.smith
Date 2020-01-18.14:07:02
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1579356422.98.0.720330231724.issue39375@roundup.psfhosted.org>
In-reply-to
Content
The warning would not apply to Windows. The environment block is part of the Process Environment Block (PEB) record, which is protected by a critical-section lock. The runtime library acquires the PEB lock before accessing mutable PEB values. For example:

Getting an environment variable:

    >>> win32api.GetEnvironmentVariable('foo')

        Breakpoint 0 hit
        ntdll!RtlQueryEnvironmentVariable:
        00007ffc`d737a2f0 48895c2408      mov     qword ptr [rsp+8],rbx
            ss:00000094`ec9ef470=0000000000000000

RtlQueryEnvironmentVariable acquires the PEB lock (i.e. ntdll!FastPebLock) before getting the value. The lock is passed to RtlEnterCriticalSection in register rcx:

        0:000> be 2; g

        Breakpoint 2 hit
        ntdll!RtlEnterCriticalSection:
        00007ffc`d737b400 4883ec28        sub     rsp,28h

        0:000> kc 3
        Call Site
        ntdll!RtlEnterCriticalSection
        ntdll!RtlQueryEnvironmentVariable
        KERNELBASE!GetEnvironmentVariableW

        0:000> ?? @rcx == (_RTL_CRITICAL_SECTION *)@@(ntdll!FastPebLock)
        bool true

Setting an environment variable:

    >>> win32api.SetEnvironmentVariable('foo', 'eggs')

        Breakpoint 1 hit
        ntdll!RtlSetEnvironmentVar:
        00007ffc`d73bc7d0 4c894c2420      mov     qword ptr [rsp+20h],r9 ss:00000094`ec9ef488=0000000000000000

RtlSetEnvironmentVar acquires the PEB lock before setting the environment variable:

        0:000> be 2; g

        Breakpoint 2 hit
        ntdll!RtlEnterCriticalSection:
        00007ffc`d737b400 4883ec28        sub     rsp,28h

        0:000> kc 3
        Call Site
        ntdll!RtlEnterCriticalSection
        ntdll!RtlSetEnvironmentVar
        KERNELBASE!SetEnvironmentVariableW

        0:000> ?? @rcx == (_RTL_CRITICAL_SECTION *)@@(ntdll!FastPebLock)
        bool true
History
Date User Action Args
2020-01-22 00:50:05eryksununlinkissue39375 messages
2020-01-18 14:07:03eryksunsetrecipients: + eryksun, gregory.p.smith, docs@python
2020-01-18 14:07:02eryksunsetmessageid: <1579356422.98.0.720330231724.issue39375@roundup.psfhosted.org>
2020-01-18 14:07:02eryksunlinkissue39375 messages
2020-01-18 14:07:02eryksuncreate