Skip to content

Commit

Permalink
node,async-wrap: verify domain enter/exit are set
Browse files Browse the repository at this point in the history
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: nodejs#8231
Signed-off-by: Trevor Norris <[email protected]>
  • Loading branch information
trevnorris committed Sep 2, 2014
1 parent cdc01fa commit 81a9739
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 24 deletions.
20 changes: 10 additions & 10 deletions src/async-wrap-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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()) {
Expand Down
24 changes: 10 additions & 14 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
Expand All @@ -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());
}
}

Expand Down
47 changes: 47 additions & 0 deletions test/addons/repl-domain-abort/binding.cc
Original file line number Diff line number Diff line change
@@ -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);
8 changes: 8 additions & 0 deletions test/addons/repl-domain-abort/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
'targets': [
{
'target_name': 'binding',
'sources': [ 'binding.cc' ]
}
]
}
64 changes: 64 additions & 0 deletions test/addons/repl-domain-abort/test.js
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 81a9739

Please sign in to comment.