Memory leak on async calls in dev server
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 142k
- Forks
- 32.5k
- Avg merge
- 2d 14h
- Merged PRs (30d)
- 351
Description
Link to the code that reproduces this issue
https://github.com/pontasan/nextjs_case20251101_2.git
To Reproduce
1.Start the server with npm run dev.
2.Open the following link in your browser: http://localhost:3000/test
3.After a while, the server crashes while GC logs are being printed.
Current vs. Expected behavior
It appears that a memory leak occurs only in the development server when calling async functions from route.ts.
Running a large number of async functions in a loop results in a steady increase in heap usage over time.
In contrast, there is no such issue in the production build.
I noticed that when running the development server for an extended period, the heap usage never decreases, which led me to conduct this experiment.
This example is intentionally extreme to make the issue easier to reproduce, but even in development mode, I believe it should be able to handle asynchronous function calls inside a loop just as it does in production.
import v8 from 'v8'
export async function GET(request: Request) {
v8.setFlagsFromString('--trace_gc')
for (; ;) { // This is an intentionally extreme example to make the issue easier to reproduce.
await testFunc()
}
return Response.json({})
}
async function testFunc() {
// NOP
}
The following image was taken right after the development server crashed.
The issue does not reproduce in the production build or in the following Node.js code, and the heap remains stable.
const v8 = require('node:v8')
v8.setFlagsFromString('--trace_gc')
async function testFunc() {
// NOP
}
(async () => {
for (; ;) {
await testFunc()
}
})()
On the other hand, in the production build environment, the heap remains stable.
Running the same test in Node.js also shows stable behavior.
Provide environment information
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 21.6.0: Mon Aug 22 20:19:52 PDT 2022; root:xnu-8020.140.49~2/RELEASE_ARM64_T6000
Available memory (MB): 65536
Available CPU cores: 10
Binaries:
Node: 24.11.0
npm: 11.6.1
Yarn: N/A
pnpm: N/A
Relevant Packages:
next: 16.0.2-canary.3 // Latest available version is detected (16.0.2-canary.3).
eslint-config-next: N/A
react: 19.2.0
react-dom: 19.2.0
typescript: 5.9.3
Next.js Config:
output: N/A
The same issue occurs not only in 16.0.2-canary.3, but also in 16.0.1.
It also occurs in Node.js v22.21.1.
Which area(s) are affected? (Select all that apply)
Route Handlers
Which stage(s) are affected? (Select all that apply)
next dev (local)
Additional context
Below is a video of the development server.
The GC logs show a steady increase in memory consumption.
https://drive.google.com/file/d/1_Z_2UWRbj1IWBtgIwCb7nQcfnZAdGwaw/view?usp=drive_link
Below is a video of the production build.
The heap usage remains stable.
https://drive.google.com/file/d/1byrJM7SMAw_vodNmji6zxkrpM6BrgG2d/view?usp=drive_link
Below is a video of the same process running in Node.js, where the heap also remains stable.
https://drive.google.com/file/d/1PKZdfE_0eCR2QMG8aANzc7RxglOHfPQM/view?usp=drive_link
The test code can be found at the following link:
https://github.com/pontasan/nextjs_case20251101_2.git
The Next.js example is located at: /app/test/route.ts
The Node.js example is located at: /test.js
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the reproduction repository and compare app/test/route.ts with test.js, running npm run dev and the production build to confirm the development-only behavior. Investigate the dev server's handling of the Route Handler's repeated async calls, using the supplied GC output as evidence; done means memory remains stable in development as it does in production and Node.js.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- nextjs, node.js, typescript
- Domain
- api, backend, devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100