Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

fix listen addrs race #136

Merged
merged 1 commit into from
Aug 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions swarm_addr.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func (s *Swarm) InterfaceListenAddresses() ([]ma.Multiaddr, error) {
s.listeners.RUnlock() // RLock end

if ifaceListenAddres != nil && !isEOL {
// Cache is valid
return ifaceListenAddres, nil
// Cache is valid, clone the slice
return append(ifaceListenAddres[:0:0], ifaceListenAddres...), nil
}

// Cache is not valid
Expand Down Expand Up @@ -64,5 +64,5 @@ func (s *Swarm) InterfaceListenAddresses() ([]ma.Multiaddr, error) {

s.listeners.Unlock() // Lock end

return ifaceListenAddres, nil
return append(ifaceListenAddres[:0:0], ifaceListenAddres...), nil
}
18 changes: 18 additions & 0 deletions swarm_addr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,21 @@ func TestDialBadAddrs(t *testing.T) {
test(m("/ip6/fe80::100")) // link local
test(m("/ip4/127.0.0.1/udp/1234/utp")) // utp
}

func TestAddrRace(t *testing.T) {
ctx := context.Background()
s := makeSwarms(ctx, t, 1)[0]

a1, err := s.InterfaceListenAddresses()
if err != nil {
t.Fatal(err)
}
a2, err := s.InterfaceListenAddresses()
if err != nil {
t.Fatal(err)
}

if len(a1) > 0 && len(a2) > 0 && &a1[0] == &a2[0] {
t.Fatal("got the exact same address set twice; this could lead to data races")
}
}