mirror of
https://github.com/oven-sh/bun
synced 2026-02-14 04:49:06 +00:00
This fixes a bug introduced in commit 9bb4a6af19 where the uSockets sweep
timer optimization could cause CLOSE_WAIT connections to accumulate when
using reverse proxies like nginx.
**Problem**: The sweep timer enable/disable functions used reference counting
but could decrement the counter below zero when socket unlinking occurred
more frequently than linking. This caused the sweep timer to be disabled
prematurely, preventing proper cleanup of idle connections.
**Root Cause**: `us_internal_disable_sweep_timer()` was called without
checking if the counter was already zero, causing negative counts that
broke the timer lifecycle.
**Solution**: Add bounds checking to prevent the sweep_timer_count from
going negative in `us_internal_disable_sweep_timer()`.
**Impact**:
- Prevents CLOSE_WAIT connection accumulation with nginx reverse proxies
- Maintains proper cleanup of idle TCP sockets
- Preserves the performance benefits of the original optimization
**Testing**: Added regression tests that verify rapid connection churn
doesn't break server responsiveness and idle connections are handled properly.
Fixes user report: "After a few hours, my server fills up with hundreds
of CLOSE_WAIT connections and never clears until I restart Bun."
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>