Skip to content

Commit 8057586

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 8057586

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

library/std/src/env.rs

+28-10
Original file line numberDiff line numberDiff line change
@@ -313,17 +313,26 @@ 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
///
324331
/// - [Austin Group Bugzilla](https://austingroupbugs.net/view.php?id=188)
325332
/// - [GNU C library Bugzilla](https://sourceware.org/bugzilla/show_bug.cgi?id=15607#c2)
326333
///
334+
/// [`std::net::ToSocketAddrs`]: crate::net::ToSocketAddrs
335+
///
327336
/// # Panics
328337
///
329338
/// This function may panic if `key` is empty, contains an ASCII equals sign `'='`
@@ -351,17 +360,26 @@ fn _set_var(key: &OsStr, value: &OsStr) {
351360

352361
/// Removes an environment variable from the environment of the currently running process.
353362
///
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.
363+
/// Note that while concurrent access to environment variables ought to be safe
364+
/// in Rust, some platforms only expose inherently unsafe non-threadsafe APIs
365+
/// for inspecting the environment. As a result, using `set_var` or
366+
/// `remove_var` in a multi-threaded Rust program can lead to undefined
367+
/// behavior, for example in combination with DNS lookups from
368+
/// [`std::net::ToSocketAddrs`]. This is a bug
369+
/// ([rust#27970](https://github.com/rust-lang/rust/issues/27970)) and will be
370+
/// fixed in a future version of Rust. Additionally, extra care needs to be
371+
/// taken when auditing calls to unsafe external FFI functions to ensure that
372+
/// any external environment accesses are properly synchronized with accesses
373+
/// in Rust. Since Rust does not expose its environment lock directly, this
374+
/// means that all accesses to the environment must go through Rust's [`var`].
359375
///
360376
/// Discussion of this unsafety on Unix may be found in:
361377
///
362378
/// - [Austin Group Bugzilla](https://austingroupbugs.net/view.php?id=188)
363379
/// - [GNU C library Bugzilla](https://sourceware.org/bugzilla/show_bug.cgi?id=15607#c2)
364380
///
381+
/// [`std::net::ToSocketAddrs`]: crate::net::ToSocketAddrs
382+
///
365383
/// # Panics
366384
///
367385
/// This function may panic if `key` is empty, contains an ASCII equals sign

0 commit comments

Comments
 (0)