-
-
Notifications
You must be signed in to change notification settings - Fork 14.7k
Consider deprecating and/or modifying behavior of std::env::set_var #90308
Copy link
Copy link
Closed
Labels
A-processArea: `std::process` and `std::env`Area: `std::process` and `std::env`C-bugCategory: This is a bug.Category: This is a bug.E-help-wantedCall for participation: Help is requested to fix this issue.Call for participation: Help is requested to fix this issue.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.
Metadata
Metadata
Assignees
Labels
A-processArea: `std::process` and `std::env`Area: `std::process` and `std::env`C-bugCategory: This is a bug.Category: This is a bug.E-help-wantedCall for participation: Help is requested to fix this issue.Call for participation: Help is requested to fix this issue.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.
Type
Fields
Give feedbackNo fields configured for issues without a type.
Observing an environment variable concurrently with a call to
setenvconstitutes a data race. Rust currently handles this by using a mutex, but that only protects against functions instd::env. My interpretation of POSIX (which appears to be the same as the glibc developers') is that any libc function is allowed to callgetenv. This has already caused problems withgetaddrinfoin #27970.Additionally, a large amount of C code calls
getenvbut is otherwise thread-safe. While this isn't necessarily the standard library's issue, it's impossible for third-party libraries to soundly use this code without requiring the user to certify that this isn't an issue via anunsafeblock. Some examples of this happening in practice are in time-rs/time#293 and rust-lang/flate2-rs#272.rustsec/advisory-db#926 had several proposals on how this could be handled brought up.
Make
std::env::set_varunsafeThis is arguably the cleanest solution to the issue. This could be considered a soundness fix, which would make it an option, but the ecosystem impact feels like it'd be too big.
Don't actually call
setenvstdcould keep track of the environment itself, and makeset_varchanges only visible tovarand subprocesses. This is probably the way to solve the issue with the least impact on existing code, but the behavior is somewhat unexpected and not zero-cost.Only call
setenvin single-threaded codeThis would reduce the impact further, but seems even less expected for negligible benefit.
Deprecate
std::env::set_varThis would make it clear that setting an environment variable in the current process is discouraged. It could also be combined with not actually calling
setenv, which would be my preferred solution to this issue.