Skip to content

Commit a0abe0f

Browse files
committed
fix: clear the registrar when libp2p stops
1 parent eafb934 commit a0abe0f

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ class Libp2p extends EventEmitter {
187187
this.pubsub && await this.pubsub.stop()
188188
this._dht && await this._dht.stop()
189189
await this.transportManager.close()
190+
await this.registrar.close()
190191
} catch (err) {
191192
if (err) {
192193
log.error(err)

src/registrar.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,23 @@ class Registrar {
4646
this._handle = handle
4747
}
4848

49+
/**
50+
* Cleans up the registrar
51+
* @async
52+
*/
53+
async close () {
54+
// Close all connections we're tracking
55+
const tasks = []
56+
for (const connectionList of this.connections.values()) {
57+
for (const connection of connectionList) {
58+
tasks.push(connection.close())
59+
}
60+
}
61+
62+
await tasks
63+
this.connections.clear()
64+
}
65+
4966
/**
5067
* Add a new connected peer to the record
5168
* TODO: this should live in the ConnectionManager

test/registrar/registrar.node.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,19 @@ describe('registrar on dial', () => {
5454
const remoteConn = remoteLibp2p.registrar.getConnection(peerInfo)
5555
expect(remoteConn).to.exist()
5656
})
57+
58+
it('should be closed on libp2p stop', async () => {
59+
libp2p = new Libp2p(mergeOptions(baseOptions, {
60+
peerInfo
61+
}))
62+
63+
await libp2p.dial(remoteAddr)
64+
expect(libp2p.registrar.connections.size).to.equal(1)
65+
66+
sinon.spy(libp2p.registrar, 'close')
67+
68+
await libp2p.stop()
69+
expect(libp2p.registrar.close.callCount).to.equal(1)
70+
expect(libp2p.registrar.connections.size).to.equal(0)
71+
})
5772
})

0 commit comments

Comments
 (0)