From 73385edcdf41dce2546332ab8bc8c947b242f8ce Mon Sep 17 00:00:00 2001 From: stelcheck Date: Mon, 27 Nov 2017 17:13:29 +0900 Subject: [PATCH] Better error management on debugger proxy conn. * Listen for errors on the debugger proxy connections * Destroy debugger proxy connections after end. --- index.js | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 2b5d1e3..a3bf874 100755 --- a/index.js +++ b/index.js @@ -266,18 +266,21 @@ try { // whenever a shutdown is detected var debuggerConnections = []; -function closeDebuggerConnection() { - debuggerConnections.forEach(function ({ localSocket, debuggerConnection }) { - debuggerConnection.unpipe(localSocket); - localSocket.unpipe(debuggerConnection); +function closeDebuggerConnection({ localSocket, debuggerConnection }) { + debuggerConnection.unpipe(localSocket); + localSocket.unpipe(debuggerConnection); - localSocket.unref(); - debuggerConnection.unref(); + localSocket.unref(); + debuggerConnection.unref(); - debuggerConnection.end(); - localSocket.end(); - }); + debuggerConnection.end(); + localSocket.end(); + debuggerConnection.destroy(); + localSocket.destroy(); +} +function closeDebuggerConnections() { + debuggerConnections.forEach(closeDebuggerConnection); debuggerConnections = []; } @@ -293,6 +296,16 @@ net.createServer(function (localSocket) { debuggerConnection.pipe(localSocket); localSocket.pipe(debuggerConnection); + debuggerConnection.on('error', function (error) { + logger.warning('Error raised on the connection to the worker debug port', error); + closeDebuggerConnection({ localSocket, debuggerConnection }); + }); + + localSocket.on('error', function (error) { + logger.warning('Error raised on the connection to the debugger proxy port', error); + closeDebuggerConnection({ localSocket, debuggerConnection }); + }); + debuggerConnections.push({ localSocket, debuggerConnection }); }).listen(debug.port); @@ -308,14 +321,14 @@ cluster.on('message', function (worker, message) { switch (message) { case 'reload': - closeDebuggerConnection(); + closeDebuggerConnections(); logger.notice('reloading worker'); mage.core.processManager.reload(function () { logger.notice('worker reloaded'); }); break; case 'shutdown': - closeDebuggerConnection(); + closeDebuggerConnections(); logger.notice('shutting down'); mage.quit(); break;