mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 10:28:47 +00:00
## Summary Fixes #24147 - Fixed EventEmitter crash when `removeAllListeners()` is called from within an event handler while a `removeListener` meta-listener is registered - Added undefined check before iterating over listeners array to match Node.js behavior - Added comprehensive regression tests ## Bug Description When `removeAllListeners(type)` was called: 1. From within an event handler 2. While a `removeListener` meta-listener was registered 3. For an event type with no listeners It would crash with: `TypeError: undefined is not an object (evaluating 'this._events')` ## Root Cause The `removeAllListeners` function tried to access `listeners.length` without checking if `listeners` was defined first. When called with an event type that had no listeners, `events[type]` returned `undefined`, causing the crash. ## Fix Added a check `if (listeners !== undefined)` before iterating, matching the behavior in Node.js core: https://github.com/nodejs/node/blob/main/lib/events.js#L768 ## Test plan - ✅ Created regression test in `test/regression/issue/24147.test.ts` - ✅ Verified test fails with `USE_SYSTEM_BUN=1 bun test` (reproduces bug) - ✅ Verified test passes with `bun bd test` (confirms fix) - ✅ Test covers the exact reproduction case from the issue - ✅ Additional tests for edge cases (actual listeners, nested calls) 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Bot <claude-bot@bun.sh> Co-authored-by: Claude <noreply@anthropic.com>