Skip to content

Conversation

@JeremieRodon
Copy link
Contributor

Problem

In the init::config::tests module, the usage of std::env::set_var and std::env::remove_var can cause a race condition which can occasionally fail the cargo test invocation.

This happens when 2 tests using the EnvManager type are running in parallel in different threads of the test binary.
You can verify that on any Linux system by running (e.g.):

cargo test test_get_multi_exporter_config_datadog_ -- --test-threads 3

By selecting those 3 datadog tests that use the EnvManager and ensuring parallelism on 3 threads, you should be able to reproduce the race condition 100% of the time. Note that without the --test-threads 3 argument, you should also run multi-threaded unless you have a single core. Also note that even when simply running cargo test to run all tests, the race condition will happen occasionally out of bad luck (that's how I found the problem).

Solution

I introduced a static Mutex<()> in the EnvManager. When calling EnvManager::new(), the Mutex guard becomes part of the EnvManager struct, ensuring only one EnvManager can exist at a time among the threads of the test binary. Essentially, it guarantees that tests using the EnvManager are run sequentially and do not interfere with each other.

…usage of env::set_var and env::remove_var in the inherently multi-threaded test binary
@JeremieRodon JeremieRodon force-pushed the fix/race-condition-in-init-config-tests branch from 500aaee to 25829a0 Compare January 5, 2026 16:39
@mheffner mheffner self-requested a review January 5, 2026 18:31
Copy link
Contributor

@mheffner mheffner left a comment

Choose a reason for hiding this comment

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

Nice! We've been mostly using nextest which will run these as separate processes, but it's good to keep this safe for cargo test runs too.

@mheffner mheffner merged commit 83298b4 into streamfold:main Jan 5, 2026
4 checks passed
@JeremieRodon JeremieRodon deleted the fix/race-condition-in-init-config-tests branch January 6, 2026 09:12
@JeremieRodon
Copy link
Contributor Author

I followed DEVELOPING.md like a good boy 😄

If tests should be performed with another tool, it could be desirable to update it at some point 🙂

@mheffner
Copy link
Contributor

mheffner commented Jan 6, 2026

If tests should be performed with another tool, it could be desirable to update it at some point 🙂

Ahh! Good point, I'll update those.

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