Bisecting 107 Next.js canaries to the line that leaked 2 GB
Quick answer
Next.js 16.3.0 to 16.3.4 kept about 2 MB of memory per request on routes using cacheComponents and "use cache" (vercel/next.js#97938, fixed in 16.3.5). I bisected it to the release itself: 16.3.0-canary.107 is clean, 16.3.0 leaks, and the cause is #96426, which removed renderSignal from an AbortSignal.any() composite. Putting that one entry back in a built 16.3.0 drops the route from +2,010 to +0.56 MB per 1,000 requests.
The leak, measured
The issue already said 16.3.x leaked and 16.2.6 did not. What it never pinned down was which change did it, and the comments went back and forth on mechanisms. I wanted the change, not another theory, so I measured releases instead of reading diffs.
The reproduction is the one attached to the issue: one dynamic [slug] route, a per-slug "use cache" scope of about 565 KB, cacheComponents: true and output: "standalone". Every version was measured on Node 24.22 with the same command, cycling through 50 slugs, with a forced GC before every sample:
npx next-leak . --routes '/[slug]' --requests 300 --connections 8 --cycles 4 --idle 8 --max-old-space 3072
| Next.js | Verdict | Retained growth |
|---|---|---|
| 16.2.6 | no leak | +0.58 MB / 1,000 req |
| 16.2.12 | no leak | +0.58 MB / 1,000 req |
| 16.3.0 | leak | +2,010 MB / 1,000 req |
| 16.3.3 | leak | +2,008 MB / 1,000 req |
Those numbers are after a forced collection, so this is memory no GC gives back. On 16.3.0 the heap went 46 → 322 → 616 → 909 → 1,201 MB across four cycles of 300 requests. A production server runs no forced GCs and serves more than 300 requests, so it gets there faster. If you want the protocol behind those numbers, it's the one from how to prove a Next.js memory leak.
Stable releases give the wrong kind of answer
next-leak bisect installs each version into its own copy of the app, builds it, measures the route and halves the range:
npx next-leak bisect . --next 16.2.6..16.3.3 --routes '/[slug]'
It answered in five measurements: last good 16.2.12, first bad 16.3.0. Correct, and useless. 16.2.x patches come from a backport branch cut at 16.2.0, so v16.2.12...v16.3.0 on GitHub is the whole 16.3 development cycle. I counted it locally: 1,728 commits.
The canaries are where that cycle actually happened. There were 107 of them between those two versions, so seven more builds were enough:
| Version | Verdict |
|---|---|
| 16.3.0-canary.52 | no leak |
| 16.3.0-canary.80 | no leak |
| 16.3.0-canary.93 | no leak |
| 16.3.0-canary.101 | no leak |
| 16.3.0-canary.104 | no leak |
| 16.3.0-canary.106 | no leak |
| 16.3.0-canary.107 | no leak (+0.55 MB / 1,000 req) |
| 16.3.0 | leak |
The leak was in no canary at all. It came with the release: 16 commits between canary.107 and v16.3.0, mostly docs, tests and CI. One of them, #96426 ("Make caches error if called after prerender aborts"), touches exactly the code path cacheComponents puts this route on.
That's the non-obvious part, and the reason I now bisect canaries by default: across a minor release, the stable answer is a haystack, and the canaries are where the needle lives.
Proving the line instead of guessing it
#96426 does two things in use-cache-wrapper:
- It returns early, with a promise that never settles, when a
"use cache"call starts after the prerender was aborted. - It drops
outerWorkUnitStore.renderSignalfrom the signals passed toAbortSignal.any()for the inner prerender of the cache entry.
Building Next.js from source to test each half takes a while, so I didn't. I reverted the halves directly in the compiled dist of 16.3.0 (both the CommonJS and the ESM copy), rebuilt the app, checked that the built chunk carried the change, and measured again with the same command:
| 16.3.0 with… | Verdict | Retained growth |
|---|---|---|
| nothing reverted | leak | +2,010 MB / 1,000 req |
| both halves reverted | no leak | +0.56 MB / 1,000 req |
only renderSignal restored | no leak | +0.56 MB / 1,000 req |
| only the early return disabled | leak | +2,011 MB / 1,000 req |
The early return is harmless. The missing renderSignal is the leak.
Why one signal is worth 2 MB a request
AbortSignal.any([a, b]) returns a composite that aborts when any source does. React adds an abort listener to the signal it receives. While a source can still abort, the composite has to stay alive, and with it React's listener and everything that listener closes over: the render state of that request.
Before #96426, renderSignal was one of the sources. The render ended, the composite aborted, everything was released. Without it, two sources were left: a timeout that's cleared when the prerender succeeds, and the dynamic-access signal, which on this route never aborted. So every request left behind a composite that could never fire, holding its render.
The fix that shipped in 16.3.5 (#98448, the backport of #97476) takes the other road. renderSignal stays out, and the timeout controller is aborted once the prerender finishes, with the comment "Release React's listener from the composite signal." Same mechanism, closed from the other side. That match is what convinces me the diagnosis holds.
On Node 22 it's worse, and the heap cap never sees it
I ran the same 16.3.0 build on Node 22.20 to check whether the Node version changed anything. It did. The memory grew outside the V8 heap, so --max-old-space-size=3072 never fired. The process reached 13.7 GB resident and the kernel's out-of-memory killer ended it:
Memory cgroup out of memory: Killed process 9191 (next-server (v1) total-vm:25695260kB, anon-rss:13751124kB
In a container that's exit code 137, with no "JavaScript heap out of memory" line anywhere. If you're sizing pods by heap limit, this is the case that gets past you. next-leak used to report that death as a failed measurement, which stopped the bisect at its first version. It now reads the memory cgroup's OOM-kill counter and reports a process killed that way as a leak.
What to do with this
- On 16.3.0–16.3.4 with
cacheComponents: upgrade to 16.3.5 or later. Nothing else to do. - Still growing on 16.3.5+? Then it's a different leak; three others I measured in production are a good place to start.
- Before blaming a version, measure the route, and only then bisect:
npx next-leak .npx next-leak bisect . --next <good>..<bad> --routes <route>
The bisect with the canary step and the OOM detection ship in next-leak 0.18.0. The workspaces, the runs and bisect.json from everything above are kept, so any number here can be checked against the run that produced it.
