-
-
Notifications
You must be signed in to change notification settings - Fork 29.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Avoid modifying the process global environment (not thread safe) #83557
Comments
For more context, see https://bugs.python.org/issue39375 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. |
+1 This has impact on subinterpreters once they stop sharing the GIL. (It's already on my list of global resources that need better protection.) |
@colesbury, is this something that needs to be addressed for free-threaded builds? |
given modifying the C/kernel provided environment blob is already broken regardless of free-threaded builds, I doubt it needs special attention beyond making sure the existing those APIs haven't been compatible with the modern world for decades. |
If we went with my idea in the opening message of tracking local modifications, it might be a challenge. But my gut feeling is that we just won't bother doing this? I don't recall the last time I've seen this come up as an issue. Hopefully that means people just don't do it and pass new env= mappings to subprocesses? |
I agree with what @gpshead wrote -- I think we should document it, but I don't think there's a good fix for it at the Python level. The same problem affects other languages and runtimes:
Rachel Kroll had a blog post ~7 years ago about setenv that concludes with:
I expect we'll have users run into this problem every so often, even with updated docs. It's too bad because I think the technical fix for glibc, etc. is not too complex. |
While I mostly like to pretend https://github.com/python/cpython/blob/main/Modules/posixmodule.c#L7884 |
Permalink: Line 7884 in 0890ad7
|
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: