Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

debugger: don't spawn child process in remote mode #14172

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 21 additions & 20 deletions lib/_debugger.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ exports.start = function(argv, stdin, stdout) {

if (argv.length < 1) {
console.error('Usage: node debug script.js');
console.error(' node debug <host>:<port>');
console.error(' node debug -p <pid>');
process.exit(1);
}

Expand Down Expand Up @@ -1625,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 @@ -1633,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 @@ -1659,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 @@ -1706,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 @@ -1719,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);
});
});
}
};