Commit 682bcc86 authored by Steven Allen's avatar Steven Allen

fix listen addrs race

Copy the listen address list before returning it.

If you're wondering about the syntax:
https://github.com/go101/go101/wiki/How-to-perfectly-clone-a-slice%3F
parent 4a42085d
......@@ -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