diff --git a/les/freeclient.go b/les/freeclient.go index 8f3c7bb61a..4d1c83dd62 100644 --- a/les/freeclient.go +++ b/les/freeclient.go @@ -90,6 +90,8 @@ func newFreeClientPool(db ethdb.Database, connectedLimit, totalLimit int, quit c // connect should be called after a successful handshake. If the connection was // rejected, there is no need to call disconnect. +// +// Note: the disconnectFn callback should not block. func (f *freeClientPool) connect(address string, disconnectFn func()) bool { f.lock.Lock() defer f.lock.Unlock() @@ -121,7 +123,7 @@ func (f *freeClientPool) connect(address string, disconnectFn func()) bool { i.connected = false f.disconnPool.Push(i, -i.logUsage) log.Debug("Client kicked out", "address", i.address) - go i.disconnectFn() + i.disconnectFn() } else { // keep the old client and reject the new one f.connPool.Push(i, i.linUsage) diff --git a/les/freeclient_test.go b/les/freeclient_test.go index eb7eee78b5..f78a3dda8c 100644 --- a/les/freeclient_test.go +++ b/les/freeclient_test.go @@ -52,7 +52,7 @@ func testFreeClientPool(t *testing.T, connLimit, clientCount int) { pool = newFreeClientPool(db, connLimit, 10000, quit, &wg, &clock) connected = make([]bool, clientCount) connTicks = make([]int, clientCount) - disconnCh = make(chan int) + disconnCh = make(chan int, clientCount) ) peerId := func(i int) string { return fmt.Sprintf("test peer #%d", i) @@ -78,8 +78,7 @@ func testFreeClientPool(t *testing.T, connLimit, clientCount int) { } // randomly connect and disconnect peers, expect to have a similar total connection time at the end - tickCounter := 0 - for ; tickCounter < testFreeClientPoolTicks; tickCounter++ { + for tickCounter := 0; tickCounter < testFreeClientPoolTicks; tickCounter++ { clock.Run(1 * time.Second) i := rand.Intn(clientCount) @@ -115,7 +114,7 @@ func testFreeClientPool(t *testing.T, connLimit, clientCount int) { // check if the total connected time of peers are all in the expected range for i, c := range connected { if c { - connTicks[i] += tickCounter + connTicks[i] += testFreeClientPoolTicks } if connTicks[i] < expMin || connTicks[i] > expMax { t.Errorf("Total connected time of test node #%d (%d) outside expected range (%d to %d)", i, connTicks[i], expMin, expMax) @@ -124,7 +123,7 @@ func testFreeClientPool(t *testing.T, connLimit, clientCount int) { // a previously unknown peer should be accepted now if !pool.connect("newPeer", func() {}) { - t.Errorf("Previously unknown peer rejected") + t.Fatalf("Previously unknown peer rejected") } // close and restart pool diff --git a/les/handler.go b/les/handler.go index e478a2c3c9..babc1d62fe 100644 --- a/les/handler.go +++ b/les/handler.go @@ -292,7 +292,7 @@ func (pm *ProtocolManager) handle(p *peer) error { // test peer address is not a tcp address, don't use client pool if can not typecast if ok { id := addr.IP.String() - if !pm.clientPool.connect(id, func() { pm.removePeer(p.id) }) { + if !pm.clientPool.connect(id, func() { go pm.removePeer(p.id) }) { return p2p.DiscTooManyPeers } defer pm.clientPool.disconnect(id)