From 86db568cd875dabbdc6b1215cc17ffbef8afe7b4 Mon Sep 17 00:00:00 2001 From: Jacob Heun Date: Fri, 14 Feb 2020 14:12:30 +0100 Subject: [PATCH] fix: catch thrown maConn errors in listener (#122) When upgrading sockets to MultiaddConnections, it's possible for an error to be thrown. This can crash the application if a client disconnects prior to the listener uprading the socket, as is likely occurring in #121. Errors will now be caught and logged when attempting to upgrade the socket. --- src/listener.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/listener.js b/src/listener.js index 10b57a2..3c2f146 100644 --- a/src/listener.js +++ b/src/listener.js @@ -20,15 +20,15 @@ module.exports = ({ handler, upgrader }, options) => { // Avoid uncaught errors caused by unstable connections socket.on('error', err => log('socket error', err)) - const maConn = toConnection(socket, { listeningAddr }) - log('new inbound connection %s', maConn.remoteAddr) - + let maConn let conn try { + maConn = toConnection(socket, { listeningAddr }) + log('new inbound connection %s', maConn.remoteAddr) conn = await upgrader.upgradeInbound(maConn) } catch (err) { - log.error('inbound connection failed to upgrade', err) - return maConn.close() + log.error('inbound connection failed', err) + return maConn && maConn.close() } log('inbound connection %s upgraded', maConn.remoteAddr)