From f0ef597e094f2519486e080897956389584876e4 Mon Sep 17 00:00:00 2001 From: Jackson Tian Date: Thu, 26 Mar 2015 22:06:21 +0800 Subject: [PATCH] debugger: don't spawn child process in remote mode When debug in remote mode with host:port or pid, the interface spawn child process also. If the debugger agent is running, will get following output: ``` < Error: listen EADDRINUSE :::5858 < at Object.exports._errnoException (util.js:734:11) < at exports._exceptionWithHostPort (util.js:757:20) < at Agent.Server._listen2 (net.js:1155:14) < at listen (net.js:1181:10) < at Agent.Server.listen (net.js:1268:5) < at Object.start (_debug_agent.js:21:9) < at startup (node.js:68:9) < at node.js:799:3 ``` This fix won't spawn child process and no more error message was shown. Reviewed-By: Julien Gilli PR-URL: https://github.com/joyent/node/pull/14172 --- lib/_debugger.js | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/lib/_debugger.js b/lib/_debugger.js index c4155ce4c753..6f7a80b77a82 100644 --- a/lib/_debugger.js +++ b/lib/_debugger.js @@ -1627,6 +1627,7 @@ Interface.prototype.trySpawn = function(cb) { this.killChild(); assert(!this.child); + var isRemote = false; if (this.args.length === 2) { var match = this.args[1].match(/^([^:]+):(\d+)$/); @@ -1635,21 +1636,13 @@ Interface.prototype.trySpawn = function(cb) { // `node debug localhost:5858` host = match[1]; port = parseInt(match[2], 10); - this.child = { - kill: function() { - // TODO Do we really need to handle it? - } - }; + isRemote = true; } } else if (this.args.length === 3) { // `node debug -p pid` if (this.args[1] === '-p' && /^\d+$/.test(this.args[2])) { - this.child = { - kill: function() { - // TODO Do we really need to handle it? - } - }; process._debugProcess(parseInt(this.args[2], 10)); + isRemote = true; } else { var match = this.args[1].match(/^--port=(\d+)$/); if (match) { @@ -1661,10 +1654,13 @@ Interface.prototype.trySpawn = function(cb) { } } - this.child = spawn(process.execPath, childArgs); + if (!isRemote) { + // pipe stream into debugger + this.child = spawn(process.execPath, childArgs); - this.child.stdout.on('data', this.childPrint.bind(this)); - this.child.stderr.on('data', this.childPrint.bind(this)); + this.child.stdout.on('data', this.childPrint.bind(this)); + this.child.stderr.on('data', this.childPrint.bind(this)); + } this.pause(); @@ -1708,9 +1704,10 @@ Interface.prototype.trySpawn = function(cb) { client.on('error', connectError); function connectError() { - // If it's failed to connect 4 times then don't catch the next error + // If it's failed to connect 10 times then print failed message if (connectionAttempts >= 10) { - client.removeListener('error', connectError); + self.stdout.write(' failed, please retry\n'); + return; } setTimeout(attemptConnect, 500); } @@ -1721,10 +1718,12 @@ Interface.prototype.trySpawn = function(cb) { client.connect(port, host); } - this.child.stderr.once('data', function() { - setImmediate(function() { - self.print('connecting to port ' + port + '..', true); - attemptConnect(); + self.print('connecting to ' + host + ':' + port + ' ..', true); + if (isRemote) { + attemptConnect(); + } else { + this.child.stderr.once('data', function() { + setImmediate(attemptConnect); }); - }); + } };