From ebce7de7eb11d9efa8f32522af65a81915bec755 Mon Sep 17 00:00:00 2001 From: Jeromy Date: Tue, 18 Oct 2016 17:10:22 -0700 Subject: [PATCH] fix race conditions in dial_sync and limiter tests --- dial_sync.go | 1 - limiter_test.go | 10 +++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/dial_sync.go b/dial_sync.go index 75d3f0f..48b7789 100644 --- a/dial_sync.go +++ b/dial_sync.go @@ -81,7 +81,6 @@ func (ds *DialSync) DialLock(ctx context.Context, p peer.ID) (*Conn, error) { ad.conn, ad.err = ds.dialFunc(ctx, p) close(ad.waitch) ad.cancel() - ad.waitch = nil // to ensure nobody tries reusing this }(ctx, p, actd) } diff --git a/limiter_test.go b/limiter_test.go index 646019d..cbb6afd 100644 --- a/limiter_test.go +++ b/limiter_test.go @@ -5,6 +5,7 @@ import ( "fmt" "math/rand" "strconv" + "sync" "testing" "time" @@ -170,13 +171,17 @@ func TestFDLimiting(t *testing.T) { } func TestTokenRedistribution(t *testing.T) { + var lk sync.Mutex hangchs := make(map[peer.ID]chan struct{}) df := func(ctx context.Context, p peer.ID, a ma.Multiaddr) (iconn.Conn, error) { if tcpPortOver(a, 10) { return (iconn.Conn)(nil), nil } - <-hangchs[p] + lk.Lock() + ch := hangchs[p] + lk.Unlock() + <-ch return nil, fmt.Errorf("test bad dial") } l := newDialLimiterWithParams(df, 8, 4) @@ -190,6 +195,9 @@ func TestTokenRedistribution(t *testing.T) { // take all fd limit tokens with hang dials for _, pid := range pids { hangchs[pid] = make(chan struct{}) + } + + for _, pid := range pids { tryDialAddrs(ctx, l, pid, bads, resch) } -- GitLab