[issue39375] Document os.environ[x] = y and os.putenv() as thread unsafe

Eryk Sun report at bugs.python.org
Wed Jan 22 03:11:06 EST 2020


Eryk Sun <eryksun at gmail.com> added the comment:

> no need to remove that message.  

I was discussing the wrong API. It's not directly relevant that Windows API functions that access the process environment are protected by the PEB lock. Python primarily uses [_w]environ, a copy of the process environment that's managed by the C runtime library (ucrt). os.putenv modifies this environment via _wputenv. (Ultimately this syncs with the process environment by calling SetEnvironmentVariableW.)

Functions that modify and read ucrt's environment are protected by a lock. But there's still a concern if multithreaded code reads or modifies [_w]environ concurrent to a _[w]putenv call. Also, [_w]getenv returns a pointer to the value in [_w]environ, so it has the same problem. A significant difference, however, is that _[w]putenv in ucrt is not POSIX compliant, since it copies the caller's string. Also, ucrt has safer [_w]getenv_s and _[w]dupenv_s functions that return a copy.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39375>
_______________________________________


More information about the Python-bugs-list mailing list