From d01c14193202748b5ae46bee051dff91c08bb2e0 Mon Sep 17 00:00:00 2001 From: Dominik Roos Date: Fri, 15 Sep 2023 10:04:56 +0200 Subject: [PATCH] scion/ping: wait for send go routine to terminate Wait for the ping sender go routine to terminate before we return the stats. With bad luck, the receiver go routine receives all the replies before the sender go routine has terminated. Because only the sender go routine increments the stats.Sent count, we end up with wrong stats that claim more packets were received then sent. --- scion/ping/ping.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/scion/ping/ping.go b/scion/ping/ping.go index c332bce42c..4efea528d5 100644 --- a/scion/ping/ping.go +++ b/scion/ping/ping.go @@ -20,6 +20,7 @@ import ( "encoding/binary" "math/rand" "net" + "sync" "time" "github.com/scionproto/scion/pkg/addr" @@ -178,8 +179,12 @@ func (p *pinger) Ping(ctx context.Context, remote *snet.UDPAddr) (Stats, error) p.drain(ctx) }() + var wg sync.WaitGroup + wg.Add(1) + go func() { defer log.HandlePanic() + defer wg.Done() for i := uint16(0); i < p.attempts; i++ { if err := p.send(remote); err != nil { errSend <- serrors.WrapStr("sending", err) @@ -210,6 +215,7 @@ func (p *pinger) Ping(ctx context.Context, remote *snet.UDPAddr) (Stats, error) p.receive(reply) } } + wg.Wait() return p.stats, nil }