Skip to content
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
70 changes: 52 additions & 18 deletions lib/internal/process/promises.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
'use strict';

const promiseRejectEvent = process._promiseRejectEvent;
const hasBeenNotifiedProperty = new WeakMap();
const promiseToGuidProperty = new WeakMap();
const pendingUnhandledRejections = [];

const kNotified = Symbol('node-unhandledrejection-notified');
const kGuid = Symbol('node-unhandledrejection-uid');
let lastPromiseId = 1;

exports.setup = setupPromises;
class RejectedList {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not simply class RejectedList extends Array { push(promise, reason) {super.push({promise, reason})} }?

push(promise, reason) {
if (this.tail === undefined) {
this.head = this.tail = {promise, reason, next: undefined};
} else {
this.tail.next = {promise, reason, next: undefined};
this.tail = this.tail.next;
}
}

shift() {
var item = this.head;
if (item === undefined)
return;
this.head = item.next;
if (this.head === undefined)
this.tail = undefined;
return item;
}
}

const pendingUnhandledRejections = new RejectedList();

function getAsynchronousRejectionWarningObject(uid) {
return new Error('Promise rejection was handled ' +
Expand All @@ -24,17 +45,27 @@ function setupPromises(scheduleMicrotasks) {
});

function unhandledRejection(promise, reason) {
hasBeenNotifiedProperty.set(promise, false);
promiseToGuidProperty.set(promise, lastPromiseId++);
Object.defineProperties(promise, {
[kNotified]: {
enumerable: false,
configurable: true,
writable: true,
value: false
},
[kGuid]: {
enumerable: false,
configurable: true,
value: lastPromiseId++
}
});
addPendingUnhandledRejection(promise, reason);
}

function rejectionHandled(promise) {
const hasBeenNotified = hasBeenNotifiedProperty.get(promise);
const hasBeenNotified = promise[kNotified];
if (hasBeenNotified !== undefined) {
hasBeenNotifiedProperty.delete(promise);
const uid = promiseToGuidProperty.get(promise);
promiseToGuidProperty.delete(promise);
promise[kNotified] = undefined;
const uid = promise[kGuid];
if (hasBeenNotified === true) {
let warning = null;
if (!process.listenerCount('rejectionHandled')) {
Expand All @@ -57,12 +88,13 @@ function setupPromises(scheduleMicrotasks) {

function emitWarning(uid, reason) {
const warning = new Error('Unhandled promise rejection ' +
`(rejection id: ${uid}): ${String(reason)}`);
`(rejection id: ${uid})`);
warning.name = 'UnhandledPromiseRejectionWarning';
warning.id = uid;
if (reason instanceof Error) {
warning.stack = reason.stack;
warning.detail = reason.stack;
}
warning.reason = reason;
process.emitWarning(warning);
if (!deprecationWarned) {
deprecationWarned = true;
Expand All @@ -76,12 +108,12 @@ function setupPromises(scheduleMicrotasks) {
var deprecationWarned = false;
function emitPendingUnhandledRejections() {
let hadListeners = false;
while (pendingUnhandledRejections.length > 0) {
const promise = pendingUnhandledRejections.shift();
const reason = pendingUnhandledRejections.shift();
if (hasBeenNotifiedProperty.get(promise) === false) {
hasBeenNotifiedProperty.set(promise, true);
const uid = promiseToGuidProperty.get(promise);
let item;
while ((item = pendingUnhandledRejections.shift()) !== undefined) {
const { promise, reason } = item;
if (promise[kNotified] === false) {
promise[kNotified] = true;
const uid = promise[kGuid];
if (!process.emit('unhandledRejection', reason, promise)) {
emitWarning(uid, reason);
} else {
Expand All @@ -99,3 +131,5 @@ function setupPromises(scheduleMicrotasks) {

return emitPendingUnhandledRejections;
}

exports.setup = setupPromises;
4 changes: 4 additions & 0 deletions test/message/unhandled_promise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
'use strict';
const common = require('../common');
const p = Promise.reject(new Error('This was rejected'));
setImmediate(() => p.catch(common.noop));
13 changes: 13 additions & 0 deletions test/message/unhandled_promise.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(node:*) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1)
Error: This was rejected
at *
at *
at *
at *
at *
at *
at *
at *
at *
(node:*) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:*) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously (rejection id: 1)
23 changes: 16 additions & 7 deletions test/message/unhandled_promise_trace_warnings.out
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
(node:*) Error: This was rejected
at * (*test*message*unhandled_promise_trace_warnings.js:*)
(node:*) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1)
at *
at *
at *
at *
at *
at *
at *
at *
Error: This was rejected
at *
at *
at *
at *
Expand All @@ -18,12 +27,12 @@
at *
at *
(node:*) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously (rejection id: 1)
at getAsynchronousRejectionWarningObject (internal/process/promises.js:*)
at rejectionHandled (internal/process/promises.js:*)
at *
at Promise.then *
at Promise.catch *
at Immediate.setImmediate [as _onImmediate] (*test*message*unhandled_promise_trace_warnings.js:*)
at *
at *
at *
at *
at *
at *
at *
at *
3 changes: 1 addition & 2 deletions test/parallel/test-promises-unhandled-symbol-rejections.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ const expectedDeprecationWarning = 'Unhandled promise rejections are ' +
'rejections that are not handled will ' +
'terminate the Node.js process with a ' +
'non-zero exit code.';
const expectedPromiseWarning = 'Unhandled promise rejection (rejection id: ' +
'1): Symbol()';
const expectedPromiseWarning = 'Unhandled promise rejection (rejection id: 1)';

common.expectWarning({
DeprecationWarning: expectedDeprecationWarning,
Expand Down