Skip to content

Commit

Permalink
src: set PromiseHooks by Environment
Browse files Browse the repository at this point in the history
The new JS PromiseHooks introduced in the referenced PR are per
v8::Context. This meant that code depending on them, such as
AsyncLocalStorage, wouldn't behave correctly across vm.Context
instances.

PromiseHooks are now synchronized across the main Context and any
Context created via vm.Context.

Refs: nodejs#36394
Fixes: nodejs#38781
  • Loading branch information
bengl committed May 26, 2021
1 parent 2a1dea8 commit 52b1733
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 7 deletions.
23 changes: 17 additions & 6 deletions src/async_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -454,12 +454,23 @@ static void EnablePromiseHook(const FunctionCallbackInfo<Value>& args) {

static void SetPromiseHooks(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Local<Context> ctx = env->context();
ctx->SetPromiseHooks(
args[0]->IsFunction() ? args[0].As<Function>() : Local<Function>(),
args[1]->IsFunction() ? args[1].As<Function>() : Local<Function>(),
args[2]->IsFunction() ? args[2].As<Function>() : Local<Function>(),
args[3]->IsFunction() ? args[3].As<Function>() : Local<Function>());

Local<Function> hook0 = args[0]->IsFunction() ?
args[0].As<Function>() :
Local<Function>();
Local<Function> hook1 = args[1]->IsFunction() ?
args[1].As<Function>() :
Local<Function>();
Local<Function> hook2 = args[2]->IsFunction() ?
args[2].As<Function>() :
Local<Function>();
Local<Function> hook3 = args[3]->IsFunction() ?
args[3].As<Function>() :
Local<Function>();

env->async_hooks()->SetJSPromiseHooks(hook0, hook1, hook2, hook3);

env->context()->SetPromiseHooks(hook0, hook1, hook2, hook3);
}

static void DisablePromiseHook(const FunctionCallbackInfo<Value>& args) {
Expand Down
46 changes: 46 additions & 0 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,21 @@ v8::Local<v8::Object> AsyncHooks::native_execution_async_resource(size_t i) {
return PersistentToLocal::Strong(native_execution_async_resources_[i]);
}

inline void AsyncHooks::SetJSPromiseHooks(
v8::Local<v8::Function> fn0,
v8::Local<v8::Function> fn1,
v8::Local<v8::Function> fn2,
v8::Local<v8::Function> fn3) {
js_promise_hooks_[0].Reset(env()->isolate(), fn0);
js_promise_hooks_[1].Reset(env()->isolate(), fn1);
js_promise_hooks_[2].Reset(env()->isolate(), fn2);
js_promise_hooks_[3].Reset(env()->isolate(), fn3);
for (size_t i = 0; i < contexts_.size(); i++) {
PersistentToLocal::Strong(contexts_[i])
->SetPromiseHooks(fn0, fn1, fn2, fn3);
}
}

inline v8::Local<v8::String> AsyncHooks::provider_string(int idx) {
return env()->isolate_data()->async_wrap_provider(idx);
}
Expand Down Expand Up @@ -217,6 +232,37 @@ void AsyncHooks::clear_async_id_stack() {
fields_[kStackLength] = 0;
}

inline void AsyncHooks::AddContext(v8::Local<v8::Context> ctx) {
ctx->SetPromiseHooks(
js_promise_hooks_[0].IsEmpty() ?
v8::Local<v8::Function>() :
PersistentToLocal::Strong(js_promise_hooks_[0]),
js_promise_hooks_[1].IsEmpty() ?
v8::Local<v8::Function>() :
PersistentToLocal::Strong(js_promise_hooks_[1]),
js_promise_hooks_[2].IsEmpty() ?
v8::Local<v8::Function>() :
PersistentToLocal::Strong(js_promise_hooks_[2]),
js_promise_hooks_[3].IsEmpty() ?
v8::Local<v8::Function>() :
PersistentToLocal::Strong(js_promise_hooks_[3]));

size_t id = contexts_.size();
contexts_.resize(id + 1);
contexts_[id].Reset(env()->isolate(), ctx);
}

inline void AsyncHooks::RemoveContext(v8::Local<v8::Context> ctx) {
for (auto it = contexts_.begin(); it != contexts_.end(); it++) {
v8::Local<v8::Context> saved_context = PersistentToLocal::Strong(*it);
if (saved_context == ctx) {
it->Reset();
contexts_.erase(it);
break;
}
}
}

// The DefaultTriggerAsyncIdScope(AsyncWrap*) constructor is defined in
// async_wrap-inl.h to avoid a circular dependency.

Expand Down
14 changes: 14 additions & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,12 @@ class AsyncHooks : public MemoryRetainer {
// The `js_execution_async_resources` array contains the value in that case.
inline v8::Local<v8::Object> native_execution_async_resource(size_t index);

inline void SetJSPromiseHooks(
v8::Local<v8::Function> fn0,
v8::Local<v8::Function> fn1,
v8::Local<v8::Function> fn2,
v8::Local<v8::Function> fn3);

inline v8::Local<v8::String> provider_string(int idx);

inline void no_force_checks();
Expand All @@ -711,6 +717,10 @@ class AsyncHooks : public MemoryRetainer {
inline bool pop_async_context(double async_id);
inline void clear_async_id_stack(); // Used in fatal exceptions.

inline void AddContext(v8::Local<v8::Context> ctx);
inline void RemoveContext(v8::Local<v8::Context> ctx);


AsyncHooks(const AsyncHooks&) = delete;
AsyncHooks& operator=(const AsyncHooks&) = delete;
AsyncHooks(AsyncHooks&&) = delete;
Expand Down Expand Up @@ -770,6 +780,10 @@ class AsyncHooks : public MemoryRetainer {

// Non-empty during deserialization
const SerializeInfo* info_ = nullptr;

std::vector<v8::Global<v8::Context>> contexts_;

v8::Global<v8::Function> js_promise_hooks_[4];
};

class ImmediateInfo : public MemoryRetainer {
Expand Down
7 changes: 6 additions & 1 deletion src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,19 @@ ContextifyContext::ContextifyContext(
// Allocation failure, maximum call stack size reached, termination, etc.
if (v8_context.IsEmpty()) return;

context_.Reset(env->isolate(), v8_context.ToLocalChecked());
Local<Context> context = v8_context.ToLocalChecked();

context_.Reset(env->isolate(), context);
context_.SetWeak(this, WeakCallback, WeakCallbackType::kParameter);
env->AddCleanupHook(CleanupHook, this);
env->async_hooks()->AddContext(context);
}


ContextifyContext::~ContextifyContext() {
env()->RemoveCleanupHook(CleanupHook, this);

env()->async_hooks()->RemoveContext(PersistentToLocal::Strong(context_));
}


Expand Down
35 changes: 35 additions & 0 deletions test/parallel/test-async-local-storage-contexts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

require('../common');
const assert = require('assert');
const vm = require('vm');
const { AsyncLocalStorage } = require('async_hooks');

// Regression test for https://github.com/nodejs/node/issues/38781

const context = vm.createContext({
AsyncLocalStorage,
assert
});

vm.runInContext(`
const storage = new AsyncLocalStorage()
async function test() {
return storage.run({ test: 'vm' }, async () => {
assert.ok(storage.getStore());
await 42;
assert.ok(storage.getStore());
});
}
test()
`, context);

const storage = new AsyncLocalStorage();
async function test() {
return storage.run({ test: 'main context' }, async () => {
assert.ok(storage.getStore());
await 42;
assert.ok(storage.getStore());
});
}
test();

0 comments on commit 52b1733

Please sign in to comment.