-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Add discussion that concurrent access to the environment is unsafe #116888
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -313,17 +313,26 @@ impl Error for VarError { | |
/// Sets the environment variable `key` to the value `value` for the currently running | ||
/// process. | ||
/// | ||
/// Note that while concurrent access to environment variables is safe in Rust, | ||
/// some platforms only expose inherently unsafe non-threadsafe APIs for | ||
/// inspecting the environment. As a result, extra care needs to be taken when | ||
/// auditing calls to unsafe external FFI functions to ensure that any external | ||
/// environment accesses are properly synchronized with accesses in Rust. | ||
/// Note that while concurrent access to environment variables ought to be safe | ||
/// in Rust, some platforms only expose inherently unsafe non-threadsafe APIs | ||
/// for inspecting the environment. As a result, using `set_var` or | ||
/// `remove_var` in a multi-threaded Rust program can lead to undefined | ||
/// behavior, for example in combination with DNS lookups from | ||
/// [`std::net::ToSocketAddrs`]. This is a bug | ||
/// ([rust#27970](https://github.com/rust-lang/rust/issues/27970)) and will be | ||
/// fixed in a future version of Rust. Additionally, extra care needs to be | ||
/// taken when auditing calls to unsafe external FFI functions to ensure that | ||
/// any external environment accesses are properly synchronized with accesses | ||
/// in Rust. Since Rust does not expose its environment lock directly, this | ||
/// means that all accesses to the environment must go through Rust's [`var`]. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is requesting something impossible in most cases. I'm not sure that's reasonable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you have a suggestion what to write there? Yes, it's unreasonable, but that's the current state of the function AFAICT. |
||
/// | ||
/// Discussion of this unsafety on Unix may be found in: | ||
/// | ||
/// - [Austin Group Bugzilla](https://austingroupbugs.net/view.php?id=188) | ||
/// - [GNU C library Bugzilla](https://sourceware.org/bugzilla/show_bug.cgi?id=15607#c2) | ||
/// | ||
/// [`std::net::ToSocketAddrs`]: crate::net::ToSocketAddrs | ||
/// | ||
/// # Panics | ||
/// | ||
/// This function may panic if `key` is empty, contains an ASCII equals sign `'='` | ||
|
@@ -351,17 +360,26 @@ fn _set_var(key: &OsStr, value: &OsStr) { | |
|
||
/// Removes an environment variable from the environment of the currently running process. | ||
/// | ||
/// Note that while concurrent access to environment variables is safe in Rust, | ||
/// some platforms only expose inherently unsafe non-threadsafe APIs for | ||
/// inspecting the environment. As a result extra care needs to be taken when | ||
/// auditing calls to unsafe external FFI functions to ensure that any external | ||
/// environment accesses are properly synchronized with accesses in Rust. | ||
/// Note that while concurrent access to environment variables ought to be safe | ||
/// in Rust, some platforms only expose inherently unsafe non-threadsafe APIs | ||
/// for inspecting the environment. As a result, using `set_var` or | ||
/// `remove_var` in a multi-threaded Rust program can lead to undefined | ||
/// behavior, for example in combination with DNS lookups from | ||
/// [`std::net::ToSocketAddrs`]. This is a bug | ||
/// ([rust#27970](https://github.com/rust-lang/rust/issues/27970)) and will be | ||
/// fixed in a future version of Rust. Additionally, extra care needs to be | ||
/// taken when auditing calls to unsafe external FFI functions to ensure that | ||
/// any external environment accesses are properly synchronized with accesses | ||
/// in Rust. Since Rust does not expose its environment lock directly, this | ||
/// means that all accesses to the environment must go through Rust's [`var`]. | ||
/// | ||
/// Discussion of this unsafety on Unix may be found in: | ||
/// | ||
/// - [Austin Group Bugzilla](https://austingroupbugs.net/view.php?id=188) | ||
/// - [GNU C library Bugzilla](https://sourceware.org/bugzilla/show_bug.cgi?id=15607#c2) | ||
/// | ||
/// [`std::net::ToSocketAddrs`]: crate::net::ToSocketAddrs | ||
/// | ||
/// # Panics | ||
/// | ||
/// This function may panic if `key` is empty, contains an ASCII equals sign | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this makes it sound like rust will fix the concurrent access problem, but it cannot. it should say that this function will probably become unsafe in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It also seems to me that get_var has the same problems, right? It's reading data that might be changed by concurrent C code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
getenv
is usually considered safe in a multithreaded C environment, butsetenv
/putenv
is not, I do not think that it requires an extra note.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, C code designed for multithreaded use will almost never write to the env.