mirror of
https://github.com/oven-sh/bun
synced 2026-02-02 15:08:46 +00:00
### What does this PR do? Fixes a bug where idle WebSocket connections would cause 100% CPU usage on macOS and other BSD systems using kqueue. **Root cause:** The kqueue event filter comparison was using bitwise AND (`&`) instead of equality (`==`) when checking the filter type. Combined with missing `EV_ONESHOT` flags on writable events, this caused the event loop to continuously spin even when no actual I/O was pending. **Changes:** 1. **Fixed filter comparison** in `epoll_kqueue.c`: Changed `filter & EVFILT_READ` to `filter == EVFILT_READ` (same for `EVFILT_WRITE`). The filter field is a value, not a bitmask. 2. **Added `EV_ONESHOT` flag** to writable events: kqueue writable events now use one-shot mode to prevent continuous triggering. 3. **Re-arm writable events when needed**: After a one-shot writable event fires, the code now properly updates the poll state and re-arms the writable event if another write is still pending. ### How did you verify your code works? Added a test that: 1. Creates a TLS WebSocket server and client 2. Sends messages then lets the connection sit idle 3. Measures CPU usage over 3 seconds 4. Fails if CPU usage exceeds 2% (expected is ~0.XX% when idle)