Skip to content

Commit

Permalink
debugger: don't spawn child process in remote mode
Browse files Browse the repository at this point in the history
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 <julien.gilli@joyent.com>
PR-URL: nodejs/node-v0.x-archive#14172
  • Loading branch information
JacksonTian authored and Julien Gilli committed Apr 14, 2015
1 parent c63a39b commit f0ef597
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions lib/_debugger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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+)$/);

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

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

0 comments on commit f0ef597

Please sign in to comment.