Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
fix: catch error from maConn.close (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobheun authored Apr 28, 2020
1 parent 2bc2b36 commit 0fe0815
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ const {
multiaddrToNetConfig
} = require('./utils')

/**
* Attempts to close the given maConn. If a failure occurs, it will be logged.
* @private
* @param {MultiaddrConnection} maConn
*/
async function attemptClose (maConn) {
try {
maConn && await maConn.close()
} catch (err) {
log.error('an error occurred closing the connection', err)
}
}

module.exports = ({ handler, upgrader }, options) => {
const listener = new EventEmitter()

Expand All @@ -28,7 +41,7 @@ module.exports = ({ handler, upgrader }, options) => {
conn = await upgrader.upgradeInbound(maConn)
} catch (err) {
log.error('inbound connection failed', err)
return maConn && maConn.close()
return attemptClose(maConn)
}

log('inbound connection %s upgraded', maConn.remoteAddr)
Expand All @@ -51,7 +64,7 @@ module.exports = ({ handler, upgrader }, options) => {
if (!server.listening) return

return new Promise((resolve, reject) => {
server.__connections.forEach(maConn => maConn.close())
server.__connections.forEach(maConn => attemptClose(maConn))
server.close(err => err ? reject(err) : resolve())
})
}
Expand Down

0 comments on commit 0fe0815

Please sign in to comment.