Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4feb881

Browse files
committedMay 29, 2024
Elaborate about modifying env vars in multi-threaded programs
1 parent 2500719 commit 4feb881

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed
 

‎std/src/env.rs

+28-18
Original file line numberDiff line numberDiff line change
@@ -323,15 +323,20 @@ impl Error for VarError {
323323
/// This function is also always safe to call on Windows, in single-threaded
324324
/// and multi-threaded programs.
325325
///
326-
/// In multi-threaded programs on other operating systems, you must ensure that
327-
/// are no other threads concurrently writing or *reading*(!) from the
328-
/// environment through functions other than the ones in this module. You are
329-
/// responsible for figuring out how to achieve this, but we strongly suggest
330-
/// not using `set_var` or `remove_var` in multi-threaded programs at all.
331-
///
332-
/// Most C libraries, including libc itself, do not advertise which functions
333-
/// read from the environment. Even functions from the Rust standard library do
334-
/// that, e.g. for DNS lookups from [`std::net::ToSocketAddrs`].
326+
/// In multi-threaded programs on other operating systems, we strongly suggest
327+
/// not using `set_var` or `remove_var` at all. The exact requirement is: you
328+
/// must ensure that there are no other threads concurrently writing or
329+
/// *reading*(!) the environment through functions or global variables other
330+
/// than the ones in this module. The problem is that these operating systems
331+
/// do not provide a thread-safe way to read the environment, and most C
332+
/// libraries, including libc itself, do not advertise which functions read
333+
/// from the environment. Even functions from the Rust standard library may
334+
/// read the environment without going through this module, e.g. for DNS
335+
/// lookups from [`std::net::ToSocketAddrs`]. No stable guarantee is made about
336+
/// which functions may read from the environment in future versions of a
337+
/// library. All this makes it not practically possible for you to guarantee
338+
/// that no other thread will read the environment, so the only safe option is
339+
/// to not use `set_var` or `remove_var` in multi-threaded programs at all.
335340
///
336341
/// Discussion of this unsafety on Unix may be found in:
337342
///
@@ -385,15 +390,20 @@ unsafe fn _set_var(key: &OsStr, value: &OsStr) {
385390
/// This function is also always safe to call on Windows, in single-threaded
386391
/// and multi-threaded programs.
387392
///
388-
/// In multi-threaded programs, you must ensure that are no other threads
389-
/// concurrently writing or *reading*(!) from the environment through functions
390-
/// other than the ones in this module. You are responsible for figuring out
391-
/// how to achieve this, but we strongly suggest not using `set_var` or
392-
/// `remove_var` in multi-threaded programs at all.
393-
///
394-
/// Most C libraries, including libc itself, do not advertise which functions
395-
/// read from the environment. Even functions from the Rust standard library do
396-
/// that, e.g. for DNS lookups from [`std::net::ToSocketAddrs`].
393+
/// In multi-threaded programs on other operating systems, we strongly suggest
394+
/// not using `set_var` or `remove_var` at all. The exact requirement is: you
395+
/// must ensure that there are no other threads concurrently writing or
396+
/// *reading*(!) the environment through functions or global variables other
397+
/// than the ones in this module. The problem is that these operating systems
398+
/// do not provide a thread-safe way to read the environment, and most C
399+
/// libraries, including libc itself, do not advertise which functions read
400+
/// from the environment. Even functions from the Rust standard library may
401+
/// read the environment without going through this module, e.g. for DNS
402+
/// lookups from [`std::net::ToSocketAddrs`]. No stable guarantee is made about
403+
/// which functions may read from the environment in future versions of a
404+
/// library. All this makes it not practically possible for you to guarantee
405+
/// that no other thread will read the environment, so the only safe option is
406+
/// to not use `set_var` or `remove_var` in multi-threaded programs at all.
397407
///
398408
/// Discussion of this unsafety on Unix may be found in:
399409
///

0 commit comments

Comments
 (0)
Failed to load comments.