Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

async_hooks: C++ Embedder API overhaul #14040

Closed
wants to merge 9 commits into from
Next Next commit
async_hooks: C++ Embedder API overhaul
* Fix AsyncHooksGetTriggerAsyncId such it corresponds to
async_hooks.triggerAsyncId and not async_hooks.initTriggerId.
* Use an async_context struct instead of two async_uid values.
  This change was necessary since the fixing
  AsyncHooksGetTriggerAsyncId otherwise makes it impossible to
  get the correct default trigger id. It also prevents an invalid
  triggerAsyncId in MakeCallback.
* Rename async_uid to async_id for consistency

async_hooks: fix AsyncHooksGetTriggerAsyncId
AndreasMadsen committed Jul 5, 2017
commit 4af3a333ed92f650677a83661cb3e1161fb37c9f
2 changes: 1 addition & 1 deletion src/async-wrap.cc
Original file line number Diff line number Diff line change
@@ -751,7 +751,7 @@ async_uid AsyncHooksGetCurrentId(Isolate* isolate) {


async_uid AsyncHooksGetTriggerAsyncId(Isolate* isolate) {
return Environment::GetCurrent(isolate)->get_init_trigger_id();
return Environment::GetCurrent(isolate)->trigger_id();
}

async_uid AsyncHooksGetTriggerId(Isolate* isolate) {
2 changes: 1 addition & 1 deletion src/node.h
Original file line number Diff line number Diff line change
@@ -604,7 +604,7 @@ class AsyncResource {
resource_(isolate, resource),
trigger_async_id_(trigger_async_id) {
if (trigger_async_id_ == -1)
trigger_async_id_ = AsyncHooksGetTriggerAsyncId(isolate);
trigger_async_id_ = Environment::GetCurrent(isolate)->get_init_trigger_id();

uid_ = EmitAsyncInit(isolate, resource, name, trigger_async_id_);
}
27 changes: 27 additions & 0 deletions test/addons/async-hooks-id/binding.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "node.h"

namespace {

using v8::FunctionCallbackInfo;
using v8::Local;
using v8::Object;
using v8::Value;

void GetExecutionAsyncId(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(
node::AsyncHooksGetExecutionAsyncId(args.GetIsolate()));
}

void GetTriggerAsyncId(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(
node::AsyncHooksGetTriggerAsyncId(args.GetIsolate()));
}

void Initialize(Local<Object> exports) {
NODE_SET_METHOD(exports, "getExecutionAsyncId", GetExecutionAsyncId);
NODE_SET_METHOD(exports, "getTriggerAsyncId", GetTriggerAsyncId);
}

} // namespace

NODE_MODULE(binding, Initialize)
9 changes: 9 additions & 0 deletions test/addons/async-hooks-id/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
'targets': [
{
'target_name': 'binding',
'defines': [ 'V8_DEPRECATION_WARNINGS=1' ],
'sources': [ 'binding.cc' ]
}
]
}
26 changes: 26 additions & 0 deletions test/addons/async-hooks-id/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

const common = require('../../common');
const assert = require('assert');
const binding = require(`./build/${common.buildType}/binding`);
const async_hooks = require('async_hooks');

assert.strictEqual(
binding.getExecutionAsyncId(),
async_hooks.executionAsyncId()
);
assert.strictEqual(
binding.getTriggerAsyncId(),
async_hooks.triggerAsyncId()
);

process.nextTick(common.mustCall(function() {
assert.strictEqual(
binding.getExecutionAsyncId(),
async_hooks.executionAsyncId()
);
assert.strictEqual(
binding.getTriggerAsyncId(),
async_hooks.triggerAsyncId()
);
}));
6 changes: 0 additions & 6 deletions test/addons/async-resource/binding.cc
Original file line number Diff line number Diff line change
@@ -92,11 +92,6 @@ void GetResource(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(r->get_resource());
}

void GetCurrentId(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(
node::AsyncHooksGetExecutionAsyncId(args.GetIsolate()));
}

void Initialize(Local<Object> exports) {
NODE_SET_METHOD(exports, "createAsyncResource", CreateAsyncResource);
NODE_SET_METHOD(exports, "destroyAsyncResource", DestroyAsyncResource);
@@ -105,7 +100,6 @@ void Initialize(Local<Object> exports) {
NODE_SET_METHOD(exports, "callViaUtf8Name", CallViaUtf8Name);
NODE_SET_METHOD(exports, "getUid", GetUid);
NODE_SET_METHOD(exports, "getResource", GetResource);
NODE_SET_METHOD(exports, "getCurrentId", GetCurrentId);
}

} // namespace
7 changes: 3 additions & 4 deletions test/addons/async-resource/test.js
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ const binding = require(`./build/${common.buildType}/binding`);
const async_hooks = require('async_hooks');

const kObjectTag = Symbol('kObjectTag');
const rootAsyncId = async_hooks.executionAsyncId();

const bindingUids = [];
let expectedTriggerId;
@@ -38,8 +39,6 @@ async_hooks.createHook({
}
}).enable();

assert.strictEqual(binding.getCurrentId(), 1);

for (const call of [binding.callViaFunction,
binding.callViaString,
binding.callViaUtf8Name]) {
@@ -49,14 +48,14 @@ for (const call of [binding.callViaFunction,
methöd(arg) {
assert.strictEqual(this, object);
assert.strictEqual(arg, 42);
assert.strictEqual(binding.getCurrentId(), uid);
assert.strictEqual(async_hooks.executionAsyncId(), uid);
return 'baz';
},
kObjectTag
};

if (passedTriggerId === undefined)
expectedTriggerId = 1;
expectedTriggerId = rootAsyncId;
else
expectedTriggerId = passedTriggerId;