|
362 | 362 | } |
363 | 363 | } |
364 | 364 |
|
| 365 | + function noop() {} |
| 366 | + |
365 | 367 | function setupProcessFatal() { |
366 | 368 | const async_wrap = process.binding('async_wrap'); |
367 | 369 | // Arrays containing hook flags and ids for async_hook calls. |
|
372 | 374 | kDefaultTriggerAsyncId, kStackLength } = async_wrap.constants; |
373 | 375 |
|
374 | 376 | process._fatalException = function(er) { |
375 | | - var caught; |
376 | | - |
377 | 377 | // It's possible that kDefaultTriggerAsyncId was set for a constructor |
378 | 378 | // call that threw and was never cleared. So clear it now. |
379 | 379 | async_id_fields[kDefaultTriggerAsyncId] = -1; |
380 | 380 |
|
381 | 381 | if (exceptionHandlerState.captureFn !== null) { |
382 | 382 | exceptionHandlerState.captureFn(er); |
383 | | - caught = true; |
384 | | - } |
385 | | - |
386 | | - if (!caught) |
387 | | - caught = process.emit('uncaughtException', er); |
388 | | - |
389 | | - // If someone handled it, then great. otherwise, die in C++ land |
390 | | - // since that means that we'll exit the process, emit the 'exit' event |
391 | | - if (!caught) { |
| 383 | + } else if (!process.emit('uncaughtException', er)) { |
| 384 | + // If someone handled it, then great. otherwise, die in C++ land |
| 385 | + // since that means that we'll exit the process, emit the 'exit' event |
392 | 386 | try { |
393 | 387 | if (!process._exiting) { |
394 | 388 | process._exiting = true; |
|
397 | 391 | } catch (er) { |
398 | 392 | // nothing to be done about it at this point. |
399 | 393 | } |
| 394 | + return false; |
| 395 | + } |
400 | 396 |
|
| 397 | + // If we handled an error, then make sure any ticks get processed |
| 398 | + // by ensuring that the next Immediate cycle isn't empty |
| 399 | + NativeModule.require('timers').setImmediate(noop); |
| 400 | + |
| 401 | + // Emit the after() hooks now that the exception has been handled. |
| 402 | + if (async_hook_fields[kAfter] > 0) { |
| 403 | + const { emitAfter } = NativeModule.require('internal/async_hooks'); |
| 404 | + do { |
| 405 | + emitAfter(async_id_fields[kExecutionAsyncId]); |
| 406 | + } while (async_hook_fields[kStackLength] > 0); |
| 407 | + // Or completely empty the id stack. |
401 | 408 | } else { |
402 | | - // If we handled an error, then make sure any ticks get processed |
403 | | - NativeModule.require('timers').setImmediate(process._tickCallback); |
404 | | - |
405 | | - // Emit the after() hooks now that the exception has been handled. |
406 | | - if (async_hook_fields[kAfter] > 0) { |
407 | | - do { |
408 | | - NativeModule.require('internal/async_hooks').emitAfter( |
409 | | - async_id_fields[kExecutionAsyncId]); |
410 | | - } while (async_hook_fields[kStackLength] > 0); |
411 | | - // Or completely empty the id stack. |
412 | | - } else { |
413 | | - clearAsyncIdStack(); |
414 | | - } |
| 409 | + clearAsyncIdStack(); |
415 | 410 | } |
416 | 411 |
|
417 | | - return caught; |
| 412 | + return true; |
418 | 413 | }; |
419 | 414 | } |
420 | 415 |
|
|
0 commit comments