node,async-wrap: verify domain enter/exit are set
authorTrevor Norris <trev.norris@gmail.com>
Tue, 2 Sep 2014 18:30:45 +0000 (11:30 -0700)
committerTrevor Norris <trev.norris@gmail.com>
Tue, 2 Sep 2014 18:30:45 +0000 (11:30 -0700)
The REPL global object lazy loads modules by placing getters for each.
This causes MakeDomainCallback() to be run if a native module is loaded
from the REPL, but if the domain module hasn't been loaded then there
are no enter/exit callbacks to be called. Causing an assert() to fail.

Fix the issue by conditionally running the callback instead of asserting
it is available. Also add "addon" test to verify the fix.

Fixes: #8231
Signed-off-by: Trevor Norris <trev.norris@gmail.com>
src/async-wrap-inl.h
src/node.cc
test/addons/repl-domain-abort/binding.cc [new file with mode: 0644]
test/addons/repl-domain-abort/binding.gyp [new file with mode: 0644]
test/addons/repl-domain-abort/test.js [new file with mode: 0644]

index 388ee19..324c57b 100644 (file)
@@ -101,11 +101,11 @@ inline v8::Handle<v8::Value> AsyncWrap::MakeDomainCallback(
 
     v8::Local<v8::Function> enter =
       domain->Get(env()->enter_string()).As<v8::Function>();
-    assert(enter->IsFunction());
-    enter->Call(domain, 0, NULL);
-
-    if (try_catch.HasCaught())
-      return Undefined(env()->isolate());
+    if (enter->IsFunction()) {
+      enter->Call(domain, 0, NULL);
+      if (try_catch.HasCaught())
+        return Undefined(env()->isolate());
+    }
   }
 
   v8::Local<v8::Value> ret = cb->Call(context, argc, argv);
@@ -117,11 +117,11 @@ inline v8::Handle<v8::Value> AsyncWrap::MakeDomainCallback(
   if (has_domain) {
     v8::Local<v8::Function> exit =
       domain->Get(env()->exit_string()).As<v8::Function>();
-    assert(exit->IsFunction());
-    exit->Call(domain, 0, NULL);
-
-    if (try_catch.HasCaught())
-      return Undefined(env()->isolate());
+    if (exit->IsFunction()) {
+      exit->Call(domain, 0, NULL);
+      if (try_catch.HasCaught())
+        return Undefined(env()->isolate());
+    }
   }
 
   if (has_async_listener()) {
index fe97888..d2f9f8b 100644 (file)
@@ -1032,13 +1032,11 @@ Handle<Value> MakeDomainCallback(Environment* env,
         return Undefined(env->isolate());
       }
 
-      Local<Function> enter =
-          domain->Get(env->enter_string()).As<Function>();
-      assert(enter->IsFunction());
-      enter->Call(domain, 0, NULL);
-
-      if (try_catch.HasCaught()) {
-        return Undefined(env->isolate());
+      Local<Function> enter = domain->Get(env->enter_string()).As<Function>();
+      if (enter->IsFunction()) {
+        enter->Call(domain, 0, NULL);
+        if (try_catch.HasCaught())
+          return Undefined(env->isolate());
       }
     }
   }
@@ -1050,13 +1048,11 @@ Handle<Value> MakeDomainCallback(Environment* env,
   }
 
   if (has_domain) {
-    Local<Function> exit =
-        domain->Get(env->exit_string()).As<Function>();
-    assert(exit->IsFunction());
-    exit->Call(domain, 0, NULL);
-
-    if (try_catch.HasCaught()) {
-      return Undefined(env->isolate());
+    Local<Function> exit = domain->Get(env->exit_string()).As<Function>();
+    if (exit->IsFunction()) {
+      exit->Call(domain, 0, NULL);
+      if (try_catch.HasCaught())
+        return Undefined(env->isolate());
     }
   }
 
diff --git a/test/addons/repl-domain-abort/binding.cc b/test/addons/repl-domain-abort/binding.cc
new file mode 100644 (file)
index 0000000..1337395
--- /dev/null
@@ -0,0 +1,47 @@
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+#include <node.h>
+#include <v8.h>
+
+using v8::Function;
+using v8::FunctionCallbackInfo;
+using v8::Handle;
+using v8::HandleScope;
+using v8::Isolate;
+using v8::Object;
+using v8::Value;
+
+void Method(const FunctionCallbackInfo<Value>& args) {
+  Isolate* isolate = args.GetIsolate();
+  HandleScope scope(isolate);
+  node::MakeCallback(isolate,
+                     isolate->GetCurrentContext()->Global(),
+                     args[0].As<Function>(),
+                     0,
+                     NULL);
+}
+
+void init(Handle<Object> target) {
+  NODE_SET_METHOD(target, "method", Method);
+}
+
+NODE_MODULE(binding, init);
diff --git a/test/addons/repl-domain-abort/binding.gyp b/test/addons/repl-domain-abort/binding.gyp
new file mode 100644 (file)
index 0000000..3bfb844
--- /dev/null
@@ -0,0 +1,8 @@
+{
+  'targets': [
+    {
+      'target_name': 'binding',
+      'sources': [ 'binding.cc' ]
+    }
+  ]
+}
diff --git a/test/addons/repl-domain-abort/test.js b/test/addons/repl-domain-abort/test.js
new file mode 100644 (file)
index 0000000..c35faaf
--- /dev/null
@@ -0,0 +1,64 @@
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+var assert = require('assert');
+var repl = require('repl');
+var stream = require('stream');
+var buildType = process.config.target_defaults.default_configuration;
+var buildPath = __dirname + '/build/' + buildType + '/binding';
+var cb_ran = false;
+
+process.on('exit', function() {
+  assert(cb_ran);
+  console.log('ok');
+});
+
+var lines = [
+  // This line shouldn't cause an assertion error.
+  'require(\'' + buildPath + '\')' +
+  // Log output to double check callback ran.
+  '.method(function() { console.log(\'cb_ran\'); });',
+];
+
+var dInput = new stream.Readable();
+var dOutput = new stream.Writable();
+
+dInput._read = function _read(size) {
+  while (lines.length > 0 && this.push(lines.shift()));
+  if (lines.length === 0)
+    this.push(null);
+};
+
+dOutput._write = function _write(chunk, encoding, cb) {
+  if (chunk.toString().indexOf('cb_ran') === 0)
+    cb_ran = true;
+  cb();
+};
+
+var options = {
+  input: dInput,
+  output: dOutput,
+  terminal: false,
+  ignoreUndefined: true
+};
+
+// Run commands from fake REPL.
+var dummy = repl.start(options);