Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible memory leak in Fetch API #68578

Closed
kamdubiel opened this issue Aug 6, 2024 · 5 comments
Closed

Possible memory leak in Fetch API #68578

kamdubiel opened this issue Aug 6, 2024 · 5 comments
Labels
bug Issue was opened via the bug report template. Output (export/standalone) Related to the the output option in `next.config.js`. Performance Anything with regards to Next.js performance.

Comments

@kamdubiel
Copy link

kamdubiel commented Aug 6, 2024

Link to the code that reproduces this issue

https://github.com/kamdubiel/next-fetch

To Reproduce

I created simple reproduction with docker compose. There's simple Express API (just to return random JSON data), K6 for stress test and NextJS 14.2.5 with simple dynamic route with async server component.

  1. Clone the repository: https://github.com/kamdubiel/next-fetch
  2. Run the containers: docker compose up -d
  3. Attach to Next container stats: docker stats next
  4. Run K6 test: docker compose run k6 run /scripts/ewoks.js
  5. Wait for the K6 test to finish - there will be high memory usage even with idling Next server. It will never go down, and in real world app it's causing out of memory exceptions every few days.

Current vs. Expected behavior

Current:
When container starts, it's memory usage is around ~45MB.
After stress test, the memory usage goes up to ~400MB, and stays there forever. Running more tests will increase the idle memory usage.

Expected:
After stress test, when Next is idling, the memory used should drop.

Provide environment information

From Docker container:

Operating System:
  Platform: linux
  Arch: x64
  Version: #39-Ubuntu SMP PREEMPT_DYNAMIC Fri Jul  5 21:49:14 UTC 2024
  Available memory (MB): 63434
  Available CPU cores: 16
Binaries:
  Node: 20.16.0
  npm: 10.8.1
  Yarn: 1.22.22
  pnpm: N/A
Relevant Packages:
  next: 14.2.5 // Latest available version is detected (14.2.5).
  eslint-config-next: 14.2.5
  react: 18.3.1
  react-dom: 18.3.1
  typescript: 5.5.4
Next.js Config:
  output: standalone

Which area(s) are affected? (Select all that apply)

Output (export/standalone), Performance

Which stage(s) are affected? (Select all that apply)

next start (local), Other (Deployed)

Additional context

I tested my reproduction code against different Next ~14 versions, canary releases and NodeJS versions. It looks like all the versions since async server components support are affected.
Also, I tested the same code with axios and cross-fetch, and it works correctly, so for me it looks like the issue is with Fetch caching.
I have the same issue in real-world app hosted in AKS, and it causes out of memory errors every few days - is there any way to make the garbage collector clean up memory allocations more aggressively when using native fetch?

I think I checked all the other similar issues like:
#64212
#54708
But I think that my example is much simpler so I decided to create new issue.

@kamdubiel kamdubiel added the bug Issue was opened via the bug report template. label Aug 6, 2024
@github-actions github-actions bot added Output (export/standalone) Related to the the output option in `next.config.js`. Performance Anything with regards to Next.js performance. labels Aug 6, 2024
@hahmadzadeh
Copy link

We faced this problem in our platform, And we fixed it by downgrading node js to 20.15.1.

@khuezy
Copy link
Contributor

khuezy commented Aug 7, 2024

Hey @kamdubiel I wonder if it's the console.log buffering the string that's causing the high memory usage. What if you removed the log?

@kamdubiel
Copy link
Author

kamdubiel commented Aug 7, 2024

@khuezy I tested without console.log and it has no effect on memory usage.

@hahmadzadeh Thank you for that, indeed 20.15.1 works - I didn't test that one.

Unfortunately 22.6.0 that will become LTS in October is also affected.

@khuezy
Copy link
Contributor

khuezy commented Aug 7, 2024

undici's fetch seems to be a nightmare. Have you notified the node team of the leaks happening to that library? There's this: https://undici.nodejs.org/#/?id=garbage-collection but your repo is consuming the body... so something else in node is leaking 🤷

@samcx
Copy link
Member

samcx commented Aug 7, 2024

@kamdubiel Thank you for submitting an issue and thanks for taking a look everyone!

Since this seems to be a Node issue and not a Next.js issue, I will be converting this to a discussion for further discussion in case anyone else comes across this.

@vercel vercel locked and limited conversation to collaborators Aug 7, 2024
@samcx samcx converted this issue into discussion #68636 Aug 7, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
bug Issue was opened via the bug report template. Output (export/standalone) Related to the the output option in `next.config.js`. Performance Anything with regards to Next.js performance.
Projects
None yet
Development

No branches or pull requests

4 participants