Skip to content

Commit 8436e8f

Browse files
committed
Add discussion that concurrent access to the environment is unsafe
The bug report #27970 has existed for 8 years, the actual bug dates back to Rust pre-1.0. I documented it since it's in the interest of the user to be aware of it. The note can be removed once #27970 is fixed.
1 parent e8b8c78 commit 8436e8f

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

library/std/src/env.rs

+24-10
Original file line numberDiff line numberDiff line change
@@ -313,11 +313,18 @@ impl Error for VarError {
313313
/// Sets the environment variable `key` to the value `value` for the currently running
314314
/// process.
315315
///
316-
/// Note that while concurrent access to environment variables is safe in Rust,
317-
/// some platforms only expose inherently unsafe non-threadsafe APIs for
318-
/// inspecting the environment. As a result, extra care needs to be taken when
319-
/// auditing calls to unsafe external FFI functions to ensure that any external
320-
/// environment accesses are properly synchronized with accesses in Rust.
316+
/// Note that while concurrent access to environment variables ought to be safe
317+
/// in Rust, some platforms only expose inherently unsafe non-threadsafe APIs
318+
/// for inspecting the environment. As a result, using `set_var` or
319+
/// `remove_var` in a multi-threaded Rust program can lead to undefined
320+
/// behavior, for example in combination with DNS lookups from
321+
/// [`std::net::ToSocketAddrs`]. This is a bug
322+
/// ([rust#27970](https://github.com/rust-lang/rust/issues/27970)) and will be
323+
/// fixed in a future version of Rust. Additionally, extra care needs to be
324+
/// taken when auditing calls to unsafe external FFI functions to ensure that
325+
/// any external environment accesses are properly synchronized with accesses
326+
/// in Rust. Since Rust does not expose its environment lock directly, this
327+
/// means that all accesses to the environment must go through Rust's [`var`].
321328
///
322329
/// Discussion of this unsafety on Unix may be found in:
323330
///
@@ -351,11 +358,18 @@ fn _set_var(key: &OsStr, value: &OsStr) {
351358

352359
/// Removes an environment variable from the environment of the currently running process.
353360
///
354-
/// Note that while concurrent access to environment variables is safe in Rust,
355-
/// some platforms only expose inherently unsafe non-threadsafe APIs for
356-
/// inspecting the environment. As a result extra care needs to be taken when
357-
/// auditing calls to unsafe external FFI functions to ensure that any external
358-
/// environment accesses are properly synchronized with accesses in Rust.
361+
/// Note that while concurrent access to environment variables ought to be safe
362+
/// in Rust, some platforms only expose inherently unsafe non-threadsafe APIs
363+
/// for inspecting the environment. As a result, using `set_var` or
364+
/// `remove_var` in a multi-threaded Rust program can lead to undefined
365+
/// behavior, for example in combination with DNS lookups from
366+
/// [`std::net::ToSocketAddrs`]. This is a bug
367+
/// ([rust#27970](https://github.com/rust-lang/rust/issues/27970)) and will be
368+
/// fixed in a future version of Rust. Additionally, extra care needs to be
369+
/// taken when auditing calls to unsafe external FFI functions to ensure that
370+
/// any external environment accesses are properly synchronized with accesses
371+
/// in Rust. Since Rust does not expose its environment lock directly, this
372+
/// means that all accesses to the environment must go through Rust's [`var`].
359373
///
360374
/// Discussion of this unsafety on Unix may be found in:
361375
///

0 commit comments

Comments
 (0)