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_wrap: add uid argument to all asyncWrap hooks #4600

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/async-wrap-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,16 @@ inline AsyncWrap::AsyncWrap(Environment* env,
v8::HandleScope scope(env->isolate());

v8::Local<v8::Value> argv[] = {
v8::Int32::New(env->isolate(), provider),
v8::Integer::New(env->isolate(), get_uid()),
v8::Int32::New(env->isolate(), provider),
Null(env->isolate()),
Null(env->isolate())
};

if (parent != nullptr)
argv[2] = parent->object();
if (parent != nullptr) {
argv[2] = v8::Integer::New(env->isolate(), parent->get_uid());
argv[3] = parent->object();
}

v8::MaybeLocal<v8::Value> ret =
init_fn->Call(env->context(), object, ARRAY_SIZE(argv), argv);
Expand Down
5 changes: 3 additions & 2 deletions src/async-wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ Local<Value> AsyncWrap::MakeCallback(const Local<Function> cb,

Local<Function> pre_fn = env()->async_hooks_pre_function();
Local<Function> post_fn = env()->async_hooks_post_function();
Local<Value> uid = Integer::New(env()->isolate(), get_uid());
Local<Object> context = object();
Local<Object> process = env()->process_object();
Local<Object> domain;
Expand Down Expand Up @@ -207,14 +208,14 @@ Local<Value> AsyncWrap::MakeCallback(const Local<Function> cb,
}

if (ran_init_callback() && !pre_fn.IsEmpty()) {
if (pre_fn->Call(context, 0, nullptr).IsEmpty())
if (pre_fn->Call(context, 1, &uid).IsEmpty())
FatalError("node::AsyncWrap::MakeCallback", "pre hook threw");
}

Local<Value> ret = cb->Call(context, argc, argv);

if (ran_init_callback() && !post_fn.IsEmpty()) {
if (post_fn->Call(context, 0, nullptr).IsEmpty())
if (post_fn->Call(context, 1, &uid).IsEmpty())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the meaning of the number 1 here? Also, will the uid be enough during pre or post calls?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It specifies the length of the arguments array (&uid) that follows.

The general idea is that the user will collect all the required information in the init hook and map it to a Map object using the uid. Thus the uid should be all that the user needs in the pre, post and destroy hook.

FatalError("node::AsyncWrap::MakeCallback", "post hook threw");
}

Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-async-wrap-check-providers.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ let keyList = pkeys.slice();
keyList.splice(0, 1);


function init(id) {
keyList = keyList.filter((e) => e != pkeys[id]);
function init(id, provider) {
keyList = keyList.filter((e) => e != pkeys[provider]);
}

function noop() { }
Expand Down
13 changes: 10 additions & 3 deletions test/parallel/test-async-wrap-disabled-propagate-parent.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,24 @@ const net = require('net');
const async_wrap = process.binding('async_wrap');
const providers = Object.keys(async_wrap.Providers);

const uidSymbol = Symbol('uid');

let cntr = 0;
let server;
let client;

function init(type, id, parent) {
if (parent) {
function init(uid, type, parentUid, parentHandle) {
this[uidSymbol] = uid;

if (parentHandle) {
cntr++;
// Cannot assert in init callback or will abort.
process.nextTick(() => {
assert.equal(providers[type], 'TCPWRAP');
assert.equal(parent, server._handle, 'server doesn\'t match parent');
assert.equal(parentUid, server._handle[uidSymbol],
'server uid doesn\'t match parent uid');
assert.equal(parentHandle, server._handle,
'server handle doesn\'t match parent handle');
assert.equal(this, client._handle, 'client doesn\'t match context');
});
}
Expand Down
15 changes: 12 additions & 3 deletions test/parallel/test-async-wrap-propagate-parent.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,26 @@ const common = require('../common');
const assert = require('assert');
const net = require('net');
const async_wrap = process.binding('async_wrap');
const providers = Object.keys(async_wrap.Providers);

const uidSymbol = Symbol('uid');

let cntr = 0;
let server;
let client;

function init(type, id, parent) {
if (parent) {
function init(uid, type, parentUid, parentHandle) {
this[uidSymbol] = uid;

if (parentHandle) {
cntr++;
// Cannot assert in init callback or will abort.
process.nextTick(() => {
assert.equal(parent, server._handle, 'server doesn\'t match parent');
assert.equal(providers[type], 'TCPWRAP');
assert.equal(parentUid, server._handle[uidSymbol],
'server uid doesn\'t match parent uid');
assert.equal(parentHandle, server._handle,
'server handle doesn\'t match parent handle');
assert.equal(this, client._handle, 'client doesn\'t match context');
});
}
Expand Down
57 changes: 57 additions & 0 deletions test/parallel/test-async-wrap-uid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use strict';

require('../common');
const fs = require('fs');
const assert = require('assert');
const async_wrap = process.binding('async_wrap');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In JavaScript code we follow camelCase style.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


const storage = new Map();
async_wrap.setupHooks(init, pre, post, destroy);
async_wrap.enable();

function init(uid) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about asserting if we got all four parameters?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test/parallel/test-async-wrap-propagate-parent.js and test/parallel/test-async-wrap-disabled-propagate-parent.js tests that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool then 👍

storage.set(uid, {
init: true,
pre: false,
post: false,
destroy: false
});
}

function pre(uid) {
storage.get(uid).pre = true;
}

function post(uid) {
storage.get(uid).post = true;
}

function destroy(uid) {
storage.get(uid).destroy = true;
}

fs.access(__filename, function(err) {
assert.ifError(err);
});

fs.access(__filename, function(err) {
assert.ifError(err);
});

async_wrap.disable();

process.once('exit', function() {
assert.strictEqual(storage.size, 2);

for (const item of storage) {
const uid = item[0];
const value = item[1];
assert.strictEqual(typeof uid, 'number');
assert.deepStrictEqual(value, {
init: true,
pre: true,
post: true,
destroy: true
});
}
});