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

Fix async wrap http #4509

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
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
Next Next commit
env: introduce AsyncWrapScope for MakeCallback
Do not go through async wrap hooks if we are already within the
`MakeCallback`. This commit allows calling `MakeCallback` recursively.
indutny committed Jan 1, 2016
commit 2100e96b39916de1cce0e1eeeecfed3f28c252fd
17 changes: 16 additions & 1 deletion src/async-wrap.cc
Original file line number Diff line number Diff line change
@@ -176,10 +176,25 @@ Local<Value> AsyncWrap::MakeCallback(const Local<Function> cb,
int argc,
Local<Value>* argv) {
CHECK(env()->context() == env()->isolate()->GetCurrentContext());
Local<Object> context = object();

if (env()->in_async_callback()) {
TryCatch try_catch(env()->isolate());
try_catch.SetVerbose(true);

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

if (try_catch.HasCaught()) {
return Undefined(env()->isolate());
}

return ret;
}

Environment::AsyncCallbackScope async_scope(env());

Local<Function> pre_fn = env()->async_hooks_pre_function();
Local<Function> post_fn = env()->async_hooks_post_function();
Local<Object> context = object();
Local<Object> process = env()->process_object();
Local<Object> domain;
bool has_domain = false;
18 changes: 18 additions & 0 deletions src/env-inl.h
Original file line number Diff line number Diff line change
@@ -88,6 +88,15 @@ inline void Environment::AsyncHooks::set_enable_callbacks(uint32_t flag) {
fields_[kEnableCallbacks] = flag;
}

inline Environment::AsyncCallbackScope::AsyncCallbackScope(Environment* env) :
env_(env) {
env_->set_in_async_callback(true);
}

inline Environment::AsyncCallbackScope::~AsyncCallbackScope() {
env_->set_in_async_callback(false);
}

inline Environment::DomainFlag::DomainFlag() {
for (int i = 0; i < kFieldsCount; ++i) fields_[i] = 0;
}
@@ -206,6 +215,7 @@ inline Environment::Environment(v8::Local<v8::Context> context,
uv_loop_t* loop)
: isolate_(context->GetIsolate()),
isolate_data_(IsolateData::GetOrCreate(context->GetIsolate(), loop)),
in_async_callback_(false),
timer_base_(uv_now(loop)),
using_domains_(false),
printed_error_(false),
@@ -323,6 +333,14 @@ inline Environment::AsyncHooks* Environment::async_hooks() {
return &async_hooks_;
}

inline bool Environment::in_async_callback() const {
return in_async_callback_;
}

inline void Environment::set_in_async_callback(bool value) {
in_async_callback_ = value;
}

inline Environment::DomainFlag* Environment::domain_flag() {
return &domain_flag_;
}
13 changes: 13 additions & 0 deletions src/env.h
Original file line number Diff line number Diff line change
@@ -305,6 +305,15 @@ class Environment {
DISALLOW_COPY_AND_ASSIGN(AsyncHooks);
};

class AsyncCallbackScope {
public:
explicit AsyncCallbackScope(Environment* env);
~AsyncCallbackScope();

private:
Environment* env_;
};

class DomainFlag {
public:
inline uint32_t* fields();
@@ -435,6 +444,9 @@ class Environment {
inline void FinishHandleCleanup(uv_handle_t* handle);

inline AsyncHooks* async_hooks();
inline bool in_async_callback() const;
inline void set_in_async_callback(bool value);

inline DomainFlag* domain_flag();
inline TickInfo* tick_info();
inline ArrayBufferAllocatorInfo* array_buffer_allocator_info();
@@ -542,6 +554,7 @@ class Environment {
uv_prepare_t idle_prepare_handle_;
uv_check_t idle_check_handle_;
AsyncHooks async_hooks_;
bool in_async_callback_;
DomainFlag domain_flag_;
TickInfo tick_info_;
ArrayBufferAllocatorInfo array_buffer_allocator_info_;
2 changes: 2 additions & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
@@ -3196,6 +3196,8 @@ void LoadEnvironment(Environment* env) {
// thrown during process startup.
try_catch.SetVerbose(true);

Environment::AsyncCallbackScope async_scope(env);

env->SetMethod(env->process_object(), "_rawDebug", RawDebug);

Local<Value> arg = env->process_object();