Skip to content

Commit 983f5a7

Browse files
committed
p2p/nat: fix concurrent access to autodisc Interface
Concurrent calls to Interface methods on autodisc could return a "not discovered" error if the discovery did not finish before the call. autodisc.wait expected the done channel to carry the found Interface but it was closed instead. The fix is to use sync.Once for now, which is easier to get right. And there is a test. Finally. This will have to change again when we introduce re-discovery.
1 parent f7fdb4d commit 983f5a7

2 files changed

Lines changed: 63 additions & 17 deletions

File tree

‎p2p/nat/nat.go‎

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -172,18 +172,20 @@ func PMP(gateway net.IP) Interface {
172172
// This type is useful because discovery can take a while but we
173173
// want return an Interface value from UPnP, PMP and Auto immediately.
174174
type autodisc struct {
175-
what string
176-
done <-chan Interface
175+
what string // type of interface being autodiscovered
176+
once sync.Once
177+
doit func() Interface
177178

178179
mu sync.Mutex
179180
found Interface
180181
}
181182

182183
func startautodisc(what string, doit func() Interface) Interface {
183184
// TODO: monitor network configuration and rerun doit when it changes.
184-
done := make(chan Interface)
185-
ad := &autodisc{what: what, done: done}
186-
go func() { done <- doit(); close(done) }()
185+
ad := &autodisc{what: what, doit: doit}
186+
// Start the auto discovery as early as possible so it is already
187+
// in progress when the rest of the stack calls the methods.
188+
go ad.wait()
187189
return ad
188190
}
189191

@@ -218,19 +220,15 @@ func (n *autodisc) String() string {
218220
}
219221
}
220222

223+
// wait blocks until auto-discovery has been performed.
221224
func (n *autodisc) wait() error {
222-
n.mu.Lock()
223-
found := n.found
224-
n.mu.Unlock()
225-
if found != nil {
226-
// already discovered
227-
return nil
228-
}
229-
if found = <-n.done; found == nil {
230-
return errors.New("no UPnP or NAT-PMP router discovered")
225+
n.once.Do(func() {
226+
n.mu.Lock()
227+
n.found = n.doit()
228+
n.mu.Unlock()
229+
})
230+
if n.found == nil {
231+
return fmt.Errorf("no %s router discovered", n.what)
231232
}
232-
n.mu.Lock()
233-
n.found = found
234-
n.mu.Unlock()
235233
return nil
236234
}

‎p2p/nat/nat_test.go‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package nat
2+
3+
import (
4+
"bytes"
5+
"net"
6+
"testing"
7+
"time"
8+
)
9+
10+
// This test checks that autodisc doesn't hang and returns
11+
// consistent results when multiple goroutines call its methods
12+
// concurrently.
13+
func TestAutoDiscRace(t *testing.T) {
14+
ad := startautodisc("thing", func() Interface {
15+
time.Sleep(500 * time.Millisecond)
16+
return extIP{33, 44, 55, 66}
17+
})
18+
19+
// Spawn a few concurrent calls to ad.ExternalIP.
20+
type rval struct {
21+
ip net.IP
22+
err error
23+
}
24+
results := make(chan rval, 50)
25+
for i := 0; i < cap(results); i++ {
26+
go func() {
27+
ip, err := ad.ExternalIP()
28+
results <- rval{ip, err}
29+
}()
30+
}
31+
32+
// Check that they all return the correct result within the deadline.
33+
deadline := time.After(550 * time.Millisecond)
34+
for i := 0; i < cap(results); i++ {
35+
select {
36+
case <-deadline:
37+
t.Fatal("deadline exceeded")
38+
case rval := <-results:
39+
if rval.err != nil {
40+
t.Errorf("result %d: unexpected error: %v", i, rval.err)
41+
}
42+
wantIP := net.IP{33, 44, 55, 66}
43+
if !bytes.Equal(rval.ip, wantIP) {
44+
t.Errorf("result %d: got IP %v, want %v", i, rval.ip, wantIP)
45+
}
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)