Commit 4a3ffb7
fix(sse): fix zombie SSE connection memory leak
Root cause: adapter.bun.ts explicitly set idleTimeout: 0, disabling
Bun's idle timeout entirely. When connections enter TCP CLOSE_WAIT
(reverse proxy or Electron shell swallowing client FIN), the server
has no signal that the connection is dead — three resource classes
leak permanently: AsyncQueue grows without bound (14 MB/s, peak
24.5 GB), setInterval heartbeats keep ticking, Bus subscriptions
are never cancelled.
Five changes:
- server/adapter.bun.ts: idleTimeout: 0 → 120. Heartbeat interval
is 10s so active connections reset the timer on every beat and are
never killed; dead connections are cleaned up within 120s
- server/instance/event.ts: register c.req.raw.signal abort listener
as a second cleanup path independent of responseReadable.cancel().
Hono skips this listener on Bun 1.2+ (isOldBunVersion gate); we
add it unconditionally. writeSSE try/catch kept for forward
compatibility — Hono 4.x write() has an empty catch so this block
does not fire on the current version
- server/instance/global.ts: same two changes applied to streamEvents()
- util/queue.ts: add capacity limit (default 1000), drop oldest entry
on overflow — defensive backstop so growth is bounded even if a
cleanup signal is missed
- bus/global.ts: setMaxListeners(100) — fixes #22422
MaxListenersExceededWarning; each SSE connection adds one listener,
default limit of 10 is too low for normal concurrent usage
Fixes #22198, fixes #22422
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>1 parent 5b60e51 commit 4a3ffb7
5 files changed
Lines changed: 46 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
15 | 19 | | |
16 | 20 | | |
17 | 21 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
78 | 84 | | |
79 | 85 | | |
80 | 86 | | |
81 | 87 | | |
82 | | - | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
83 | 97 | | |
84 | 98 | | |
85 | 99 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
60 | 66 | | |
61 | 67 | | |
62 | 68 | | |
63 | 69 | | |
64 | | - | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
65 | 79 | | |
66 | 80 | | |
67 | 81 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
| 6 | + | |
5 | 7 | | |
6 | 8 | | |
7 | | - | |
8 | | - | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
9 | 17 | | |
10 | 18 | | |
11 | 19 | | |
| |||
0 commit comments