labs
한국어 hello@taklabs.dev

An MCP daemon that ate 9GB in 6 days was waiting on a response that never ends

The /mcp bridge waited on never-ending subscriptions/listen SSE responses with arrayBuffer(), and abandoned subscriptions piled keep-alive frames onto the heap. I made it stream responses and abort the request on socket close.

What looked wrong?

I keep a qmd mcp --http daemon running locally, and it had been up for six days. In ps it looked fine, with an RSS of about 135MB. Footprint and top told a different story: 9170MB in total, 8858MB of it compressed memory. No wonder the machine kept swapping.

The daemon is meant to stay up for a long time, so a restart wasn't a fix. I wanted to trace the growth through the records before touching anything.

What did the records show?

The daemon writes an hourly health log, and I started there. Two numbers climbed together: open listens went from 58 at hour 1 to 1024 at hour 138, and heap_used went from 28MB to 2279MB. When the heap rises in step with the count of open listen streams, the leak is almost certainly in how subscriptions/listen is handled.

hour 1 hour 138 0 500 1000 1500 2000 MB heap_used in MB, hour 1 vs hour 138

When I measured, lsof showed no real TCP connections, yet /health reported 1024 listens and 1024 MCP requests in flight. Every client had gone, and the server still believed they were all there.

The MCP SDK I pin, version 2.3.1, sets DEFAULT_MAX_SUBSCRIPTIONS to 1024 in its bundle and refuses any new listen past that cap. So on top of eating memory, the daemon had at some point stopped accepting new subscriptions at all.

Why weren't subscriptions released?

Reading the /mcp handler in src/mcp/server.ts, I found two problems stacked on each other.

The bridge buffered the whole SDK response with await response.arrayBuffer() before handing it to the Node response. That works for an ordinary JSON-RPC reply, but subscriptions/listen is SSE and never ends, so the bridge waited forever to finish reading an endless body and nothing ever reached the client. The in-flight counter was only decremented after that await settled, so it never came down.

The Request passed to the SDK also carried no abort signal. The SDK closes a listen stream only on cancel, abort, or an explicit teardown, and nothing told it that the client socket had closed. The subscription just stayed.

For each orphaned subscription the SDK produced a keep-alive frame every 15 seconds, and those frames piled up in a buffer nobody read. The heap grew to 2279MB, and most of the rest of the 9170MB footprint was compressed memory.

What did I change?

pipeline(Readable.fromWeb(body), nodeRes)

The response body now streams instead of being buffered, so SSE frames go out to the client as they are produced and have nowhere to accumulate on the heap.

I also created an AbortController and attached its signal to the Request. It aborts when a close event fires on nodeRes or the socket. The SDK tears down a listen when request.signal aborts, so when a client leaves, its subscription and keep-alive timer are cleaned up together.

Before After SDK listen stream arrayBuffer() waits forever Keep-alive frames pile up on heap SDK listen stream pipeline streams frames out nodeRes or socket close AbortController aborts request SDK tears down listen

The diff is small: 106 lines added, 1 removed, most of it tests for the release behavior. Before wrapping up I ran the simplify skill once to tidy it.

How did I verify it?

I first confirmed that the new test failed before the fix (a 30-second timeout) and passed after. A test that can't reproduce the bug says nothing about the fix.

Local CI showed oxlint at zero, tsc -p tsconfig.build.json at zero errors, and the lex gate passing. The full vitest run had 2252 passing and 1 failing. The failure was the "batch is faster than sequential" timing assertion in llm.test.ts (1704ms ≤ 306ms). That test embeds with a real model and has nothing to do with this change, and the machine was in a swap thrash caused by this very leak when it ran. I noted the failure in the PR. The full bun run was 2250 passing, 3 skipped, 0 failing. I skipped the electron smoke test because it's unrelated to the change and needs electron installed.

What limits remain?

I couldn't verify the release test on Bun. Bun 1.3's node:http server emits no event at all when a client disconnects from a POST whose body has already been read; I measured that myself. With no event, nothing triggers the abort, so I skipped the test on Bun with skipIf. The daemon runs on Node, so I judged there's no impact in practice, but this is the spot to revisit if Bun's behavior changes.

What did I learn?

Had I looked only at RSS in ps, I would have waved through a healthy 135MB process. The real size only showed up in a footprint that counts compressed memory. The health log already recorded listens next to heap_used, so seeing those two curves rise together narrowed the search right away.

When a counter stalls at a round number like 1024, look for a limit first. Here it was the SDK's subscription cap, and finding it showed that the leak was also an outage, because new listens were being refused.

The work took two sessions and cost a little under $3.

Build stats

ModelInputOutputCache readCache writeUSD
claude-opus-5-513027,8278,034,12867,501$2.70
claude-sonnet-5-561,61040,81125,086$0.08
Total13629,4378,074,93992,587$2.78
claude-opus-5-58,129,586claude-sonnet-5-567,513
Tokens by model
simplify1
Skill calls

Counted from 2 session transcripts at 2026-11-15T01:32Z (PR #177, sessions 2026-11-14 to 2026-11-14). Tokens include subagents; USD is list price. Code: +106 / −1 lines.