diff --git a/.chloggen/statsdreceiver-fix-race.yaml b/.chloggen/statsdreceiver-fix-race.yaml new file mode 100644 index 0000000000000..bc9c608d3e9a4 --- /dev/null +++ b/.chloggen/statsdreceiver-fix-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: Fix a data race in statsdreceiver on shutdown + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [42878] + +# (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: [user] diff --git a/receiver/statsdreceiver/internal/transport/tcp_server.go b/receiver/statsdreceiver/internal/transport/tcp_server.go index 435708fe3c17d..918329db46b9b 100644 --- a/receiver/statsdreceiver/internal/transport/tcp_server.go +++ b/receiver/statsdreceiver/internal/transport/tcp_server.go @@ -19,10 +19,8 @@ var errTCPServerDone = errors.New("server stopped") type tcpServer struct { listener net.Listener - reporter Reporter wg sync.WaitGroup transport Transport - stopChan chan struct{} } // Ensure that Server is implemented on TCP Server. @@ -43,7 +41,6 @@ func NewTCPServer(transport Transport, address string) (Server, error) { return nil, fmt.Errorf("starting to listen %s socket: %w", transport.String(), err) } - tsrv.stopChan = make(chan struct{}) return &tsrv, nil } @@ -53,42 +50,34 @@ func (t *tcpServer) ListenAndServe(nextConsumer consumer.Metrics, reporter Repor return errNilListenAndServeParameters } - t.reporter = reporter -LOOP: for { - connChan := make(chan net.Conn, 1) - go func() { - c, err := t.listener.Accept() - if err != nil { - t.reporter.OnDebugf("TCP Transport - Accept error: %v", - err) - } else { - connChan <- c + conn, err := t.listener.Accept() + if err != nil { + if errors.Is(err, net.ErrClosed) { + return errTCPServerDone } - }() - - select { - case conn := <-connChan: - t.wg.Add(1) - go t.handleConn(conn, transferChan) - case <-t.stopChan: - break LOOP + reporter.OnDebugf("TCP Transport - Accept error: %v", err) + continue } + + t.wg.Add(1) + go func() { + defer t.wg.Done() + handleTCPConn(conn, reporter, transferChan) + }() } - return errTCPServerDone } -// handleConn is helper that parses the buffer and split it line by line to be parsed upstream. -func (t *tcpServer) handleConn(c net.Conn, transferChan chan<- Metric) { +// handleTCPConn is helper that parses the buffer and split it line by line to be parsed upstream. +func handleTCPConn(c net.Conn, reporter Reporter, transferChan chan<- Metric) { payload := make([]byte, 4096) var remainder []byte for { n, err := c.Read(payload) if err != nil { if !errors.Is(err, io.EOF) { - t.reporter.OnDebugf("TCP transport (%s) Error reading payload: %v", c.LocalAddr(), err) + reporter.OnDebugf("TCP transport (%s) Error reading payload: %v", c.LocalAddr(), err) } - t.wg.Done() return } buf := bytes.NewBuffer(append(remainder, payload[0:n]...)) @@ -110,7 +99,7 @@ func (t *tcpServer) handleConn(c net.Conn, transferChan chan<- Metric) { // Close closes the server. func (t *tcpServer) Close() error { - close(t.stopChan) + err := t.listener.Close() t.wg.Wait() - return t.listener.Close() + return err }