Unverified Commit bfc3909f authored by Steven Allen's avatar Steven Allen Committed by GitHub

Merge pull request #136 from libp2p/fix/listen-addrs-race

fix listen addrs race
parents 221d8d5e 682bcc86
......@@ -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
......@@ -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
}
......@@ -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")
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment