diff --git a/src/node_task_queue.cc b/src/node_task_queue.cc index 7bd36ae698034a..1731eeba7916a5 100644 --- a/src/node_task_queue.cc +++ b/src/node_task_queue.cc @@ -25,7 +25,6 @@ using v8::kPromiseResolveAfterResolved; using v8::Local; using v8::Maybe; using v8::Number; -using v8::Nothing; using v8::Object; using v8::Promise; using v8::PromiseRejectEvent; @@ -37,7 +36,7 @@ static Maybe GetAssignedPromiseAsyncId(Environment* env, Local id_symbol) { Local maybe_async_id; if (!promise->Get(env->context(), id_symbol).ToLocal(&maybe_async_id)) { - return Nothing(); + return v8::Just(AsyncWrap::kInvalidAsyncId); } return maybe_async_id->IsNumber() ? maybe_async_id->NumberValue(env->context()) @@ -54,15 +53,15 @@ static Maybe GetAssignedPromiseWrapAsyncId(Environment* env, Local promiseWrap = promise->GetInternalField(0); if (promiseWrap->IsObject()) { Local maybe_async_id; - if (!promiseWrap.As()->Get(env->context(), id_symbol).ToLocal(&maybe_async_id)) { - return Nothing(); + if (!promiseWrap.As()->Get(env->context(), id_symbol) + .ToLocal(&maybe_async_id)) { + return v8::Just(AsyncWrap::kInvalidAsyncId); } return maybe_async_id->IsNumber() ? maybe_async_id->NumberValue(env->context()) : v8::Just(AsyncWrap::kInvalidAsyncId); - } - else { - return Nothing(); + } else { + return v8::Just(AsyncWrap::kInvalidAsyncId); } } @@ -116,28 +115,40 @@ void PromiseRejectCallback(PromiseRejectMessage message) { double async_id = AsyncWrap::kInvalidAsyncId; double trigger_async_id = AsyncWrap::kInvalidAsyncId; - GetAssignedPromiseAsyncId(env, promise, env->async_id_symbol()) - .To(&async_id); - GetAssignedPromiseAsyncId(env, promise, env->trigger_async_id_symbol()) - .To(&trigger_async_id); - - GetAssignedPromiseWrapAsyncId(env, promise, env->async_id_symbol()) - .To(&async_id); - GetAssignedPromiseWrapAsyncId(env, promise, env->trigger_async_id_symbol()) - .To(&trigger_async_id); - + TryCatchScope try_catch_async_id(env); + + 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 == AsyncWrap::kInvalidAsyncId && + trigger_async_id == AsyncWrap::kInvalidAsyncId) { + // That means that promise might be a PromiseWrap, so we'll + // check there as well. + if (!GetAssignedPromiseWrapAsyncId(env, promise, env->async_id_symbol()) + .To(&async_id)) return; + if (!GetAssignedPromiseWrapAsyncId( + env, promise, env->trigger_async_id_symbol()) + .To(&trigger_async_id)) return; + } + if (async_id != AsyncWrap::kInvalidAsyncId && trigger_async_id != AsyncWrap::kInvalidAsyncId) { env->async_hooks()->push_async_context( async_id, trigger_async_id, promise); } + if (try_catch_async_id.HasCaught()) { + // What must be done here? + } + TryCatchScope try_catch(env); USE(callback->Call( env->context(), Undefined(isolate), arraysize(args), args)); if (async_id != AsyncWrap::kInvalidAsyncId && - trigger_async_id != AsyncWrap::kInvalidAsyncId && + trigger_async_id != AsyncWrap::kInvalidAsyncId && env->execution_async_id() == async_id) { // This condition might not be true if async_hooks was enabled during // the promise callback execution. diff --git a/test/async-hooks/test-unhandled-rejection-context.js b/test/async-hooks/test-unhandled-rejection-context.js index 55fe246937a878..b70d97f99fd441 100644 --- a/test/async-hooks/test-unhandled-rejection-context.js +++ b/test/async-hooks/test-unhandled-rejection-context.js @@ -19,18 +19,9 @@ const hooks = initHooks({ }); hooks.enable(); - -// Promise.resolve() -// .then(() => { -// throw new Error('Throwing on purpose'); -// }); - Promise.reject(); process.on('unhandledRejection', common.mustCall(() => { - // assert.strictEqual(promiseAsyncIds.length, 2); - // assert.strictEqual(async_hooks.executionAsyncId(), promiseAsyncIds[1]); - assert.strictEqual(promiseAsyncIds.length, 1); assert.strictEqual(async_hooks.executionAsyncId(), promiseAsyncIds[0]); }));