Fix race condition in statsd external_env initialization#352
Merged
StephenWakely merged 1 commit intoDataDog:masterfrom Jan 30, 2026
Merged
Conversation
Add sync.RWMutex to protect the externalEnv package variable from concurrent access when multiple goroutines call statsd.New() simultaneously. Uses RWMutex (not sync.Once) to allow re-initialization if the DD_EXTERNAL_ENV environment variable changes between client creations in the same process. Fixes data race detected in parallel tests at external_env.go:19
vickenty
approved these changes
Jan 30, 2026
StephenWakely
approved these changes
Jan 30, 2026
Merged
This was referenced Feb 2, 2026
6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A number of tests in one of our repos initiate a statsd client and we're consistently hitting data race failures while running unit tests with the race detector enabled.
This PR fixes the data race in external_env.go where externalEnv is accessed without synchronization during concurrent client creation.
The fix uses sync.RWMutex to protect reads and writes, allowing re-initialization across different environments in the same process while preventing races.
There are no API changes and a minimal performance impact on hot path (RLock for reads).
I tested with go test -race across parallel test suites in our repo and things look good.