Skip to content

Commit

Permalink
fix pair bug when core closes (#407)
Browse files Browse the repository at this point in the history
  • Loading branch information
mafintosh authored Jul 26, 2023
1 parent a60d17b commit ee3b7ca
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion lib/replicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,7 @@ module.exports = class Replicator {
this.peers = []
this.findingPeers = 0 // updateable from the outside

this._attached = new Set()
this._inflight = new InflightTracker()
this._blocks = new BlockTracker()
this._hashes = new BlockTracker()
Expand All @@ -995,6 +996,13 @@ module.exports = class Replicator {
this._ifAvailable = 0
this._updatesPending = 0
this._applyingReorg = null

const self = this
this._onstreamclose = onstreamclose

function onstreamclose () {
self.detachFrom(this.userData)
}
}

cork () {
Expand Down Expand Up @@ -1665,21 +1673,36 @@ module.exports = class Replicator {
attachTo (protomux, session) {
const makePeer = this._makePeer.bind(this, protomux, session)

this._attached.add(protomux)
protomux.pair({ protocol: 'hypercore/alpha', id: this.discoveryKey }, makePeer)
protomux.stream.setMaxListeners(0)
protomux.stream.on('close', this._onstreamclose)

this._ifAvailable++
protomux.stream.opened.then((opened) => {
this._ifAvailable--

if (opened) makePeer()
else if (session) session.close().catch(noop)
this._checkUpgradeIfAvailable()
})
}

detachFrom (protomux) {
if (this._attached.delete(protomux)) {
protomux.stream.removeListener('close', this._onstreamclose)
protomux.unpair({ protocol: 'hypercore/alpha', id: this.discoveryKey })
}
}

destroy () {
for (const peer of this.peers) {
peer.protomux.unpair({ protocol: 'hypercore/alpha', id: this.discoveryKey })
this.detachFrom(peer.protomux)
peer.channel.close()
}
for (const protomux of this._attached) {
this.detachFrom(protomux)
}
}

_makePeer (protomux, session) {
Expand Down

0 comments on commit ee3b7ca

Please sign in to comment.