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

Commit 0fe0815

Browse files
authored
fix: catch error from maConn.close (#128)
1 parent 2bc2b36 commit 0fe0815

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/listener.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ const {
1313
multiaddrToNetConfig
1414
} = require('./utils')
1515

16+
/**
17+
* Attempts to close the given maConn. If a failure occurs, it will be logged.
18+
* @private
19+
* @param {MultiaddrConnection} maConn
20+
*/
21+
async function attemptClose (maConn) {
22+
try {
23+
maConn && await maConn.close()
24+
} catch (err) {
25+
log.error('an error occurred closing the connection', err)
26+
}
27+
}
28+
1629
module.exports = ({ handler, upgrader }, options) => {
1730
const listener = new EventEmitter()
1831

@@ -28,7 +41,7 @@ module.exports = ({ handler, upgrader }, options) => {
2841
conn = await upgrader.upgradeInbound(maConn)
2942
} catch (err) {
3043
log.error('inbound connection failed', err)
31-
return maConn && maConn.close()
44+
return attemptClose(maConn)
3245
}
3346

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

5366
return new Promise((resolve, reject) => {
54-
server.__connections.forEach(maConn => maConn.close())
67+
server.__connections.forEach(maConn => attemptClose(maConn))
5568
server.close(err => err ? reject(err) : resolve())
5669
})
5770
}

0 commit comments

Comments
 (0)