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

Gregory P. Smith report at bugs.python.org
Fri Jan 17 19:36:08 EST 2020


New submission from Gregory P. Smith <greg at krypto.org>:

The underlying API calls made by os.putenv() and os.environ[name] = value syntax are not thread safe on POSIX systems.  POSIX _does not have_ any thread safe way to access the process global environment.

In a pure Python program, the GIL prevents this from being an issue.  But when embedded in a C/C++ program or using extension modules that launch their own threads from C, those threads could also make the invalid assumption that they can safely _read_ the environment.  Which is a race condition when a Python thread is doing a putenv() at the same time.

We should document the danger.

CPython's os module snapshots a copy of the environment into a dict at import time (during CPython startup).  But os.environ[] assignment and os.putenv() modify the actual process global environment in addition to updating this dict.  (If an embedded interpreter is launched from a process with other threads already running, even that initial environment reading could be unsafe if the larger application has a thread that wrongly assumes it has exclusive environment access)

For people modifying os.environ so that the change is visible to child processes, we can recommend using the env= parameter on subprocess API calls to supply a new environment.

A broader issue of should we be modifying the process global environment state at all from os.putenv() and os.environ[] assignment still exists.  I'll track that in another issue (to be opened).

----------
assignee: docs at python
components: Documentation
messages: 360221
nosy: docs at python, gregory.p.smith
priority: normal
severity: normal
status: open
title: Document os.environ[x] = y and os.putenv() as thread unsafe
versions: Python 2.7, Python 3.7, Python 3.8, Python 3.9

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


More information about the New-bugs-announce mailing list