Skip to content

Commit

Permalink
[probes.external] Fix a race condition in server mode (#744)
Browse files Browse the repository at this point in the history
  • Loading branch information
apolcyn committed May 19, 2024
1 parent d4bfee7 commit 333e6f1
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions probes/external/external_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ func (p *Probe) runServerProbe(ctx, startCtx context.Context) {

outstandingReqs := make(map[int32]requestInfo)
var outstandingReqsMu sync.RWMutex
sendDoneCh := make(chan struct{})

if err := p.startCmdIfNotRunning(startCtx); err != nil {
p.l.Error(err.Error())
Expand All @@ -212,6 +211,7 @@ func (p *Probe) runServerProbe(ctx, startCtx context.Context) {

// Read probe replies until we have no outstanding requests or context has
// run out.
expectedRepliesReceived := 0
for {
select {
case <-ctx.Done():
Expand All @@ -221,6 +221,7 @@ func (p *Probe) runServerProbe(ctx, startCtx context.Context) {
outstandingReqsMu.Lock()
reqInfo, ok := outstandingReqs[rep.GetRequestId()]
if ok {
expectedRepliesReceived++
delete(outstandingReqs, rep.GetRequestId())
}
outstandingReqsMu.Unlock()
Expand All @@ -243,14 +244,10 @@ func (p *Probe) runServerProbe(ctx, startCtx context.Context) {
p.processProbeResult(ps, p.results[reqInfo.target.Key()])
}

// If we are done sending requests, we can exit if we have no
// outstanding requests.
select {
case <-sendDoneCh:
if len(outstandingReqs) == 0 {
return
}
default:
// We send a total if len(p.targets) requests. We can exit if we've
// seen replies for all of them.
if expectedRepliesReceived == len(p.targets) {
return
}
}
}()
Expand All @@ -269,9 +266,6 @@ func (p *Probe) runServerProbe(ctx, startCtx context.Context) {
time.Sleep(TimeBetweenRequests)
}

// Send signal to receiver loop that we are done sending requests.
close(sendDoneCh)

// Wait for receiver goroutine to exit.
wg.Wait()

Expand Down

0 comments on commit 333e6f1

Please sign in to comment.