diff --git a/.chloggen/fix-statsd-tcp-server-data-race.yaml b/.chloggen/fix-statsd-tcp-server-data-race.yaml new file mode 100644 index 0000000000000..95f16df6bc65b --- /dev/null +++ b/.chloggen/fix-statsd-tcp-server-data-race.yaml @@ -0,0 +1,27 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: bug_fix + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: statsdreceiver + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Fixes a data race that can occur during shutdown of the statsdreceiver's TCP server implementation. + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [42986] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [api] diff --git a/receiver/statsdreceiver/internal/transport/tcp_server.go b/receiver/statsdreceiver/internal/transport/tcp_server.go index 435708fe3c17d..935a37b779c47 100644 --- a/receiver/statsdreceiver/internal/transport/tcp_server.go +++ b/receiver/statsdreceiver/internal/transport/tcp_server.go @@ -20,7 +20,8 @@ var errTCPServerDone = errors.New("server stopped") type tcpServer struct { listener net.Listener reporter Reporter - wg sync.WaitGroup + wg *sync.WaitGroup + wgMu sync.Mutex transport Transport stopChan chan struct{} } @@ -30,7 +31,9 @@ var _ Server = (*tcpServer)(nil) // NewTCPServer creates a transport.Server using TCP as its transport. func NewTCPServer(transport Transport, address string) (Server, error) { - var tsrv tcpServer + tsrv := tcpServer{ + wg: &sync.WaitGroup{}, + } var err error if !transport.IsStreamTransport() { @@ -69,7 +72,10 @@ LOOP: select { case conn := <-connChan: + // Potential data race here because t.wg.Add is called concurrently with t.wg.Wait in Close(). + t.wgMu.Lock() t.wg.Add(1) + t.wgMu.Unlock() go t.handleConn(conn, transferChan) case <-t.stopChan: break LOOP @@ -88,7 +94,9 @@ func (t *tcpServer) handleConn(c net.Conn, transferChan chan<- Metric) { if !errors.Is(err, io.EOF) { t.reporter.OnDebugf("TCP transport (%s) Error reading payload: %v", c.LocalAddr(), err) } + t.wgMu.Lock() t.wg.Done() + t.wgMu.Unlock() return } buf := bytes.NewBuffer(append(remainder, payload[0:n]...)) @@ -111,6 +119,8 @@ func (t *tcpServer) handleConn(c net.Conn, transferChan chan<- Metric) { // Close closes the server. func (t *tcpServer) Close() error { close(t.stopChan) + t.wgMu.Lock() t.wg.Wait() + t.wgMu.Unlock() return t.listener.Close() }