Skip to content

Commit

Permalink
[api] Stop the drone when port isn't open after some timeout
Browse files Browse the repository at this point in the history
Failing to do so causes zombie processes to be created. Haibu instance
loses tracks of processes which were started but failed to listen on a
socket and doesn't kill them.
  • Loading branch information
mmalecki committed Jul 30, 2012
1 parent c914cce commit 5445c1a
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/haibu/core/spawner.js
Expand Up @@ -163,6 +163,7 @@ Spawner.prototype.spawn = function spawn (repo, callback) {
options = ['--plugin', 'net'],
foreverOptions,
carapaceBin,
timeout,
error,
drone;

Expand Down Expand Up @@ -293,6 +294,7 @@ Spawner.prototype.spawn = function spawn (repo, callback) {
//
drone.removeListener('exit', onExit);
drone.removeListener('message', onCarapacePort);
clearTimeout(timeout);
}
}

Expand Down Expand Up @@ -322,6 +324,7 @@ Spawner.prototype.spawn = function spawn (repo, callback) {
//
drone.removeListener('exit', onExit);
drone.removeListener('error', onError);
clearTimeout(timeout);
}
}

Expand Down Expand Up @@ -380,9 +383,25 @@ Spawner.prototype.spawn = function spawn (repo, callback) {
//
drone.removeListener('error', onError);
drone.removeListener('message', onCarapacePort);
clearTimeout(timeout);
}
}

function onTimeout() {
drone.removeListener('exit', onExit);

drone.stop();
error = new Error('Error spawning drone');
error.blame = {
type: 'user',
message: 'Script took too long to listen on a socket'
};
error.stdout = stdout.join('\n');
error.stderr = stderr.join('\n');

callback(error);
}

//
// Listen to the appropriate events and start the drone process.
//
Expand All @@ -393,6 +412,9 @@ Spawner.prototype.spawn = function spawn (repo, callback) {
drone.once('start', onChildStart);
drone.on('restart', onChildRestart);
drone.on('message', onCarapacePort);

timeout = setTimeout(onTimeout, haibu.config.get('portTimeout') || 5000);

drone.start();
});
};
Expand Down

0 comments on commit 5445c1a

Please sign in to comment.