|
409 | 409 | } |
410 | 410 | } |
411 | 411 |
|
| 412 | + function noop() {} |
| 413 | + |
412 | 414 | function setupProcessFatal() { |
413 | 415 | const async_wrap = process.binding('async_wrap'); |
414 | 416 | // Arrays containing hook flags and ids for async_hook calls. |
|
419 | 421 | kDefaultTriggerAsyncId, kStackLength } = async_wrap.constants; |
420 | 422 |
|
421 | 423 | process._fatalException = function(er) { |
422 | | - var caught; |
423 | | - |
424 | 424 | // It's possible that kDefaultTriggerAsyncId was set for a constructor |
425 | 425 | // call that threw and was never cleared. So clear it now. |
426 | 426 | async_id_fields[kDefaultTriggerAsyncId] = -1; |
427 | 427 |
|
428 | 428 | if (exceptionHandlerState.captureFn !== null) { |
429 | 429 | exceptionHandlerState.captureFn(er); |
430 | | - caught = true; |
431 | | - } |
432 | | - |
433 | | - if (!caught) |
434 | | - caught = process.emit('uncaughtException', er); |
435 | | - |
436 | | - // If someone handled it, then great. otherwise, die in C++ land |
437 | | - // since that means that we'll exit the process, emit the 'exit' event |
438 | | - if (!caught) { |
| 430 | + } else if (!process.emit('uncaughtException', er)) { |
| 431 | + // If someone handled it, then great. otherwise, die in C++ land |
| 432 | + // since that means that we'll exit the process, emit the 'exit' event |
439 | 433 | try { |
440 | 434 | if (!process._exiting) { |
441 | 435 | process._exiting = true; |
|
444 | 438 | } catch (er) { |
445 | 439 | // nothing to be done about it at this point. |
446 | 440 | } |
| 441 | + return false; |
| 442 | + } |
447 | 443 |
|
| 444 | + // If we handled an error, then make sure any ticks get processed |
| 445 | + // by ensuring that the next Immediate cycle isn't empty |
| 446 | + NativeModule.require('timers').setImmediate(noop); |
| 447 | + |
| 448 | + // Emit the after() hooks now that the exception has been handled. |
| 449 | + if (async_hook_fields[kAfter] > 0) { |
| 450 | + const { emitAfter } = NativeModule.require('internal/async_hooks'); |
| 451 | + do { |
| 452 | + emitAfter(async_id_fields[kExecutionAsyncId]); |
| 453 | + } while (async_hook_fields[kStackLength] > 0); |
| 454 | + // Or completely empty the id stack. |
448 | 455 | } else { |
449 | | - // If we handled an error, then make sure any ticks get processed |
450 | | - NativeModule.require('timers').setImmediate(process._tickCallback); |
451 | | - |
452 | | - // Emit the after() hooks now that the exception has been handled. |
453 | | - if (async_hook_fields[kAfter] > 0) { |
454 | | - do { |
455 | | - NativeModule.require('internal/async_hooks').emitAfter( |
456 | | - async_id_fields[kExecutionAsyncId]); |
457 | | - } while (async_hook_fields[kStackLength] > 0); |
458 | | - // Or completely empty the id stack. |
459 | | - } else { |
460 | | - clearAsyncIdStack(); |
461 | | - } |
| 456 | + clearAsyncIdStack(); |
462 | 457 | } |
463 | 458 |
|
464 | | - return caught; |
| 459 | + return true; |
465 | 460 | }; |
466 | 461 | } |
467 | 462 |
|
|
0 commit comments