Skip to content

Commit 1f9635d

Browse files
committed
lib,src: throw on unhanded promise rejections
Refs: nodejs#5292 Refs: nodejs/promises#26 Refs: nodejs#6355 PR-URL: nodejs#6375
1 parent b2c7a51 commit 1f9635d

File tree

17 files changed

+406
-271
lines changed

17 files changed

+406
-271
lines changed

deps/npm/node_modules/request/node_modules/form-data/Readme.md

Lines changed: 0 additions & 217 deletions
This file was deleted.

lib/internal/bootstrap_node.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,13 +304,13 @@
304304

305305
function setupProcessFatal() {
306306

307-
process._fatalException = function(er) {
307+
process._fatalException = function(er, fromPromise) {
308308
var caught;
309309

310310
if (process.domain && process.domain._errorHandler)
311311
caught = process.domain._errorHandler(er) || caught;
312312

313-
if (!caught)
313+
if (!caught && !fromPromise)
314314
caught = process.emit('uncaughtException', er);
315315

316316
// If someone handled it, then great. otherwise, die in C++ land

lib/internal/process/promises.js

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,88 +2,52 @@
22

33
const promiseRejectEvent = process._promiseRejectEvent;
44
const hasBeenNotifiedProperty = new WeakMap();
5-
const promiseToGuidProperty = new WeakMap();
65
const pendingUnhandledRejections = [];
7-
let lastPromiseId = 1;
86

97
exports.setup = setupPromises;
108

11-
function getAsynchronousRejectionWarningObject(uid) {
12-
return new Error('Promise rejection was handled ' +
13-
`asynchronously (rejection id: ${uid})`);
14-
}
15-
169
function setupPromises(scheduleMicrotasks) {
10+
const promiseInternals = {};
11+
1712
process._setupPromises(function(event, promise, reason) {
1813
if (event === promiseRejectEvent.unhandled)
1914
unhandledRejection(promise, reason);
2015
else if (event === promiseRejectEvent.handled)
2116
rejectionHandled(promise);
2217
else
2318
require('assert').fail(null, null, 'unexpected PromiseRejectEvent');
24-
});
19+
}, function getPromiseReason(data) {
20+
return data[data.indexOf('[[PromiseValue]]') + 1];
21+
}, promiseInternals);
2522

2623
function unhandledRejection(promise, reason) {
2724
hasBeenNotifiedProperty.set(promise, false);
28-
promiseToGuidProperty.set(promise, lastPromiseId++);
2925
addPendingUnhandledRejection(promise, reason);
3026
}
3127

3228
function rejectionHandled(promise) {
3329
const hasBeenNotified = hasBeenNotifiedProperty.get(promise);
3430
if (hasBeenNotified !== undefined) {
3531
hasBeenNotifiedProperty.delete(promise);
36-
const uid = promiseToGuidProperty.get(promise);
37-
promiseToGuidProperty.delete(promise);
3832
if (hasBeenNotified === true) {
39-
let warning = null;
40-
if (!process.listenerCount('rejectionHandled')) {
41-
// Generate the warning object early to get a good stack trace.
42-
warning = getAsynchronousRejectionWarningObject(uid);
43-
}
33+
promiseInternals.untrackPromise(promise);
4434
process.nextTick(function() {
45-
if (!process.emit('rejectionHandled', promise)) {
46-
if (warning === null)
47-
warning = getAsynchronousRejectionWarningObject(uid);
48-
warning.name = 'PromiseRejectionHandledWarning';
49-
warning.id = uid;
50-
process.emitWarning(warning);
51-
}
35+
process.emit('rejectionHandled', promise);
5236
});
5337
}
5438

5539
}
5640
}
5741

58-
function emitWarning(uid, reason) {
59-
const warning = new Error('Unhandled promise rejection ' +
60-
`(rejection id: ${uid}): ${reason}`);
61-
warning.name = 'UnhandledPromiseRejectionWarning';
62-
warning.id = uid;
63-
if (reason instanceof Error) {
64-
warning.stack = reason.stack;
65-
}
66-
process.emitWarning(warning);
67-
if (!deprecationWarned) {
68-
deprecationWarned = true;
69-
process.emitWarning(
70-
'Unhandled promise rejections are deprecated. In the future, ' +
71-
'promise rejections that are not handled will terminate the ' +
72-
'Node.js process with a non-zero exit code.',
73-
'DeprecationWarning', 'DEP0018');
74-
}
75-
}
76-
var deprecationWarned = false;
7742
function emitPendingUnhandledRejections() {
7843
let hadListeners = false;
7944
while (pendingUnhandledRejections.length > 0) {
8045
const promise = pendingUnhandledRejections.shift();
8146
const reason = pendingUnhandledRejections.shift();
8247
if (hasBeenNotifiedProperty.get(promise) === false) {
8348
hasBeenNotifiedProperty.set(promise, true);
84-
const uid = promiseToGuidProperty.get(promise);
8549
if (!process.emit('unhandledRejection', reason, promise)) {
86-
emitWarning(uid, reason);
50+
promiseInternals.trackPromise(promise);
8751
} else {
8852
hadListeners = true;
8953
}

node.gyp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@
196196
'src/stream_wrap.cc',
197197
'src/tcp_wrap.cc',
198198
'src/timer_wrap.cc',
199+
'src/track-promise.cc',
199200
'src/tracing/agent.cc',
200201
'src/tracing/node_trace_buffer.cc',
201202
'src/tracing/node_trace_writer.cc',
@@ -230,6 +231,7 @@
230231
'src/node_revert.h',
231232
'src/node_i18n.h',
232233
'src/pipe_wrap.h',
234+
'src/track-promise.h',
233235
'src/tty_wrap.h',
234236
'src/tcp_wrap.h',
235237
'src/udp_wrap.h',

src/env.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ namespace node {
248248
V(zero_return_string, "ZERO_RETURN") \
249249

250250
#define ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES(V) \
251+
V(array_from, v8::Function) \
251252
V(as_external, v8::External) \
252253
V(async_hooks_destroy_function, v8::Function) \
253254
V(async_hooks_init_function, v8::Function) \
@@ -264,7 +265,10 @@ namespace node {
264265
V(module_load_list_array, v8::Array) \
265266
V(pipe_constructor_template, v8::FunctionTemplate) \
266267
V(process_object, v8::Object) \
267-
V(promise_reject_function, v8::Function) \
268+
V(promise_unhandled_rejection_function, v8::Function) \
269+
V(promise_unhandled_rejection, v8::Function) \
270+
V(promise_unhandled_reject_map, v8::NativeWeakMap) \
271+
V(promise_unhandled_reject_keys, v8::Set) \
268272
V(push_values_to_array_function, v8::Function) \
269273
V(script_context_constructor_template, v8::FunctionTemplate) \
270274
V(script_data_constructor_function, v8::Function) \

0 commit comments

Comments
 (0)