Skip to content

Commit

Permalink
draft
Browse files Browse the repository at this point in the history
  • Loading branch information
sajal50 committed Feb 7, 2021
1 parent c0e66e3 commit fa97587
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/internal/process/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ function unhandledRejection(promise, reason) {
warned: false,
domain: process.domain
});
console.log(require('async_hooks').executionAsyncId());
// This causes the promise to be referenced at least for one tick.
ArrayPrototypePush(pendingUnhandledRejections, promise);
setHasRejectionToWarn(true);
Expand Down
32 changes: 32 additions & 0 deletions src/node_task_queue.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ using v8::Promise;
using v8::PromiseRejectEvent;
using v8::PromiseRejectMessage;
using v8::Value;
using v8::Maybe;
using v8::Just;
using v8::Nothing;

static Maybe<double> GetAssignedPromiseAsyncId(Environment* env,
Local<Promise> promise,
Local<Value> id_symbol) {
Local<Value> maybe_async_id;
if (!promise->Get(env->context(), id_symbol).ToLocal(&maybe_async_id)) {
return Nothing<double>();
}
return maybe_async_id->IsNumber()
? maybe_async_id->NumberValue(env->context())
: v8::Just(-1.0);
}

void PromiseRejectCallback(PromiseRejectMessage message) {
static std::atomic<uint64_t> unhandledRejections{0};
Expand Down Expand Up @@ -79,9 +94,26 @@ void PromiseRejectCallback(PromiseRejectMessage message) {
// V8 does not expect this callback to have a scheduled exceptions once it
// returns, so we print them out in a best effort to do something about it
// without failing silently and without crashing the process.
double async_id;
double trigger_async_id;
if (!GetAssignedPromiseAsyncId(env, promise, env->async_id_symbol())
.To(&async_id)) return;
if (!GetAssignedPromiseAsyncId(env, promise, env->trigger_async_id_symbol())
.To(&trigger_async_id)) return;

if (async_id != -1.0 && trigger_async_id != -1.0) {
env->async_hooks()->push_async_context(
async_id, trigger_async_id, promise);
}
TryCatchScope try_catch(env);
USE(callback->Call(
env->context(), Undefined(isolate), arraysize(args), args));
if (env->execution_async_id() == async_id) {
// This condition might not be true if async_hooks was enabled during
// the promise callback execution.
env->async_hooks()->pop_async_context(async_id);
}

if (try_catch.HasCaught() && !try_catch.HasTerminated()) {
fprintf(stderr, "Exception in PromiseRejectCallback:\n");
PrintCaughtException(isolate, env->context(), try_catch);
Expand Down

0 comments on commit fa97587

Please sign in to comment.