rust-unsafe-env-mutation

Serialize Rust environment variable mutations with a static Mutex guard.

1|Updated Nov 25, 2025
One-click install
npx skills add https://github.com/89jobrien/dotfiles --skill rust-unsafe-env-mutation
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: rust-unsafe-env-mutation
Source: https://github.com/89jobrien/dotfiles/tree/main/dot-claude/skills/rust-unsafe-env-mutation
Command: npx skills add https://github.com/89jobrien/dotfiles --skill rust-unsafe-env-mutation

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

In Rust 2024, mutating process-wide environment variables with std::env::set_var or remove_var can cause data races when tests run in parallel. This pattern introduces a static Mutex<()> to serialize access and prevent flaky CI results.

Core Features & Use Cases

  • Serialization with a shared mutex: guard env var mutations behind a single lock to ensure consistency across tests.
  • Cross-file safety: applies to multiple test modules that touch the same environment variables.
  • Cleanup guarantees: ensures env vars are restored after tests, even if a test panics.

Quick Start

Create a static ENV_MUTEX and wrap all env-var mutations with a lock around the critical section.

Frequently Asked Questions about rust-unsafe-env-mutation

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
Why do my Rust tests fail when modifying environment variables in parallel?

Rust tests fail during parallel environment variable mutations because std::env::set_var causes process-wide data races. Serializing these mutations with a static Mutex guard prevents concurrent access and eliminates flaky CI results.

How do I serialize environment variable mutations across multiple Rust test files?

Serialize environment variable mutations across multiple test files by applying a single static Mutex<()> guard around all set_var and remove_var critical sections. This enforces cross-file safety and consistent test execution.

Can I ensure environment variables are restored if a Rust test panics?

Ensure environment variables are restored after a test panic by wrapping env var mutations in a mutex lock that provides cleanup guarantees. This ensures the environment is safely restored even on panic paths.

What is the best way to prevent data races with std::env::set_var in Rust CI pipelines?

Prevent data races with std::env::set_var in CI pipelines by serializing process-wide environment variable mutations with a static Mutex. This ensures tests running in parallel cannot concurrently mutate the same env vars.

Does a shared Mutex work for env var cleanup across different Rust crates?

A shared static Mutex works for env var cleanup across different Rust crates by serializing access to process-wide environment variable mutations. It ensures consistent state and cleanup guarantees across all test modules.