Skip to content
This repository was archived by the owner on Jul 21, 2023. It is now read-only.

Commit b805cab

Browse files
authoredNov 30, 2021
chore: guard on table refresh running (#257)
We queue up ping requests to see if we should evict peers from the routing table - if we are being shut down the requests will be aborted so check to make sure we are still running before starting another. Also, don't evict a peer because the request failed if we aren't running as it may have been cancelled which doesn't mean the remote peer is invalid.
1 parent c2c5782 commit b805cab

File tree

4 files changed

+38
-18
lines changed

4 files changed

+38
-18
lines changed
 

‎src/routing-table/index.js

+23-9
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,29 @@ class RoutingTable {
3535
this._pingTimeout = pingTimeout || 10000
3636

3737
/** @type {KBucketTree} */
38-
this.kb = new KBuck({
39-
numberOfNodesPerKBucket: this._kBucketSize,
40-
numberOfNodesToPing: 1
41-
})
38+
this.kb // eslint-disable-line no-unused-expressions
4239

4340
/** @type {Date[]} */
4441
this.commonPrefixLengthRefreshedAt = []
4542

4643
this._onPing = this._onPing.bind(this)
4744
this._pingQueue = new Queue({ concurrency: 1 })
45+
this._running = false
4846
}
4947

5048
async start () {
51-
this.kb.localNodeId = await utils.convertPeerId(this._peerId)
49+
this._running = true
50+
51+
this.kb = new KBuck({
52+
localNodeId: await utils.convertPeerId(this._peerId),
53+
numberOfNodesPerKBucket: this._kBucketSize,
54+
numberOfNodesToPing: 1
55+
})
5256
this.kb.on('ping', this._onPing)
5357
}
5458

5559
async stop () {
60+
this._running = false
5661
this._pingQueue.clear()
5762
}
5863

@@ -74,6 +79,10 @@ class RoutingTable {
7479
// flood the network with ping requests if lots of newContact requests
7580
// are received
7681
this._pingQueue.add(async () => {
82+
if (!this._running) {
83+
return
84+
}
85+
7786
let responded = 0
7887

7988
try {
@@ -83,16 +92,21 @@ class RoutingTable {
8392

8493
try {
8594
timeoutController = new TimeoutController(this._pingTimeout)
95+
8696
this._log(`pinging old contact ${oldContact.peer}`)
8797
const { stream } = await this._dialer.dialProtocol(oldContact.peer, PROTOCOL_DHT, {
8898
signal: timeoutController.signal
8999
})
90100
await stream.close()
91101
responded++
92102
} catch (/** @type {any} */ err) {
93-
this._log.error('could not ping peer %p', oldContact.peer, err)
94-
this._log(`evicting old contact after ping failed ${oldContact.peer}`)
95-
this.kb.remove(oldContact.id)
103+
if (this._running) {
104+
// only evict peers if we are still running, otherwise we evict when dialing is
105+
// cancelled due to shutdown in progress
106+
this._log.error('could not ping peer %p', oldContact.peer, err)
107+
this._log(`evicting old contact after ping failed ${oldContact.peer}`)
108+
this.kb.remove(oldContact.id)
109+
}
96110
} finally {
97111
if (timeoutController) {
98112
timeoutController.clear()
@@ -101,7 +115,7 @@ class RoutingTable {
101115
})
102116
)
103117

104-
if (responded < oldContacts.length) {
118+
if (this._running && responded < oldContacts.length) {
105119
this._log(`adding new contact ${newContact.peer}`)
106120
this.kb.add(newContact)
107121
}

‎src/routing-table/refresh.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,6 @@ class RoutingTableRefresh {
218218
* and the current peer
219219
*/
220220
_maxCommonPrefix () {
221-
if (!this._routingTable.kb.localNodeId) {
222-
return 0
223-
}
224-
225221
// xor our KadId with every KadId in the k-bucket tree,
226222
// return the longest id prefix that is the same
227223
let prefixLength = 0
@@ -256,6 +252,10 @@ class RoutingTableRefresh {
256252
* Yields the common prefix length of every peer in the table
257253
*/
258254
* _prefixLengths () {
255+
if (!this._routingTable.kb) {
256+
return
257+
}
258+
259259
for (const { id } of this._routingTable.kb.toIterable()) {
260260
const distance = uint8ArrayXor(this._routingTable.kb.localNodeId, id)
261261
let leadingZeros = 0

‎test/kad-dht.spec.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -766,11 +766,10 @@ describe('KadDHT', () => {
766766
tdht.connect(dhts[2], dhts[3])
767767
])
768768

769-
const stub = sinon.stub(dhts[0]._lan._routingTable, 'closestPeers').returns([])
769+
dhts[0]._lan.findPeer = sinon.stub().returns([])
770+
dhts[0]._wan.findPeer = sinon.stub().returns([])
770771

771772
await expect(drain(dhts[0].findPeer(ids[3]))).to.eventually.be.rejected().property('code', 'ERR_LOOKUP_FAILED')
772-
773-
stub.restore()
774773
})
775774
})
776775
})

‎test/routing-table.spec.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ describe('Routing Table', () => {
2626
peerId: lipbp2p.peerId,
2727
dialer: lipbp2p
2828
})
29+
await table.start()
30+
})
31+
32+
afterEach(async () => {
33+
await table.stop()
2934
})
3035

3136
it('add', async function () {
@@ -90,7 +95,8 @@ describe('Routing Table', () => {
9095
table._pingQueue = {
9196
add: (f) => {
9297
fn = f
93-
}
98+
},
99+
clear: () => {}
94100
}
95101

96102
const peerIds = [
@@ -135,7 +141,8 @@ describe('Routing Table', () => {
135141
table._pingQueue = {
136142
add: (f) => {
137143
fn = f
138-
}
144+
},
145+
clear: () => {}
139146
}
140147

141148
const peerIds = [

0 commit comments

Comments
 (0)
This repository has been archived.