|
| 1 | +'use strict'; |
| 2 | +// Tests that the enhanced EventEmitter stack trace (containing the |
| 3 | +// "Emitted 'error' event at:" frame) is visible to both |
| 4 | +// uncaughtExceptionMonitor and uncaughtException handlers. |
| 5 | +// |
| 6 | +// Also verifies that the enhancement is NOT applied twice on the fatal |
| 7 | +// exit path (i.e., no double "Emitted 'error' event at:" frame in crash |
| 8 | +// output when there is no handler). |
| 9 | + |
| 10 | +const common = require('../common'); |
| 11 | +const assert = require('node:assert'); |
| 12 | +const { spawnSync } = require('node:child_process'); |
| 13 | +const EventEmitter = require('node:events'); |
| 14 | + |
| 15 | +// --- Test 1 & 2: uncaughtExceptionMonitor + uncaughtException --- |
| 16 | +// Both handlers must see the enhanced stack before the process would exit. |
| 17 | +{ |
| 18 | + class CustomEmitter extends EventEmitter {} |
| 19 | + |
| 20 | + const ee = new EventEmitter(); |
| 21 | + const customEE = new CustomEmitter(); |
| 22 | + |
| 23 | + let monitorCount = 0; |
| 24 | + let handlerCount = 0; |
| 25 | + |
| 26 | + process.on('uncaughtExceptionMonitor', common.mustCall((err, origin) => { |
| 27 | + assert.strictEqual(origin, 'uncaughtException'); |
| 28 | + monitorCount++; |
| 29 | + if (monitorCount === 1) { |
| 30 | + // Plain EventEmitter - frame must mention the emit call site |
| 31 | + assert.match(err.stack, /Emitted 'error' event at:/, |
| 32 | + 'Monitor: plain EE stack must be enhanced'); |
| 33 | + assert.match(err.stack, /at emitPlainError/, |
| 34 | + 'Monitor: plain EE stack must include emitPlainError frame'); |
| 35 | + } else if (monitorCount === 2) { |
| 36 | + // Subclass EventEmitter - frame must include the class name |
| 37 | + assert.match(err.stack, /Emitted 'error' event on CustomEmitter instance at:/, |
| 38 | + 'Monitor: subclass stack must be enhanced with class name'); |
| 39 | + assert.match(err.stack, /at emitSubclassError/, |
| 40 | + 'Monitor: subclass stack must include emitSubclassError frame'); |
| 41 | + } |
| 42 | + }, 2)); |
| 43 | + |
| 44 | + process.on('uncaughtException', common.mustCall((err, origin) => { |
| 45 | + assert.strictEqual(origin, 'uncaughtException'); |
| 46 | + handlerCount++; |
| 47 | + if (handlerCount === 1) { |
| 48 | + assert.match(err.stack, /Emitted 'error' event at:/, |
| 49 | + 'Handler: plain EE stack must be enhanced'); |
| 50 | + assert.match(err.stack, /at emitPlainError/, |
| 51 | + 'Handler: plain EE stack must include emitPlainError frame'); |
| 52 | + // Schedule second throw for next tick after this handler returns |
| 53 | + process.nextTick(emitSubclassError); |
| 54 | + } else if (handlerCount === 2) { |
| 55 | + assert.match(err.stack, /Emitted 'error' event on CustomEmitter instance at:/, |
| 56 | + 'Handler: subclass stack must be enhanced with class name'); |
| 57 | + assert.match(err.stack, /at emitSubclassError/, |
| 58 | + 'Handler: subclass stack must include emitSubclassError frame'); |
| 59 | + } |
| 60 | + }, 2)); |
| 61 | + |
| 62 | + function emitPlainError() { |
| 63 | + ee.emit('error', new Error('plain error')); |
| 64 | + } |
| 65 | + |
| 66 | + function emitSubclassError() { |
| 67 | + customEE.emit('error', new Error('subclass error')); |
| 68 | + } |
| 69 | + |
| 70 | + emitPlainError(); |
| 71 | +} |
| 72 | + |
| 73 | +// --- Test 3: No handler - fatal exit path must NOT double-apply the frame --- |
| 74 | +// This is the critical regression test: if the C++ ReportFatalException path |
| 75 | +// also calls enhance_fatal_stack_before_inspector after we already enhanced, |
| 76 | +// the "Emitted 'error' event at:" frame would appear twice in the crash output. |
| 77 | +{ |
| 78 | + const script = ` |
| 79 | + const EventEmitter = require('node:events'); |
| 80 | + const ee = new EventEmitter(); |
| 81 | + function emitError() { ee.emit('error', new Error('crash')); } |
| 82 | + emitError(); |
| 83 | + `; |
| 84 | + const result = spawnSync(process.execPath, ['--eval', script], { timeout: 5000 }); |
| 85 | + |
| 86 | + // Process must have exited with non-zero due to unhandled error |
| 87 | + assert.notStrictEqual(result.status, 0); |
| 88 | + |
| 89 | + const stderr = result.stderr.toString(); |
| 90 | + |
| 91 | + // The enhancement must appear - otherwise the fix regressed |
| 92 | + assert.match(stderr, /Emitted 'error' event at:/, |
| 93 | + 'Fatal path: enhanced frame must appear in crash output'); |
| 94 | + |
| 95 | + // The enhancement must appear exactly ONCE - the double-call bug would |
| 96 | + // cause it to appear twice |
| 97 | + const occurrences = (stderr.match(/Emitted 'error' event at:/g) || []).length; |
| 98 | + assert.strictEqual(occurrences, 1, |
| 99 | + `Fatal path: enhanced frame must appear exactly once, got ${occurrences}`); |
| 100 | +} |
| 101 | + |
| 102 | +// --- Test 4: Non-Error EventEmitter emit must not crash --- |
| 103 | +// When ee.emit('error', nonError) is called with a non-Error value, |
| 104 | +// kEnhanceStackBeforeInspector won't be present on the thrown value. |
| 105 | +// The guard (typeof er[kEnhanceStackBeforeInspector] === 'function') must |
| 106 | +// prevent any TypeError. |
| 107 | +{ |
| 108 | + process.once('uncaughtException', common.mustCall((err) => { |
| 109 | + // Err is a plain object here - no stack enhancement expected |
| 110 | + assert.strictEqual(err.message, 'non-error-throw'); |
| 111 | + })); |
| 112 | + |
| 113 | + process.nextTick(() => { |
| 114 | + const thrower = new EventEmitter(); |
| 115 | + thrower.emit('error', new Error('non-error-throw')); |
| 116 | + }); |
| 117 | +} |
0 commit comments