Skip to content

Data races when using crate's temp_env::with_vars_unset #36

@skatromb

Description

@skatromb

Spent some time debugging problem in tests using temp_env::with_vars_unset(). Even though it's used only in one test, it could interfere with the rest tests, because uses unsafe fn std::env::set_var.

I think it's worth noting that potential problem at README.md. I may do a PR if you're okay with that.

Here is zipped example of code setup that shows the problem.

code_example.zip

Same issue happens when I use temp_env::with_var with env variable export X=value

#[cfg(test)]
mod tests {
    use std::{thread::sleep, time::Duration};

    #[test]
    fn test_unset() {
        temp_env::with_var("X", Some("blah"), || {
            sleep(Duration::from_millis(10));
            assert_eq!(std::env::var("X").unwrap(), "blah");
        });
    }

    #[test] 
    fn test_read() {
        sleep(Duration::from_millis(1));
        assert_eq!(std::env::var("X").unwrap(), "value");
    }
}

Second test test_read fails, because gets "blah"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions