Skip to content

src: yield empty maybes for failed AsyncWrap::MakeCallback calls #22078

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

Closed
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
2 changes: 1 addition & 1 deletion src/callback_scope.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void InternalCallbackScope::Close() {
AsyncWrap::EmitAfter(env_, async_context_.async_id);
}

if (IsInnerMakeCallback()) {
if (env_->makecallback_depth() > 1) {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ inline Environment::AsyncCallbackScope::~AsyncCallbackScope() {
env_->makecallback_cntr_--;
}

inline bool Environment::AsyncCallbackScope::in_makecallback() const {
return env_->makecallback_cntr_ > 1;
inline size_t Environment::makecallback_depth() const {
return makecallback_cntr_;
}

inline Environment::ImmediateInfo::ImmediateInfo(v8::Isolate* isolate)
Expand Down
3 changes: 2 additions & 1 deletion src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -513,14 +513,15 @@ class Environment {
AsyncCallbackScope() = delete;
explicit AsyncCallbackScope(Environment* env);
~AsyncCallbackScope();
inline bool in_makecallback() const;

private:
Environment* env_;

DISALLOW_COPY_AND_ASSIGN(AsyncCallbackScope);
};

inline size_t makecallback_depth() const;

class ImmediateInfo {
public:
inline AliasedBuffer<uint32_t, v8::Uint32Array>& fields();
Expand Down
18 changes: 11 additions & 7 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ MaybeLocal<Value> InternalMakeCallback(Environment* env,
CHECK(!recv.IsEmpty());
InternalCallbackScope scope(env, recv, asyncContext);
if (scope.Failed()) {
return Undefined(env->isolate());
return MaybeLocal<Value>();
}

Local<Function> domain_cb = env->domain_callback();
Expand All @@ -772,15 +772,13 @@ MaybeLocal<Value> InternalMakeCallback(Environment* env,
}

if (ret.IsEmpty()) {
// NOTE: For backwards compatibility with public API we return Undefined()
// if the top level call threw.
scope.MarkAsFailed();
return scope.IsInnerMakeCallback() ? ret : Undefined(env->isolate());
return MaybeLocal<Value>();
}

scope.Close();
if (scope.Failed()) {
return Undefined(env->isolate());
return MaybeLocal<Value>();
}

return ret;
Expand Down Expand Up @@ -832,8 +830,14 @@ MaybeLocal<Value> MakeCallback(Isolate* isolate,
// the two contexts need not be the same.
Environment* env = Environment::GetCurrent(callback->CreationContext());
Context::Scope context_scope(env->context());
return InternalMakeCallback(env, recv, callback,
argc, argv, asyncContext);
MaybeLocal<Value> ret = InternalMakeCallback(env, recv, callback,
argc, argv, asyncContext);
if (ret.IsEmpty() && env->makecallback_depth() == 0) {
// This is only for legacy compatiblity and we may want to look into
// removing/adjusting it.
return Undefined(env->isolate());
}
return ret;
}


Expand Down
3 changes: 0 additions & 3 deletions src/node_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -543,9 +543,6 @@ class InternalCallbackScope {

inline bool Failed() const { return failed_; }
inline void MarkAsFailed() { failed_ = true; }
inline bool IsInnerMakeCallback() const {
return callback_scope_.in_makecallback();
}

private:
Environment* env_;
Expand Down