Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix test that calls setenv() without synchronization #64

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mcy
Copy link

@mcy mcy commented May 16, 2022

This test is flaky under the following interleaving:

  • exec_to_string() grabs MUTATE_ENV.
  • env_inherit_set() calls temp_env_var() which calls env::set_var().
  • exec_to_string() executes some asserts, one of which fails because
    the other test messed with the environment in a way exec_to_string()
    did not expect.
  • exec_to_string() panics and poisons MUTATE_ENV.
  • env_inherit_set() grabs the lock and panics because it's poisoned.

Taking the lock before setenv fixes this.

This test is flaky under the following interleaving:

- exec_to_string() grabs MUTATE_ENV.
- env_inherit_set() calls temp_env_var() which calls env::set_var().
- exec_to_string() executes some asserts, one of which fails because
  the other test messed with the environment in a way exec_to_string()
  did not expect.
- exec_to_string() panics and poisons MUTATE_ENV.
- env_inherit_set() grabs the lock and panics because it's poisoned.

Taking the lock *before* setenv fixes this.
@@ -317,10 +317,12 @@ struct TmpEnvVar<'a> {
}

impl<'a> TmpEnvVar<'a> {
fn new(varname: &'static str) -> TmpEnvVar<'a> {
fn new(varname: &'static str, tmp_value: &'static str) -> TmpEnvVar<'a> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with the change, I'd prefer TmpEnvVar::new() not to actually call setenv(). When possible, constructors should be side-effect-free.

The simplest fix would be to modify the body of tmp_env_var() to something like:

{
    let guard = TmpEnvVar::new(varname);
    env::set_var(varname, tmp_value);
    guard
}

Copy link
Author

@mcy mcy May 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that TmpEnvVar's sole purpose in life is to roll back a set_env call, maybe it might make sense to replace new with TmpEnvVar::set_var or something to that effect. I know this is test code, but the fact that TmpEnvVar::new(...) own will have a side effect when it is destroyed, it makes sense to relieve the possibility of surprise in this case by making it impossible to get into that situation.

(Like, I just wanna fix a flaky test; if you'd really rather I do it this way, that's fine by me.)

@mcy mcy requested a review from hniksic May 19, 2022 14:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants