Skip to content

Commit 4aff0ce

Browse files
committed
cmd/opampsupervisor: fix race condition in e2e test causing send on closed channel
1 parent 3b8ffd2 commit 4aff0ce

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

cmd/opampsupervisor/e2e_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,15 @@ func newUnstartedOpAMPServer(t *testing.T, connectingCallback onConnectingFuncFa
156156
return
157157
}
158158
isAgentConnected.Store(false)
159-
connectedChan <- false
159+
// Guard against sending on a closed channel during test cleanup.
160+
// The channel may have been closed if shutdown takes longer than the
161+
// waitForSupervisorConnection timeout (5 seconds). Using a non-blocking
162+
// send with a brief timeout prevents panic while still signaling disconnect.
163+
select {
164+
case connectedChan <- false:
165+
case <-time.After(100 * time.Millisecond):
166+
// Channel closed or already drained; this can happen during teardown
167+
}
160168
if onConnectionCloseFunc != nil {
161169
onConnectionCloseFunc(conn)
162170
}
@@ -174,6 +182,11 @@ func newUnstartedOpAMPServer(t *testing.T, connectingCallback onConnectingFuncFa
174182

175183
shutdown := func() {
176184
if !didShutdown.Load() {
185+
// Set shutdown flag BEFORE closing channel to prevent OnConnectionClose
186+
// from attempting to send on a closed channel. The flag is checked in the
187+
// OnConnectionClose callback, so setting it here ensures any late-firing
188+
// callbacks will see the flag and return early.
189+
didShutdown.Store(true)
177190
waitForSupervisorConnection(connectedChan, false)
178191
t.Log("Shutting down")
179192
err := s.Stop(t.Context())
@@ -186,7 +199,6 @@ func newUnstartedOpAMPServer(t *testing.T, connectingCallback onConnectingFuncFa
186199
}
187200
close(connectedChan)
188201
}
189-
didShutdown.Store(true)
190202
}
191203
send := func(msg *protobufs.ServerToAgent) {
192204
if !isAgentConnected.Load() {

0 commit comments

Comments
 (0)