[receiver/statsd] fix data race in TCP server shutdown #42986
+39
−2
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.
Description
Fixes a data race that occurs when shutdown is called while a new connection is created.
ListenAndServe
runs in a goroutine created here, and apparently WaitGroups aren't safe to use in this way.The UDP and UDS implementations don't use a WaitGroup and are unaffected.
Race detector log
================== WARNING: DATA RACE Write at 0x00c0000f8c08 by goroutine 15: runtime.racewrite() <autogenerated>:1 +0x1e github.com/open-telemetry/opentelemetry-collector-contrib/receiver/statsdreceiver/internal/transport.(*tcpServer).Close() /home/semaphore/opentelemetry-collector-contrib/receiver/statsdreceiver/internal/transport/tcp_server.go:114 +0x48 github.com/open-telemetry/opentelemetry-collector-contrib/receiver/statsdreceiver/internal/transport.Test_Server_ListenAndServe.func1() /home/semaphore/opentelemetry-collector-contrib/receiver/statsdreceiver/internal/transport/server_test.go:112 +0xb64 testing.tRunner() /home/semaphore/.local/share/mise/installs/go/1.23.11/packages/pkg/mod/golang.org/toolchain@v0.0.1-go1.24.0.linux-amd64/src/testing/testing.go:1792 +0x225 testing.(*T).Run.gowrap1() /home/semaphore/.local/share/mise/installs/go/1.23.11/packages/pkg/mod/golang.org/toolchain@v0.0.1-go1.24.0.linux-amd64/src/testing/testing.go:1851 +0x44 Previous read at 0x00c0000f8c08 by goroutine 17: runtime.raceread() <autogenerated>:1 +0x1e github.com/open-telemetry/opentelemetry-collector-contrib/receiver/statsdreceiver/internal/transport.(*tcpServer).ListenAndServe() /home/semaphore/opentelemetry-collector-contrib/receiver/statsdreceiver/internal/transport/tcp_server.go:72 +0x2ae github.com/open-telemetry/opentelemetry-collector-contrib/receiver/statsdreceiver/internal/transport.Test_Server_ListenAndServe.func1.1() /home/semaphore/opentelemetry-collector-contrib/receiver/statsdreceiver/internal/transport/server_test.go:84 +0x105 Goroutine 15 (running) created at: testing.(*T).Run() /home/semaphore/.local/share/mise/installs/go/1.23.11/packages/pkg/mod/golang.org/toolchain@v0.0.1-go1.24.0.linux-amd64/src/testing/testing.go:1851 +0x8f2 github.com/open-telemetry/opentelemetry-collector-contrib/receiver/statsdreceiver/internal/transport.Test_Server_ListenAndServe() /home/semaphore/opentelemetry-collector-contrib/receiver/statsdreceiver/internal/transport/server_test.go:58 +0x28b testing.tRunner() /home/semaphore/.local/share/mise/installs/go/1.23.11/packages/pkg/mod/golang.org/toolchain@v0.0.1-go1.24.0.linux-amd64/src/testing/testing.go:1792 +0x225 testing.(*T).Run.gowrap1() /home/semaphore/.local/share/mise/installs/go/1.23.11/packages/pkg/mod/golang.org/toolchain@v0.0.1-go1.24.0.linux-amd64/src/testing/testing.go:1851 +0x44 Goroutine 17 (running) created at: github.com/open-telemetry/opentelemetry-collector-contrib/receiver/statsdreceiver/internal/transport.Test_Server_ListenAndServe.func1() /home/semaphore/opentelemetry-collector-contrib/receiver/statsdreceiver/internal/transport/server_test.go:82 +0x7bc testing.tRunner() /home/semaphore/.local/share/mise/installs/go/1.23.11/packages/pkg/mod/golang.org/toolchain@v0.0.1-go1.24.0.linux-amd64/src/testing/testing.go:1792 +0x225 testing.(*T).Run.gowrap1() /home/semaphore/.local/share/mise/installs/go/1.23.11/packages/pkg/mod/golang.org/toolchain@v0.0.1-go1.24.0.linux-amd64/src/testing/testing.go:1851 +0x44 ================== testing.go:1490: race detected during execution of test === �[31mFAIL�[0m: internal/transport Test_Server_ListenAndServe (1.01s)