Skip to content

Commit

Permalink
fix: avoid data race between Statistics/Run
Browse files Browse the repository at this point in the history
Writes to the following fields are now protected by `statsMu`:

- `ipaddr`
- `addr`
- `PacketsSent`
- `PacketsRecvDuplicates`

Signed-off-by: Per Hallgren <perhallgren@users.noreply.github.com>
  • Loading branch information
perhallgren committed Feb 3, 2025
1 parent 38075b8 commit cb7d28f
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,10 @@ func (p *Pinger) updateStatistics(pkt *Packet) {
func (p *Pinger) SetIPAddr(ipaddr *net.IPAddr) {
p.ipv4 = isIPv4(ipaddr.IP)

p.statsMu.Lock()
p.ipaddr = ipaddr
p.addr = ipaddr.String()
p.statsMu.Unlock()
}

// IPAddr returns the ip address of the target host.
Expand Down Expand Up @@ -407,7 +409,9 @@ func (p *Pinger) Resolve() error {

p.ipv4 = isIPv4(ipaddr.IP)

p.statsMu.Lock()
p.ipaddr = ipaddr
p.statsMu.Unlock()

return nil
}
Expand All @@ -416,10 +420,14 @@ func (p *Pinger) Resolve() error {
// DNS name like "www.google.com" or IP like "127.0.0.1".
func (p *Pinger) SetAddr(addr string) error {
oldAddr := p.addr
p.statsMu.Lock()
p.addr = addr
p.statsMu.Unlock()
err := p.Resolve()
if err != nil {
p.statsMu.Lock()
p.addr = oldAddr
p.statsMu.Unlock()
return err
}
return nil
Expand Down Expand Up @@ -852,7 +860,9 @@ func (p *Pinger) processPacket(recv *packet) error {
inPkt.Seq = pkt.Seq
// If we've already received this sequence, ignore it.
if _, inflight := p.awaitingSequences[*pktUUID][pkt.Seq]; !inflight {
p.statsMu.Lock()
p.PacketsRecvDuplicates++
p.statsMu.Unlock()
if p.OnDuplicateRecv != nil {
p.OnDuplicateRecv(inPkt)
}
Expand Down Expand Up @@ -945,7 +955,9 @@ func (p *Pinger) sendICMP(conn packetConn) error {
}
// mark this sequence as in-flight
p.awaitingSequences[currentUUID][p.sequence] = struct{}{}
p.statsMu.Lock()
p.PacketsSent++
p.statsMu.Unlock()
p.sequence++
if p.sequence > 65535 {
newUUID := uuid.New()
Expand Down

0 comments on commit cb7d28f

Please sign in to comment.