Skip to content

Commit a0cd3e5

Browse files
[receiver/statsd] fix data race in TCP server shutdown
1 parent 8e05fdb commit a0cd3e5

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: statsdreceiver
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Fixes a data race that can occur during shutdown of the statsdreceiver's TCP server implementation.
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [42986]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: [api]

receiver/statsdreceiver/internal/transport/tcp_server.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ var errTCPServerDone = errors.New("server stopped")
2020
type tcpServer struct {
2121
listener net.Listener
2222
reporter Reporter
23-
wg sync.WaitGroup
23+
wg *sync.WaitGroup
24+
wgMu sync.Mutex
2425
transport Transport
2526
stopChan chan struct{}
2627
}
@@ -30,7 +31,9 @@ var _ Server = (*tcpServer)(nil)
3031

3132
// NewTCPServer creates a transport.Server using TCP as its transport.
3233
func NewTCPServer(transport Transport, address string) (Server, error) {
33-
var tsrv tcpServer
34+
tsrv := tcpServer{
35+
wg: &sync.WaitGroup{},
36+
}
3437
var err error
3538

3639
if !transport.IsStreamTransport() {
@@ -69,7 +72,10 @@ LOOP:
6972

7073
select {
7174
case conn := <-connChan:
75+
// Potential data race here because t.wg.Add is called concurrently with t.wg.Wait in Close().
76+
t.wgMu.Lock()
7277
t.wg.Add(1)
78+
t.wgMu.Unlock()
7379
go t.handleConn(conn, transferChan)
7480
case <-t.stopChan:
7581
break LOOP
@@ -88,7 +94,9 @@ func (t *tcpServer) handleConn(c net.Conn, transferChan chan<- Metric) {
8894
if !errors.Is(err, io.EOF) {
8995
t.reporter.OnDebugf("TCP transport (%s) Error reading payload: %v", c.LocalAddr(), err)
9096
}
97+
t.wgMu.Lock()
9198
t.wg.Done()
99+
t.wgMu.Unlock()
92100
return
93101
}
94102
buf := bytes.NewBuffer(append(remainder, payload[0:n]...))
@@ -111,6 +119,8 @@ func (t *tcpServer) handleConn(c net.Conn, transferChan chan<- Metric) {
111119
// Close closes the server.
112120
func (t *tcpServer) Close() error {
113121
close(t.stopChan)
122+
t.wgMu.Lock()
114123
t.wg.Wait()
124+
t.wgMu.Unlock()
115125
return t.listener.Close()
116126
}

0 commit comments

Comments
 (0)