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

weak handles and promises in progress stuff #5292

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
13 changes: 10 additions & 3 deletions lib/internal/process/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,21 @@ const pendingUnhandledRejections = [];
exports.setup = setupPromises;

function setupPromises(scheduleMicrotasks) {
const promiseInternals = {};

process._setupPromises(function(event, promise, reason) {
if (event === promiseRejectEvent.unhandled)
unhandledRejection(promise, reason);
else if (event === promiseRejectEvent.handled)
rejectionHandled(promise);
else
require('assert').fail(null, null, 'unexpected PromiseRejectEvent');
});
}, function getPromiseReason(data) {
var valueindex = data.indexOf('[[PromiseValue]]');
if (valueindex > 0) {
return data[valueindex + 1];
}
}, promiseInternals);

function unhandledRejection(promise, reason) {
hasBeenNotifiedProperty.set(promise, false);
Expand All @@ -26,6 +33,7 @@ function setupPromises(scheduleMicrotasks) {
if (hasBeenNotified !== undefined) {
hasBeenNotifiedProperty.delete(promise);
if (hasBeenNotified === true) {
promiseInternals.untrackPromise(promise);
process.nextTick(function() {
process.emit('rejectionHandled', promise);
});
Expand All @@ -42,8 +50,7 @@ function setupPromises(scheduleMicrotasks) {
if (hasBeenNotifiedProperty.get(promise) === false) {
hasBeenNotifiedProperty.set(promise, true);
if (!process.emit('unhandledRejection', reason, promise)) {
// Nobody is listening.
// TODO(petkaantonov) Take some default action, see #830
promiseInternals.onPromiseGC(promise);
} else {
hadListeners = true;
}
Expand Down
2 changes: 2 additions & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
'src/stream_wrap.cc',
'src/tcp_wrap.cc',
'src/timer_wrap.cc',
'src/track-promise.cc',
'src/tty_wrap.cc',
'src/process_wrap.cc',
'src/udp_wrap.cc',
Expand Down Expand Up @@ -180,6 +181,7 @@
'src/node_revert.h',
'src/node_i18n.h',
'src/pipe_wrap.h',
'src/track-promise.h',
'src/tty_wrap.h',
'src/tcp_wrap.h',
'src/udp_wrap.h',
Expand Down
1 change: 1 addition & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ namespace node {
V(pipe_constructor_template, v8::FunctionTemplate) \
V(process_object, v8::Object) \
V(promise_reject_function, v8::Function) \
V(promise_unhandled_rejection, v8::Function) \
V(push_values_to_array_function, v8::Function) \
V(script_context_constructor_template, v8::FunctionTemplate) \
V(script_data_constructor_function, v8::Function) \
Expand Down
83 changes: 82 additions & 1 deletion src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "req-wrap.h"
#include "req-wrap-inl.h"
#include "string_bytes.h"
#include "track-promise.h"
#include "util.h"
#include "uv.h"
#include "libplatform/libplatform.h"
Expand Down Expand Up @@ -104,6 +105,7 @@ using v8::Array;
using v8::ArrayBuffer;
using v8::Boolean;
using v8::Context;
using v8::Debug;
using v8::EscapableHandleScope;
using v8::Exception;
using v8::Function;
Expand All @@ -118,10 +120,12 @@ using v8::Locker;
using v8::MaybeLocal;
using v8::Message;
using v8::Name;
using v8::NativeWeakMap;
using v8::Null;
using v8::Number;
using v8::Object;
using v8::ObjectTemplate;
using v8::Persistent;
using v8::Promise;
using v8::PromiseRejectMessage;
using v8::PropertyCallbackInfo;
Expand Down Expand Up @@ -167,6 +171,10 @@ static const char* icu_data_dir = nullptr;
// used by C++ modules as well
bool no_deprecation = false;

// Unhandled promises tracking
Persistent<NativeWeakMap>* remainingURs;
Persistent<Object>* first_ur_key;
Copy link
Member

Choose a reason for hiding this comment

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

Should be part of Environment. Also, why are these pointers?

Copy link
Contributor Author

@Fishrock123 Fishrock123 Apr 25, 2016

Choose a reason for hiding this comment

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

1.Will do.
2. Persistents appear to return pointers?

Copy link
Member

Choose a reason for hiding this comment

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

I don't understand 2 but Persistent is just a value type. You assign it a Local through the .Reset() method. Or is your question how to turn a Persistent into a Local again?


#if HAVE_OPENSSL && NODE_FIPS_MODE
// used by crypto module
bool enable_fips_crypto = false;
Expand Down Expand Up @@ -1138,14 +1146,59 @@ void PromiseRejectCallback(PromiseRejectMessage message) {
callback->Call(process, arraysize(args), args);
}

void OnPromiseGC(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

CHECK(args[0]->IsObject());
Local<Object> promise = args[0].As<Object>();

TrackPromise::New(env->isolate(), promise);

HandleScope scope(env->isolate());
Copy link
Member

Choose a reason for hiding this comment

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

Why the HandleScope?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm pretty sure I needed it to make new Locals?

Copy link
Member

Choose a reason for hiding this comment

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

JS -> C++ callbacks have an implicit HandleScope, you don't need to create one yourself.

Local<NativeWeakMap> l_remainingURs =
Local<NativeWeakMap>::New(env->isolate(), *remainingURs);
Local<Object> l_first_ur_key =
Local<Object>::New(env->isolate(), *first_ur_key);
if (!l_remainingURs->Has(l_first_ur_key)) {
l_remainingURs->Set(l_first_ur_key, promise);
}
}

void UntrackPromise(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

CHECK(args[0]->IsObject());
Local<Value> promise = args[0].As<Value>();

HandleScope scope(env->isolate());
Local<NativeWeakMap> l_remainingURs =
Local<NativeWeakMap>::New(env->isolate(), *remainingURs);
Local<Object> l_first_ur_key =
Local<Object>::New(env->isolate(), *first_ur_key);
if (l_remainingURs->Get(l_first_ur_key) == promise) {
l_remainingURs->Delete(l_first_ur_key);
}
}

void SetupPromises(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Isolate* isolate = env->isolate();

HandleScope scope(isolate);
remainingURs = new Persistent<NativeWeakMap>(isolate,
NativeWeakMap::New(isolate));
first_ur_key = new Persistent<Object>(isolate, Object::New(isolate));

CHECK(args[0]->IsFunction());
CHECK(args[1]->IsFunction());
CHECK(args[2]->IsObject());

isolate->SetPromiseRejectCallback(PromiseRejectCallback);
env->set_promise_reject_function(args[0].As<Function>());
env->set_promise_unhandled_rejection(args[1].As<Function>());

env->SetMethod(args[2].As<Object>(), "onPromiseGC", OnPromiseGC);
env->SetMethod(args[2].As<Object>(), "untrackPromise", UntrackPromise);

env->process_object()->Delete(
env->context(),
Expand Down Expand Up @@ -1574,8 +1627,26 @@ void AppendExceptionLine(Environment* env,
PrintErrorString("\n%s", arrow);
}

void ReportPromiseRejection(Isolate* isolate, Local<Object> object) {
Environment* env = Environment::GetCurrent(isolate);
Local<Function> fn = env->promise_unhandled_rejection();

if (!fn.IsEmpty()) {
HandleScope scope(isolate);
Local<Value> promise = object.As<Value>();
Local<Value> internalProps =
Debug::GetInternalProperties(isolate,
promise).ToLocalChecked().As<Value>();

Local<Value> err = fn->Call(env->context(), Null(env->isolate()), 1,
&internalProps).ToLocalChecked();

ReportException(env, err, Exception::CreateMessage(isolate, err));
}
exit(1);
}

static void ReportException(Environment* env,
void ReportException(Environment* env,
Local<Value> er,
Local<Message> message) {
HandleScope scope(env->isolate());
Expand Down Expand Up @@ -4323,6 +4394,16 @@ static void StartNodeInstance(void* arg) {
} while (more == true);
}

HandleScope scope(isolate);
Local<NativeWeakMap> l_remainingURs =
Local<NativeWeakMap>::New(isolate, *remainingURs);
Local<Object> l_first_ur_key =
Local<Object>::New(env->isolate(), *first_ur_key);
if (l_remainingURs->Has(l_first_ur_key)) {
Local<Object> firstUR = l_remainingURs->Get(l_first_ur_key).As<Object>();
ReportPromiseRejection(isolate, firstUR);
}

env->set_trace_sync_io(false);

int exit_code = EmitExit(env);
Expand Down
6 changes: 6 additions & 0 deletions src/node_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ constexpr size_t arraysize(const T(&)[N]) { return N; }

bool IsExceptionDecorated(Environment* env, v8::Local<v8::Value> er);

void ReportPromiseRejection(v8::Isolate* isolate, v8::Local<v8::Object> object);

void ReportException(Environment* env,
v8::Local<v8::Value> er,
v8::Local<v8::Message> message);

void AppendExceptionLine(Environment* env,
v8::Local<v8::Value> er,
v8::Local<v8::Message> message);
Expand Down
67 changes: 67 additions & 0 deletions src/track-promise.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include "env.h"
#include "env-inl.h"
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not following why "track-promise.h" isn't included here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@trevnorris Actually, it seemed to function without anything being included. I really don't know why?

Copy link
Contributor

Choose a reason for hiding this comment

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

It seems you have a copy of the TrackPromise class duplicated in this file below – that's why it works.

If nothing else, other than this files, needs to know about the TrackPromise class, you don't need track-promise.h.

#include "node_internals.h"

namespace node {

using v8::Exception;
using v8::Function;
using v8::HandleScope;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::Persistent;
using v8::Value;
using v8::WeakCallbackData;

typedef void (*FreeCallback)(Local<Object> object, Local<Function> fn);

class TrackPromise {
public:
static TrackPromise* New(Isolate* isolate, Local<Object> object);
inline Persistent<Object>* persistent();
private:
static void WeakCallback(const WeakCallbackData<Object, TrackPromise>&);
inline void WeakCallback(Isolate* isolate, Local<Object> object);
inline TrackPromise(Isolate* isolate, Local<Object> object);
~TrackPromise();
Persistent<Object> persistent_;
};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bnoordhuis I'm not sure to get rid of this; it's duplicated in the header but that doesn't seem to work?

Copy link
Member

Choose a reason for hiding this comment

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

That's because you don't #include "track-promise.h" at the top, isn't it?



TrackPromise* TrackPromise::New(Isolate* isolate,
Local<Object> object) {
return new TrackPromise(isolate, object);
}


Persistent<Object>* TrackPromise::persistent() {
return &persistent_;
}


TrackPromise::TrackPromise(Isolate* isolate,
Local<Object> object)
: persistent_(isolate, object) {
persistent_.SetWeak(this, WeakCallback);
persistent_.MarkIndependent();
}


TrackPromise::~TrackPromise() {
persistent_.Reset();
}


void TrackPromise::WeakCallback(
const WeakCallbackData<Object, TrackPromise>& data) {
data.GetParameter()->WeakCallback(data.GetIsolate(), data.GetValue());
}


void TrackPromise::WeakCallback(Isolate* isolate, Local<Object> object) {
node::ReportPromiseRejection(isolate, object);
delete this;
}

} // namespace node
31 changes: 31 additions & 0 deletions src/track-promise.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef SRC_TRACK_PROMISE_H_
#define SRC_TRACK_PROMISE_H_

#include "v8.h"

namespace node {

class Environment;

class TrackPromise {
public:
TrackPromise(v8::Isolate* isolate, v8::Local<v8::Object> object);
virtual ~TrackPromise();

static TrackPromise* New(v8::Isolate* isolate,
v8::Local<v8::Object> object);

inline v8::Persistent<v8::Object>& persistent();

inline void WeakCallback(
const v8::WeakCallbackData<v8::Object, TrackPromise>& data);

private:
inline void WeakCallback(v8::Isolate* isolate, v8::Local<v8::Object> object);

v8::Persistent<v8::Object> persistent_;
};

} // namespace node

#endif // SRC_TRACK_PROMISE_H_
10 changes: 10 additions & 0 deletions test/message/promise_fast_reject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';
require('../common');

new Promise(function(res, rej) {
consol.log('One');
});

new Promise(function(res, rej) {
consol.log('Two');
});
15 changes: 15 additions & 0 deletions test/message/promise_fast_reject.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
*test*message*promise_fast_reject.js:*
consol.log('One');
^

ReferenceError: consol is not defined
at *test*message*promise_fast_reject.js:*:*
at Object.<anonymous> (*test*message*promise_fast_reject.js:*:*)
at Module._compile (module.js:*:*)
at Object.Module._extensions..js (module.js:*:*)
at Module.load (module.js:*:*)
at tryModuleLoad (module.js:*:*)
at Function.Module._load (module.js:*:*)
at Function.Module.runMain (module.js:*:*)
at startup (node.js:*:*)
at node.js:*:*
3 changes: 3 additions & 0 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,9 @@ assert.equal(util.inspect(set, true), 'Set { \'foo\', [size]: 1, bar: 42 }');

// test Promise
assert.equal(util.inspect(Promise.resolve(3)), 'Promise { 3 }');
process.on('unhandledRejection', (message) => {
assert.strictEqual(3, message);
});
assert.equal(util.inspect(Promise.reject(3)), 'Promise { <rejected> 3 }');
assert.equal(util.inspect(new Promise(function() {})), 'Promise { <pending> }');
var promise = Promise.resolve('foo');
Expand Down