Skip to content

Commit

Permalink
promises: refactor rejection handling
Browse files Browse the repository at this point in the history
Remove the unnecessary microTasksTickObject for scheduling microtasks
and instead use TickInfo to keep track of whether promise rejections
exist that need to be emitted. Consequently allow the microtasks to
execute on average fewer times, in more predictable manner than
previously.

Simplify unhandled & handled rejection tracking to do more in C++ to
avoid needing to expose additional info in JS.

When new unhandledRejections are emitted within an unhandledRejection
handler, allow the event loop to proceed first instead. This means
that if the end-user code handles all promise rejections on nextTick,
rejections within unhandledRejection now won't spiral into an infinite
loop.

PR-URL: #18207
Fixes: #17913
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
  • Loading branch information
apapirovski committed Jan 21, 2018
1 parent 9545c48 commit d62566e
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 157 deletions.
33 changes: 4 additions & 29 deletions lib/internal/process/next_tick.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function setupNextTick() {
const async_hooks = require('internal/async_hooks');
const promises = require('internal/process/promises');
const errors = require('internal/errors');
const emitPendingUnhandledRejections = promises.setup(scheduleMicrotasks);
const { emitPromiseRejectionWarnings } = promises;
const getDefaultTriggerAsyncId = async_hooks.getDefaultTriggerAsyncId;
// Two arrays that share state between C++ and JS.
const { async_hook_fields, async_id_fields } = async_wrap;
Expand All @@ -30,6 +30,7 @@ function setupNextTick() {

// *Must* match Environment::TickInfo::Fields in src/env.h.
const kHasScheduled = 0;
const kHasPromiseRejections = 1;

const nextTickQueue = {
head: null,
Expand Down Expand Up @@ -58,39 +59,13 @@ function setupNextTick() {
}
};

var microtasksScheduled = false;

process.nextTick = nextTick;
// Needs to be accessible from beyond this scope.
process._tickCallback = _tickCallback;

// Set the nextTick() function for internal usage.
exports.nextTick = internalNextTick;

const microTasksTickObject = {
callback: runMicrotasksCallback,
args: undefined,
[async_id_symbol]: 0,
[trigger_async_id_symbol]: 0
};
function scheduleMicrotasks() {
if (microtasksScheduled)
return;

// For the moment all microtasks come from the void until the PromiseHook
// API is implemented.
nextTickQueue.push(microTasksTickObject);
microtasksScheduled = true;
}

function runMicrotasksCallback() {
microtasksScheduled = false;
runMicrotasks();

if (nextTickQueue.head !== null || emitPendingUnhandledRejections())
scheduleMicrotasks();
}

function _tickCallback() {
let tock;
do {
Expand Down Expand Up @@ -118,8 +93,8 @@ function setupNextTick() {
emitAfter(asyncId);
}
runMicrotasks();
emitPendingUnhandledRejections();
} while (nextTickQueue.head !== null);
} while (nextTickQueue.head !== null || emitPromiseRejectionWarnings());
tickInfo[kHasPromiseRejections] = 0;
}

class TickObject {
Expand Down
186 changes: 86 additions & 100 deletions lib/internal/process/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,123 +2,109 @@

const { safeToString } = process.binding('util');

const promiseRejectEvent = process._promiseRejectEvent;
const hasBeenNotifiedProperty = new WeakMap();
const promiseToGuidProperty = new WeakMap();
const maybeUnhandledPromises = new WeakMap();
const pendingUnhandledRejections = [];
let lastPromiseId = 1;
const asyncHandledRejections = [];
let lastPromiseId = 0;

exports.setup = setupPromises;
module.exports = {
emitPromiseRejectionWarnings
};

function getAsynchronousRejectionWarningObject(uid) {
return new Error('Promise rejection was handled ' +
`asynchronously (rejection id: ${uid})`);
}

function setupPromises(scheduleMicrotasks) {
let deprecationWarned = false;
process._setupPromises(unhandledRejection, handledRejection);

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 unhandledRejection(promise, reason) {
maybeUnhandledPromises.set(promise, {
reason,
uid: ++lastPromiseId,
warned: false
});
pendingUnhandledRejections.push(promise);
return true;
}

function unhandledRejection(promise, reason) {
hasBeenNotifiedProperty.set(promise, false);
promiseToGuidProperty.set(promise, lastPromiseId++);
addPendingUnhandledRejection(promise, reason);
function handledRejection(promise) {
const promiseInfo = maybeUnhandledPromises.get(promise);
if (promiseInfo !== undefined) {
maybeUnhandledPromises.delete(promise);
if (promiseInfo.warned) {
const { uid } = promiseInfo;
// Generate the warning object early to get a good stack trace.
const warning = new Error('Promise rejection was handled ' +
`asynchronously (rejection id: ${uid})`);
warning.name = 'PromiseRejectionHandledWarning';
warning.id = uid;
asyncHandledRejections.push({ promise, warning });
return true;
}
}
return false;
}

function rejectionHandled(promise) {
const hasBeenNotified = hasBeenNotifiedProperty.get(promise);
if (hasBeenNotified !== undefined) {
hasBeenNotifiedProperty.delete(promise);
const uid = promiseToGuidProperty.get(promise);
promiseToGuidProperty.delete(promise);
if (hasBeenNotified === true) {
let warning = null;
if (!process.listenerCount('rejectionHandled')) {
// Generate the warning object early to get a good stack trace.
warning = getAsynchronousRejectionWarningObject(uid);
}
process.nextTick(function() {
if (!process.emit('rejectionHandled', promise)) {
if (warning === null)
warning = getAsynchronousRejectionWarningObject(uid);
warning.name = 'PromiseRejectionHandledWarning';
warning.id = uid;
process.emitWarning(warning);
}
});
}

const unhandledRejectionErrName = 'UnhandledPromiseRejectionWarning';
function emitWarning(uid, reason) {
try {
if (reason instanceof Error) {
process.emitWarning(reason.stack, unhandledRejectionErrName);
} else {
process.emitWarning(safeToString(reason), unhandledRejectionErrName);
}
} catch (e) {
// ignored
}

function emitWarning(uid, reason) {
try {
if (reason instanceof Error) {
process.emitWarning(reason.stack, 'UnhandledPromiseRejectionWarning');
} else {
process.emitWarning(
safeToString(reason), 'UnhandledPromiseRejectionWarning'
);
}
} catch (e) {
// ignored
const warning = new Error(
'Unhandled promise rejection. This error originated either by ' +
'throwing inside of an async function without a catch block, ' +
'or by rejecting a promise which was not handled with .catch(). ' +
`(rejection id: ${uid})`
);
warning.name = unhandledRejectionErrName;
try {
if (reason instanceof Error) {
warning.stack = reason.stack;
}
} catch (err) {
// ignored
}
process.emitWarning(warning);
emitDeprecationWarning();
}

const warning = new Error(
'Unhandled promise rejection. This error originated either by ' +
'throwing inside of an async function without a catch block, ' +
'or by rejecting a promise which was not handled with .catch(). ' +
`(rejection id: ${uid})`
);
warning.name = 'UnhandledPromiseRejectionWarning';
try {
if (reason instanceof Error) {
warning.stack = reason.stack;
}
} catch (err) {
// ignored
}
process.emitWarning(warning);
if (!deprecationWarned) {
deprecationWarned = true;
process.emitWarning(
'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.',
'DeprecationWarning', 'DEP0018');
}
let deprecationWarned = false;
function emitDeprecationWarning() {
if (!deprecationWarned) {
deprecationWarned = true;
process.emitWarning(
'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.',
'DeprecationWarning', 'DEP0018');
}
}

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);
if (!process.emit('unhandledRejection', reason, promise)) {
emitWarning(uid, reason);
} else {
hadListeners = true;
}
}
function emitPromiseRejectionWarnings() {
while (asyncHandledRejections.length > 0) {
const { promise, warning } = asyncHandledRejections.shift();
if (!process.emit('rejectionHandled', promise)) {
process.emitWarning(warning);
}
return hadListeners;
}

function addPendingUnhandledRejection(promise, reason) {
pendingUnhandledRejections.push(promise, reason);
scheduleMicrotasks();
let hadListeners = false;
let len = pendingUnhandledRejections.length;
while (len--) {
const promise = pendingUnhandledRejections.shift();
const promiseInfo = maybeUnhandledPromises.get(promise);
if (promiseInfo !== undefined) {
promiseInfo.warned = true;
const { reason, uid } = promiseInfo;
if (!process.emit('unhandledRejection', reason, promise)) {
emitWarning(uid, reason);
} else {
hadListeners = true;
}
}
}

return emitPendingUnhandledRejections;
return hadListeners || pendingUnhandledRejections.length !== 0;
}
8 changes: 8 additions & 0 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,14 @@ inline bool Environment::TickInfo::has_scheduled() const {
return fields_[kHasScheduled] == 1;
}

inline bool Environment::TickInfo::has_promise_rejections() const {
return fields_[kHasPromiseRejections] == 1;
}

inline void Environment::TickInfo::promise_rejections_toggle_on() {
fields_[kHasPromiseRejections] = 1;
}

inline void Environment::AssignToContext(v8::Local<v8::Context> context,
const ContextInfo& info) {
context->SetAlignedPointerInEmbedderData(kContextEmbedderDataIndex, this);
Expand Down
7 changes: 6 additions & 1 deletion src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,8 @@ class ModuleWrap;
V(performance_entry_callback, v8::Function) \
V(performance_entry_template, v8::Function) \
V(process_object, v8::Object) \
V(promise_reject_function, v8::Function) \
V(promise_reject_handled_function, v8::Function) \
V(promise_reject_unhandled_function, v8::Function) \
V(promise_wrap_template, v8::ObjectTemplate) \
V(push_values_to_array_function, v8::Function) \
V(randombytes_constructor_template, v8::ObjectTemplate) \
Expand Down Expand Up @@ -482,13 +483,17 @@ class Environment {
public:
inline AliasedBuffer<uint8_t, v8::Uint8Array>& fields();
inline bool has_scheduled() const;
inline bool has_promise_rejections() const;

inline void promise_rejections_toggle_on();

private:
friend class Environment; // So we can call the constructor.
inline explicit TickInfo(v8::Isolate* isolate);

enum Fields {
kHasScheduled,
kHasPromiseRejections,
kFieldsCount
};

Expand Down
Loading

0 comments on commit d62566e

Please sign in to comment.