Skip to content

Fix race condition in statsd external_env initialization#352

Merged
StephenWakely merged 1 commit intoDataDog:masterfrom
jprobinson:jprobinson/fix-external-env-race
Jan 30, 2026
Merged

Fix race condition in statsd external_env initialization#352
StephenWakely merged 1 commit intoDataDog:masterfrom
jprobinson:jprobinson/fix-external-env-race

Conversation

@jprobinson
Copy link
Copy Markdown
Member

@jprobinson jprobinson commented Jan 29, 2026

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.

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
@jprobinson jprobinson requested a review from a team as a code owner January 29, 2026 19:27
@StephenWakely StephenWakely merged commit e20587f into DataDog:master Jan 30, 2026
42 checks passed
@StephenWakely StephenWakely mentioned this pull request Feb 2, 2026
This was referenced Feb 2, 2026
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.

3 participants