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.

Title: Avoid modifying the process global environment (not thread safe)
Type: crash Stage: needs patch
Components: Interpreter Core, Subinterpreters Versions: Python 3.9
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: eric.snow, gregory.p.smith
Priority: normal Keywords:

Created on 2020-01-18 00:47 by gregory.p.smith, last changed 2022-04-11 14:59 by admin.

Messages (2)
msg360222 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2020-01-18 00:47
For more context, see which seeks to document the existing caveats.

POSIX lacks any APIs to access the process global environment in a thread safe manner.  Given this, we could _consider_ preventing os.putenv() and os.environ[x] = y assignment from actually modifying the process global environment.  They'd save their changes in our local os.environ underlying dict, set a flag that it was modified, but not modify the global.

This would be a visible behavior change and break _some_ class of code. :/

Our stdlib codepaths that launch a new process on POSIX could be modified to to always pass our a newly constructed envp from os.environ to exec/spawn APIs.  The os.system() API would need to stop using the POSIX system() API call in order for that to work.

Downside API breakage: Extension module modifications to the environment would not be picked up by Python interpreter launched subprocesses.  How much of a problem would that be in practice?

We may decide to close this as infeasible and just stick with the documentation of the sorry state of POSIX and not attempt to offer any safe non-crash-possible workarounds.
msg360225 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2020-01-18 01:33

This has impact on subinterpreters once they stop sharing the GIL.  (It's already on my list of global resources that need better protection.)
Date User Action Args
2022-04-11 14:59:25adminsetgithub: 83557
2021-04-16 00:43:07vstinnersetcomponents: + Subinterpreters
2020-01-18 01:33:02eric.snowsetnosy: + eric.snow
messages: + msg360225
2020-01-18 00:47:44gregory.p.smithcreate