Fixes #1726, hack to unref process.stdout
authorRyan Dahl <ry@tinyclouds.org>
Tue, 27 Sep 2011 19:38:23 +0000 (12:38 -0700)
committerRyan Dahl <ry@tinyclouds.org>
Tue, 27 Sep 2011 19:41:59 +0000 (12:41 -0700)
src/node.js
src/node_stdio.cc

index 6b67d3406dea3f9de979a438fc9f01bea26244d8..761f53609def4e2049edefcc5348daead6eb924b 100644 (file)
       // Note stdout._type is used for test-module-load-list.js
 
       if (binding.isatty(fd)) {
-        binding.unref();
         var tty = NativeModule.require('tty');
         stdout = new tty.WriteStream(fd);
         stdout._type = "tty";
+
+        // FIXME Hack to have stdout not keep the event loop alive.
+        // See https://github.com/joyent/node/issues/1726
+        binding.unref();
+        stdout.on('close', function() {
+          binding.ref();
+        });
       } else if (binding.isStdoutBlocking()) {
         var fs = NativeModule.require('fs');
         stdout = new fs.WriteStream(null, {fd: fd});
         stdout._type = "fs";
       } else {
-        binding.unref();
-
         var net = NativeModule.require('net');
         stdout = new net.Stream(fd);
 
+        // FIXME Hack to have stdout not keep the event loop alive.
+        // See https://github.com/joyent/node/issues/1726
+        binding.unref();
+        stdout.on('close', function() {
+          binding.ref();
+        });
+
         // FIXME Should probably have an option in net.Stream to create a
         // stream from an existing fd which is writable only. But for now
         // we'll just add this hack and set the `readable` member to false.
index 41a63cd699574c7cd965b3e9a1944957d2cad0aa..78f3afbf0b7c2c7b6d911e949b76b1cf078ef358 100644 (file)
@@ -209,6 +209,17 @@ static Handle<Value> Unref(const Arguments& args) {
 }
 
 
+static Handle<Value> Ref(const Arguments& args) {
+  HandleScope scope;
+
+  assert(unref_called == true);
+
+  uv_ref(uv_default_loop());
+
+  return Null();
+}
+
+
 static Handle<Value> OpenStdin(const Arguments& args) {
   HandleScope scope;
 
@@ -337,6 +348,7 @@ void Stdio::Initialize(v8::Handle<v8::Object> target) {
   NODE_SET_METHOD(target, "openpty", OpenPTY);
 
   NODE_SET_METHOD(target, "unref", Unref);
+  NODE_SET_METHOD(target, "ref", Ref);
 
   struct sigaction sa;
   memset(&sa, 0, sizeof(sa));