mirror of
https://github.com/oven-sh/bun
synced 2026-02-15 13:22:07 +00:00
Improve thread safety and reduce lock contention in HTTP thread callbacks by making the handoff from HTTP thread to main thread explicit and minimizing work done under lock. HTTP Thread Improvements: - Add fast-path abort check before acquiring lock (atomic read only) - Move duplicate scheduling check before lock (reduces contention) - Keep critical section brief (just data copying) - Handle OOM gracefully under lock - Add ref() before enqueueTaskConcurrent for proper lifetime management Main Thread Improvements: - Dramatically reduce lock holding time (from ~150 lines to ~30 lines) - Copy state out under lock, then release before JS work - Perform all JS interactions without lock (certificate validation, promise resolution) - Add unconditional deref() at start to balance HTTP thread ref() - Re-acquire lock briefly only when needed Reference Counting: - HTTP thread: ref() before each enqueue to main thread - Main thread: unconditional defer deref() at callback start - Prevents use-after-free and reference leaks - Matches existing onWriteRequestDataDrain/resumeRequestDataStream pattern 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>