GeezerVison is showing github.com — src: refactor uncaught exception handling · jaku/node@a33c3c6
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/bootstrap/node.js +8-2Lines changed: 8 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -254,12 +254,18 @@ Object.defineProperty(process, 'features', { | 254 | 254 |
| 255 | 255 | { | 256 | 256 | const { | 257 | | - fatalException, | | 257 | + onGlobalUncaughtException, | 258 | 258 | setUncaughtExceptionCaptureCallback, | 259 | 259 | hasUncaughtExceptionCaptureCallback | 260 | 260 | } = require('internal/process/execution'); | 261 | 261 |
| 262 | | - process._fatalException = fatalException; | | 262 | + // For legacy reasons this is still called `_fatalException`, even | | 263 | + // though it is now a global uncaught exception handler. | | 264 | + // The C++ land node::errors::TriggerUncaughtException grabs it | | 265 | + // from the process object because it has been monkey-patchable. | | 266 | + // TODO(joyeecheung): investigate whether process._fatalException | | 267 | + // can be deprecated. | | 268 | + process._fatalException = onGlobalUncaughtException; | 263 | 269 | process.setUncaughtExceptionCaptureCallback = | 264 | 270 | setUncaughtExceptionCaptureCallback; | 265 | 271 | process.hasUncaughtExceptionCaptureCallback = | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/main/worker_thread.js +13-10Lines changed: 13 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -42,7 +42,7 @@ const { | 42 | 42 | } = workerIo; | 43 | 43 |
| 44 | 44 | const { | 45 | | - fatalException: originalFatalException | | 45 | + onGlobalUncaughtException | 46 | 46 | } = require('internal/process/execution'); | 47 | 47 |
| 48 | 48 | const publicWorker = require('worker_threads'); | @@ -156,24 +156,23 @@ port.on('message', (message) => { | 156 | 156 | } | 157 | 157 | }); | 158 | 158 |
| 159 | | -// Overwrite fatalException | 160 | | -process._fatalException = (error, fromPromise) => { | 161 | | - debug(`[${threadId}] gets fatal exception`); | 162 | | - let caught = false; | | 159 | +function workerOnGlobalUncaughtException(error, fromPromise) { | | 160 | + debug(`[${threadId}] gets uncaught exception`); | | 161 | + let handled = false; | 163 | 162 | try { | 164 | | - caught = originalFatalException.call(this, error, fromPromise); | | 163 | + handled = onGlobalUncaughtException(error, fromPromise); | 165 | 164 | } catch (e) { | 166 | 165 | error = e; | 167 | 166 | } | 168 | | - debug(`[${threadId}] fatal exception caught = ${caught}`); | | 167 | + debug(`[${threadId}] uncaught exception handled = ${handled}`); | 169 | 168 |
| 170 | | - if (!caught) { | | 169 | + if (!handled) { | 171 | 170 | let serialized; | 172 | 171 | try { | 173 | 172 | const { serializeError } = require('internal/error-serdes'); | 174 | 173 | serialized = serializeError(error); | 175 | 174 | } catch {} | 176 | | - debug(`[${threadId}] fatal exception serialized = ${!!serialized}`); | | 175 | + debug(`[${threadId}] uncaught exception serialized = ${!!serialized}`); | 177 | 176 | if (serialized) | 178 | 177 | port.postMessage({ | 179 | 178 | type: ERROR_MESSAGE, | @@ -187,7 +186,11 @@ process._fatalException = (error, fromPromise) => { | 187 | 186 |
| 188 | 187 | process.exit(); | 189 | 188 | } | 190 | | -}; | | 189 | +} | | 190 | + | | 191 | +// Patch the global uncaught exception handler so it gets picked up by | | 192 | +// node::errors::TriggerUncaughtException(). | | 193 | +process._fatalException = workerOnGlobalUncaughtException; | 191 | 194 |
| 192 | 195 | markBootstrapComplete(); | 193 | 196 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/modules/cjs/loader.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -832,7 +832,7 @@ Module.runMain = function() { | 832 | 832 | return loader.import(pathToFileURL(process.argv[1]).pathname); | 833 | 833 | }) | 834 | 834 | .catch((e) => { | 835 | | - internalBinding('task_queue').triggerFatalException( | | 835 | + internalBinding('errors').triggerUncaughtException( | 836 | 836 | e, | 837 | 837 | true /* fromPromise */ | 838 | 838 | ); | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/process/execution.js +9-5Lines changed: 9 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -115,10 +115,14 @@ function noop() {} | 115 | 115 | // and exported to be written to process._fatalException, it has to be | 116 | 116 | // returned as an *anonymous function* wrapped inside a factory function, | 117 | 117 | // otherwise it breaks the test-timers.setInterval async hooks test - | 118 | | -// this may indicate that node::FatalException should fix up the callback scope | 119 | | -// before calling into process._fatalException, or this function should | 120 | | -// take extra care of the async hooks before it schedules a setImmediate. | 121 | | -function createFatalException() { | | 118 | +// this may indicate that node::errors::TriggerUncaughtException() should | | 119 | +// fix up the callback scope before calling into process._fatalException, | | 120 | +// or this function should take extra care of the async hooks before it | | 121 | +// schedules a setImmediate. | | 122 | +function createOnGlobalUncaughtException() { | | 123 | + // The C++ land node::errors::TriggerUncaughtException() will | | 124 | + // exit the process if it returns false, and continue execution if it | | 125 | + // returns true (which indicates that the exception is handled by the user). | 122 | 126 | return (er, fromPromise) => { | 123 | 127 | // It's possible that defaultTriggerAsyncId was set for a constructor | 124 | 128 | // call that threw and was never cleared. So clear it now. | @@ -206,7 +210,7 @@ module.exports = { | 206 | 210 | tryGetCwd, | 207 | 211 | evalModule, | 208 | 212 | evalScript, | 209 | | - fatalException: createFatalException(), | | 213 | + onGlobalUncaughtException: createOnGlobalUncaughtException(), | 210 | 214 | setUncaughtExceptionCaptureCallback, | 211 | 215 | hasUncaughtExceptionCaptureCallback | 212 | 216 | }; |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/process/promises.js +24-24Lines changed: 24 additions & 24 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 |
| 3 | 3 | const { Object } = primordials; | 4 | 4 |
| 5 | | -const { | 6 | | - safeToString | 7 | | -} = internalBinding('util'); | 8 | 5 | const { | 9 | 6 | tickInfo, | 10 | 7 | promiseRejectEvents: { | @@ -13,10 +10,14 @@ const { | 13 | 10 | kPromiseResolveAfterResolved, | 14 | 11 | kPromiseRejectAfterResolved | 15 | 12 | }, | 16 | | - setPromiseRejectCallback, | 17 | | - triggerFatalException | | 13 | + setPromiseRejectCallback | 18 | 14 | } = internalBinding('task_queue'); | 19 | 15 |
| | 16 | +const { | | 17 | + noSideEffectsToString, | | 18 | + triggerUncaughtException | | 19 | +} = internalBinding('errors'); | | 20 | + | 20 | 21 | // *Must* match Environment::TickInfo::Fields in src/env.h. | 21 | 22 | const kHasRejectionToWarn = 1; | 22 | 23 |
| @@ -127,7 +128,7 @@ function handledRejection(promise) { | 127 | 128 |
| 128 | 129 | const unhandledRejectionErrName = 'UnhandledPromiseRejectionWarning'; | 129 | 130 | function emitUnhandledRejectionWarning(uid, reason) { | 130 | | - const warning = getError( | | 131 | + const warning = getErrorWithoutStack( | 131 | 132 | unhandledRejectionErrName, | 132 | 133 | 'Unhandled promise rejection. This error originated either by ' + | 133 | 134 | 'throwing inside of an async function without a catch block, ' + | @@ -139,7 +140,8 @@ function emitUnhandledRejectionWarning(uid, reason) { | 139 | 140 | warning.stack = reason.stack; | 140 | 141 | process.emitWarning(reason.stack, unhandledRejectionErrName); | 141 | 142 | } else { | 142 | | - process.emitWarning(safeToString(reason), unhandledRejectionErrName); | | 143 | + process.emitWarning( | | 144 | + noSideEffectsToString(reason), unhandledRejectionErrName); | 143 | 145 | } | 144 | 146 | } catch {} | 145 | 147 |
| @@ -179,7 +181,9 @@ function processPromiseRejections() { | 179 | 181 | const { reason, uid } = promiseInfo; | 180 | 182 | switch (unhandledRejectionsMode) { | 181 | 183 | case kThrowUnhandledRejections: { | 182 | | - fatalException(reason); | | 184 | + const err = reason instanceof Error ? | | 185 | + reason : generateUnhandledRejectionError(reason); | | 186 | + triggerUncaughtException(err, true /* fromPromise */); | 183 | 187 | const handled = process.emit('unhandledRejection', reason, promise); | 184 | 188 | if (!handled) emitUnhandledRejectionWarning(uid, reason); | 185 | 189 | break; | @@ -209,7 +213,7 @@ function processPromiseRejections() { | 209 | 213 | pendingUnhandledRejections.length !== 0; | 210 | 214 | } | 211 | 215 |
| 212 | | -function getError(name, message) { | | 216 | +function getErrorWithoutStack(name, message) { | 213 | 217 | // Reset the stack to prevent any overhead. | 214 | 218 | const tmp = Error.stackTraceLimit; | 215 | 219 | Error.stackTraceLimit = 0; | @@ -225,21 +229,17 @@ function getError(name, message) { | 225 | 229 | return err; | 226 | 230 | } | 227 | 231 |
| 228 | | -function fatalException(reason) { | 229 | | - let err; | 230 | | - if (reason instanceof Error) { | 231 | | - err = reason; | 232 | | - } else { | 233 | | - err = getError( | 234 | | - 'UnhandledPromiseRejection', | 235 | | - 'This error originated either by ' + | 236 | | - 'throwing inside of an async function without a catch block, ' + | 237 | | - 'or by rejecting a promise which was not handled with .catch().' + | 238 | | - ` The promise rejected with the reason "${safeToString(reason)}".` | 239 | | - ); | 240 | | - err.code = 'ERR_UNHANDLED_REJECTION'; | 241 | | - } | 242 | | - triggerFatalException(err, true /* fromPromise */); | | 232 | +function generateUnhandledRejectionError(reason) { | | 233 | + const message = | | 234 | + 'This error originated either by ' + | | 235 | + 'throwing inside of an async function without a catch block, ' + | | 236 | + 'or by rejecting a promise which was not handled with .catch().' + | | 237 | + ' The promise rejected with the reason ' + | | 238 | + `"${noSideEffectsToString(reason)}".`; | | 239 | + | | 240 | + const err = getErrorWithoutStack('UnhandledPromiseRejection', message); | | 241 | + err.code = 'ERR_UNHANDLED_REJECTION'; | | 242 | + return err; | 243 | 243 | } | 244 | 244 |
| 245 | 245 | function listenForRejections() { | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/process/task_queues.js +8-7Lines changed: 8 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -9,10 +9,13 @@ const { | 9 | 9 | // Used to run V8's micro task queue. | 10 | 10 | runMicrotasks, | 11 | 11 | setTickCallback, | 12 | | - enqueueMicrotask, | 13 | | - triggerFatalException | | 12 | + enqueueMicrotask | 14 | 13 | } = internalBinding('task_queue'); | 15 | 14 |
| | 15 | +const { | | 16 | + triggerUncaughtException | | 17 | +} = internalBinding('errors'); | | 18 | + | 16 | 19 | const { | 17 | 20 | setHasRejectionToWarn, | 18 | 21 | hasRejectionToWarn, | @@ -143,11 +146,9 @@ function runMicrotask() { | 143 | 146 | try { | 144 | 147 | callback(); | 145 | 148 | } catch (error) { | 146 | | - // TODO(devsnek): Remove this if | 147 | | - // https://bugs.chromium.org/p/v8/issues/detail?id=8326 | 148 | | - // is resolved such that V8 triggers the fatal exception | 149 | | - // handler for microtasks. | 150 | | - triggerFatalException(error, false /* fromPromise */); | | 149 | + // runInAsyncScope() swallows the error so we need to catch | | 150 | + // it and handle it here. | | 151 | + triggerUncaughtException(error, false /* fromPromise */); | 151 | 152 | } finally { | 152 | 153 | this.emitDestroy(); | 153 | 154 | } | |
Collapse file Copy file name to clipboardExpand all lines: src/api/exceptions.cc +5-10Lines changed: 5 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -244,17 +244,12 @@ Local<Value> WinapiErrnoException(Isolate* isolate, | 244 | 244 | } | 245 | 245 | #endif | 246 | 246 |
| | 247 | +// Implement the legacy name exposed in node.h. This has not been in fact | | 248 | +// fatal any more, as the user can handle the exception in the | | 249 | +// TryCatch by listening to `uncaughtException`. | | 250 | +// TODO(joyeecheung): deprecate it in favor of a more accurate name. | 247 | 251 | void FatalException(Isolate* isolate, const v8::TryCatch& try_catch) { | 248 | | - // If we try to print out a termination exception, we'd just get 'null', | 249 | | - // so just crashing here with that information seems like a better idea, | 250 | | - // and in particular it seems like we should handle terminations at the call | 251 | | - // site for this function rather than by printing them out somewhere. | 252 | | - CHECK(!try_catch.HasTerminated()); | 253 | | - | 254 | | - HandleScope scope(isolate); | 255 | | - if (!try_catch.IsVerbose()) { | 256 | | - FatalException(isolate, try_catch.Exception(), try_catch.Message()); | 257 | | - } | | 252 | + errors::TriggerUncaughtException(isolate, try_catch); | 258 | 253 | } | 259 | 254 |
| 260 | 255 | } // namespace node |
Collapse file Copy file name to clipboardExpand all lines: src/env.cc +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -666,7 +666,7 @@ void Environment::RunAndClearNativeImmediates() { | 666 | 666 | ref_count++; | 667 | 667 | if (UNLIKELY(try_catch.HasCaught())) { | 668 | 668 | if (!try_catch.HasTerminated()) | 669 | | - FatalException(isolate(), try_catch); | | 669 | + errors::TriggerUncaughtException(isolate(), try_catch); | 670 | 670 |
| 671 | 671 | // We are done with the current callback. Increase the counter so that | 672 | 672 | // the steps below make everything *after* the current item part of | |
Collapse file Copy file name to clipboardExpand all lines: src/js_stream.cc +5-5Lines changed: 5 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -49,7 +49,7 @@ bool JSStream::IsClosing() { | 49 | 49 | Local<Value> value; | 50 | 50 | if (!MakeCallback(env()->isclosing_string(), 0, nullptr).ToLocal(&value)) { | 51 | 51 | if (try_catch.HasCaught() && !try_catch.HasTerminated()) | 52 | | - FatalException(env()->isolate(), try_catch); | | 52 | + errors::TriggerUncaughtException(env()->isolate(), try_catch); | 53 | 53 | return true; | 54 | 54 | } | 55 | 55 | return value->IsTrue(); | @@ -65,7 +65,7 @@ int JSStream::ReadStart() { | 65 | 65 | if (!MakeCallback(env()->onreadstart_string(), 0, nullptr).ToLocal(&value) || | 66 | 66 | !value->Int32Value(env()->context()).To(&value_int)) { | 67 | 67 | if (try_catch.HasCaught() && !try_catch.HasTerminated()) | 68 | | - FatalException(env()->isolate(), try_catch); | | 68 | + errors::TriggerUncaughtException(env()->isolate(), try_catch); | 69 | 69 | } | 70 | 70 | return value_int; | 71 | 71 | } | @@ -80,7 +80,7 @@ int JSStream::ReadStop() { | 80 | 80 | if (!MakeCallback(env()->onreadstop_string(), 0, nullptr).ToLocal(&value) || | 81 | 81 | !value->Int32Value(env()->context()).To(&value_int)) { | 82 | 82 | if (try_catch.HasCaught() && !try_catch.HasTerminated()) | 83 | | - FatalException(env()->isolate(), try_catch); | | 83 | + errors::TriggerUncaughtException(env()->isolate(), try_catch); | 84 | 84 | } | 85 | 85 | return value_int; | 86 | 86 | } | @@ -102,7 +102,7 @@ int JSStream::DoShutdown(ShutdownWrap* req_wrap) { | 102 | 102 | argv).ToLocal(&value) || | 103 | 103 | !value->Int32Value(env()->context()).To(&value_int)) { | 104 | 104 | if (try_catch.HasCaught() && !try_catch.HasTerminated()) | 105 | | - FatalException(env()->isolate(), try_catch); | | 105 | + errors::TriggerUncaughtException(env()->isolate(), try_catch); | 106 | 106 | } | 107 | 107 | return value_int; | 108 | 108 | } | @@ -137,7 +137,7 @@ int JSStream::DoWrite(WriteWrap* w, | 137 | 137 | argv).ToLocal(&value) || | 138 | 138 | !value->Int32Value(env()->context()).To(&value_int)) { | 139 | 139 | if (try_catch.HasCaught() && !try_catch.HasTerminated()) | 140 | | - FatalException(env()->isolate(), try_catch); | | 140 | + errors::TriggerUncaughtException(env()->isolate(), try_catch); | 141 | 141 | } | 142 | 142 | return value_int; | 143 | 143 | } | |
Collapse file Copy file name to clipboardExpand all lines: src/node.cc +3-5Lines changed: 3 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -132,7 +132,6 @@ using options_parser::kDisallowedInEnvironment; | 132 | 132 |
| 133 | 133 | using v8::Boolean; | 134 | 134 | using v8::EscapableHandleScope; | 135 | | -using v8::Exception; | 136 | 135 | using v8::Function; | 137 | 136 | using v8::FunctionCallbackInfo; | 138 | 137 | using v8::HandleScope; | @@ -218,10 +217,9 @@ MaybeLocal<Value> ExecuteBootstrapper(Environment* env, | 218 | 217 | arguments->size(), | 219 | 218 | arguments->data()); | 220 | 219 |
| 221 | | - // If there was an error during bootstrap then it was either handled by the | 222 | | - // FatalException handler or it's unrecoverable (e.g. max call stack | 223 | | - // exceeded). Either way, clear the stack so that the AsyncCallbackScope | 224 | | - // destructor doesn't fail on the id check. | | 220 | + // If there was an error during bootstrap, it must be unrecoverable | | 221 | + // (e.g. max call stack exceeded). Clear the stack so that the | | 222 | + // AsyncCallbackScope destructor doesn't fail on the id check. | 225 | 223 | // There are only two ways to have a stack size > 1: 1) the user manually | 226 | 224 | // called MakeCallback or 2) user awaited during bootstrap, which triggered | 227 | 225 | // _tickCallback(). | |
|