Files
bun.sh/src/js/node
robobun 4f1b90ad1d Fix EventEmitter crash in removeAllListeners with removeListener meta-listener (#24148)
## 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>
2025-10-28 12:32:15 -07:00
..
2025-05-31 16:21:08 -07:00
2025-01-16 22:40:39 -08:00
2025-09-01 16:20:13 -07:00
2025-01-16 22:40:39 -08:00
2025-04-19 05:41:34 -07:00
2025-04-19 05:41:34 -07:00
2025-03-20 00:45:44 -07:00
2025-09-01 16:20:13 -07:00
2025-04-19 05:41:34 -07:00
2025-08-27 20:34:37 -07:00
2024-08-09 19:28:08 -07:00
2025-09-01 16:20:13 -07:00
2025-04-19 05:41:34 -07:00
2025-01-16 22:40:39 -08:00
2025-09-01 16:20:13 -07:00
2025-09-01 16:20:13 -07:00
2025-06-22 20:51:45 -07:00