diff --git a/src/dial.js b/src/dial.js index 450265c..00070ca 100644 --- a/src/dial.js +++ b/src/dial.js @@ -212,8 +212,9 @@ class Dialer { } const muxedConn = this.switch.muxers[key].dialer(conn) - this.switch.muxedConns[b58Id] = {} - this.switch.muxedConns[b58Id].muxer = muxedConn + this.switch.muxedConns[b58Id] = { + muxer: muxedConn + } muxedConn.once('close', () => { delete this.switch.muxedConns[b58Id] diff --git a/src/index.js b/src/index.js index 0ccea35..825d97b 100644 --- a/src/index.js +++ b/src/index.js @@ -110,6 +110,11 @@ class Switch extends EE { this.stats.stop() series([ (cb) => each(this.muxedConns, (conn, cb) => { + // If the connection was destroyed while we are hanging up, continue + if (conn === undefined) { + return cb() + } + conn.muxer.end((err) => { // If OK things are fine, and someone just shut down if (err && err.message !== 'Fatal error: OK') {