Skip to content

test: Unsafe usage of env::set_var #7651

@olix0r

Description

@olix0r

In the proxy's test initialization code, we use std::env::set_var to configure a log format:

pub fn trace_subscriber(default: impl ToString) -> (Dispatch, Handle) {
    let log_level = env::var("LINKERD2_PROXY_LOG")
        .or_else(|_| env::var("RUST_LOG"))
        .unwrap_or_else(|_| default.to_string());
    let log_format = env::var("LINKERD2_PROXY_LOG_FORMAT").unwrap_or_else(|_| "PLAIN".to_string());
    env::set_var("LINKERD2_PROXY_LOG_FORMAT", &log_format);
    // This may fail, since the global log compat layer may have been
    // initialized by another test.
    let _ = init_log_compat();
    Settings::for_test(log_level, log_format).build()
}

My understanding is that this is unsafe, per rust-lang/rust#90308, because our tests run in multiple threads. It would be great if we could avoid environment mutation in our codebase.

Can we remove this? Do we actually care about JSON formatting in our tests?

Are there tools we could use to, for instance, make clippy fail on new uses of set_var/remove_var?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions