Skip to content

Commit 712cef9

Browse files
committed
libstd: Add non-Rust threadsafety warnings for setenv()
See: https://sourceware.org/bugzilla/show_bug.cgi?id=4887#c9 https://bugs.freedesktop.org/show_bug.cgi?id=65681 I just noticed this while talking to someone who was using `os.environ['FOO'] = 'BAR'` in Python and since I'm learning Rust, I was curious if it did anything special here. It looks like Rust has an internal mutex, which helps for apps that are pure Rust, but it will be an evil trap for someone later adding in native code (apps like Servo and games will be at risk). Java got this right by disallowing `setenv()` from the start.
1 parent 69e47c7 commit 712cef9

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/libstd/env.rs

+14
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,13 @@ impl Error for VarError {
243243
/// Sets the environment variable `k` to the value `v` for the currently running
244244
/// process.
245245
///
246+
/// Warning: While an an internal mutex is used to ensure concurrent
247+
/// access is safe in Rust code calling `libstd`, there is no such
248+
/// protection for non-Rust threads the same process concurrently
249+
/// invoking the underlying system `getenv()` (on Unix at least).
250+
/// Avoid calling this API except very early in process startup,
251+
/// when it is known that no other threads are running.
252+
///
246253
/// # Examples
247254
///
248255
/// ```
@@ -262,6 +269,13 @@ pub fn set_var<K: ?Sized, V: ?Sized>(k: &K, v: &V)
262269

263270
/// Removes an environment variable from the environment of the currently running process.
264271
///
272+
/// Warning: While an an internal mutex is used to ensure concurrent
273+
/// access is safe in Rust code calling `libstd`, there is no such
274+
/// protection for non-Rust threads the same process concurrently
275+
/// invoking the underlying system `getenv()` (on Unix at least).
276+
/// Avoid calling this API except very early in process startup,
277+
/// when it is known that no other threads are running.
278+
///
265279
/// # Examples
266280
///
267281
/// ```

0 commit comments

Comments
 (0)