Skip to content

Commit ba02764

Browse files
committed
feat: abort all pending dials on stop
1 parent 404fa69 commit ba02764

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

src/dialer/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class Dialer {
3838
this.timeout = timeout
3939
this.perPeerLimit = perPeerLimit
4040
this.tokens = [...new Array(concurrency)].map((_, index) => index)
41+
this.pendingDials = new Set()
4142
}
4243

4344
/**
@@ -69,6 +70,12 @@ class Dialer {
6970
const signal = anySignal(signals)
7071
const timeoutId = setTimeout(() => timeoutController.abort(), this.timeout)
7172

73+
const dial = {
74+
dialRequest,
75+
controller: timeoutController
76+
}
77+
this.pendingDials.add(dial)
78+
7279
try {
7380
const dialResult = await dialRequest.run({ ...options, signal })
7481
clearTimeout(timeoutId)
@@ -81,6 +88,8 @@ class Dialer {
8188
}
8289
log.error(err)
8390
throw err
91+
} finally {
92+
this.pendingDials.delete(dial)
8493
}
8594
}
8695

src/index.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,15 @@ class Libp2p extends EventEmitter {
194194
log('libp2p is stopping')
195195

196196
try {
197-
this.pubsub && await this.pubsub.stop()
198-
this._dht && await this._dht.stop()
197+
await Promise.all([
198+
this.pubsub && this.pubsub.stop(),
199+
this._dht && this._dht.stop()
200+
])
201+
202+
for (const dial of this.dialer.pendingDials.values()) {
203+
dial.abort()
204+
}
205+
199206
await this.transportManager.close()
200207
await this.registrar.close()
201208
} catch (err) {

test/dialing/direct.spec.js

+20
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ describe('Dialing (direct, WebSockets)', () => {
169169

170170
// We should have 2 in progress, and 1 waiting
171171
expect(dialer.tokens).to.have.length(0)
172+
expect(dialer.pendingDials.size).to.equal(1) // 1 dial request
172173

173174
deferredDial.resolve(await createMockConnection())
174175

@@ -178,6 +179,7 @@ describe('Dialing (direct, WebSockets)', () => {
178179
// Only two dials will be run, as the first two succeeded
179180
expect(localTM.dial.callCount).to.equal(2)
180181
expect(dialer.tokens).to.have.length(2)
182+
expect(dialer.pendingDials.size).to.equal(0)
181183
})
182184

183185
describe('libp2p.dialer', () => {
@@ -278,5 +280,23 @@ describe('Dialing (direct, WebSockets)', () => {
278280
await libp2p.hangUp(connection.remotePeer)
279281
expect(connection.stat.timeline.close).to.exist()
280282
})
283+
284+
it('should abort pending dials on stop', async () => {
285+
libp2p = new Libp2p({
286+
peerInfo,
287+
modules: {
288+
transport: [Transport],
289+
streamMuxer: [Muxer],
290+
connEncryption: [Crypto]
291+
}
292+
})
293+
const abort = sinon.stub()
294+
const dials = [{ abort }, { abort }, { abort }]
295+
sinon.stub(libp2p.dialer, 'pendingDials').value(new Set(dials))
296+
297+
await libp2p.stop()
298+
299+
expect(abort).to.have.property('callCount', 3)
300+
})
281301
})
282302
})

test/peer-discovery/index.node.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ describe('peer discovery scenarios', () => {
2929
remotePeerInfo2.multiaddrs.add(multiaddr('/ip4/127.0.0.1/tcp/0'))
3030
})
3131

32-
afterEach(async () => {
32+
afterEach(async function () {
33+
// Increase timeout until abort support for dht queries is in place
34+
this.timeout(10e3)
3335
libp2p && await libp2p.stop()
3436
})
3537

0 commit comments

Comments
 (0)