Skip to content

std::env members need to document "process global" risks. #69691

Open
@kentfredric

Description

@kentfredric

I've hit a few bugs in various crate test suites where 2 different tests both modify POSIX ENV, and then execute code and assertions based on those changes.

Often, this works simply due to the code being fast enough, but you can randomly get weird failures due to the data races in ENV, due to ENV being shared between threads.

But I don't see any documentation pointing out either:

a. That this risk occurs
b. How this risk occurs
c. How to manage this risk.

The best I see is: https://doc.rust-lang.org/nightly/std/env/fn.set_var.html

But its current prose of "Note that while concurrent access to environment variables is safe in Rust" can give the false impression that there isn't any problem to manage here unless one is doing FFI.

It needs to be more explicit, and highlight that you can trip into this when writing just plain ol' rust without anything fancy, with no external crates, and no custom ffi code.

( And probably needs to be mentioned at a higher level, like https://doc.rust-lang.org/nightly/std/env/index.html )

In tests, its especially spicy, as a test failing (which is ultimately a panic!), results in the Mutex becoming poisoned, which, if not managed properly in the tests, results in all tests that need to lock ENV, also failing with confusing errors, due to said poisoning.

There's already 2 PR's where I've submitted working solutions for this, and perhaps some of the strategies I've used could be used as a baseline for some documented guidance.

( Personally I wish rust wrapped the env access controls to protect code against this sort of problem, but asking for that is a bit of a tall order for me, so a documentation solution is better than nothing )

: sagiegurari/ci_info#9
: seanmonstar/reqwest#837

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsA-processArea: `std::process` and `std::env`C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions