Skip to content

Instantly share code, notes, and snippets.

@ry
Created December 22, 2009 16:53
Show Gist options
  • Save ry/261855 to your computer and use it in GitHub Desktop.
Save ry/261855 to your computer and use it in GitHub Desktop.
diff --git a/src/node.js b/src/node.js
index 69416d0..4ba4432 100644
--- a/src/node.js
+++ b/src/node.js
@@ -71,6 +71,8 @@ node.inherits = function () {
};
+var childProcesses = [];
+
process.createChildProcess = function (file, args, env) {
var child = new process.ChildProcess();
args = args || [];
@@ -85,9 +87,27 @@ process.createChildProcess = function (file, args, env) {
// needs to be searched for the 'file' command if 'file' does not contain
// a '/' character.
child.spawn(file, args, envPairs);
+ var pid = child.pid;
+ childProcesses.push(child);
+ child.addListener("exit", function () {
+ for (var i = 0; i < childProcesses.length; i++) {
+ if (childProcesses[i].pid == pid) {
+ childProcesses.splice(i, 1);
+ return;
+ }
+ }
+ });
return child;
};
+// Reap child processes on SIGINT.
+var sigint = new process.SignalHandler(process['SIGINT']);
+sigint.addListener("signal", function () {
+ for (var i = 0; i < childProcesses.length; i++) {
+ child.kill();
+ }
+});
+
process.assert = function (x, msg) {
if (!(x)) throw new Error(msg || "assertion error");
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment