Skip to main content

Module env_lock

Module env_lock 

Source
Expand description

Process-environment mutation lock.

Rust 2024 marks std::env::set_var and std::env::remove_var as unsafe because POSIX setenv/getenv are not thread-safe. Each call site that needs to mutate the environment previously rolled its own OnceLock<Mutex<()>> plus duplicated set_env_var / remove_env_var helpers carrying their own SAFETY: comments. This module consolidates that invariant into a single sound wrapper.

§Usage

Acquire the guard once, then mutate the environment through its methods. The guard holds a process-wide Mutex so concurrent callers serialize automatically.

use vtcode_commons::env_lock;

let env = env_lock::lock();
env.set_var("MY_TEST_VAR", "1");
// ... run code that reads MY_TEST_VAR ...
env.remove_var("MY_TEST_VAR");

All in-process code that mutates the environment must go through this module; direct std::env::set_var / std::env::remove_var calls bypass the lock and re-introduce the data race the wrapper is here to prevent.

Structs§

EnvGuard
RAII guard that proves ownership of the process-wide environment lock.

Functions§

lock
Acquire the process-wide environment lock.
remove_var
One-shot safe wrapper around std::env::remove_var.
set_var
One-shot safe wrapper around std::env::set_var.