Skip to content

Commit

Permalink
Merge pull request mage#14 from stelcheck/fixes/econnreset
Browse files Browse the repository at this point in the history
Better error management on debugger proxy conn.
  • Loading branch information
stelcheck authored Nov 28, 2017
2 parents 7a65cd7 + 73385ed commit aba01a5
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
}

Expand All @@ -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);

Expand All @@ -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;
Expand Down

0 comments on commit aba01a5

Please sign in to comment.