Skip to content
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

[bug] TCP memory leak caused by aborted client connections when streaming data with node-fetch #1834

Open
AtnesNess opened this issue Jul 11, 2024 · 4 comments
Labels

Comments

@AtnesNess
Copy link

Describe the bug

Node.js version: v18.16.0

OS version: Amazon Linux 2 x86_64

Description: Providing node-fetch response body stream into ctx.body leads to TCP memory leak when client aborts data download half way through.

Actual behavior

TCP memory keeps increasing with every aborted request and never goes down

Expected behavior

TCP memory to recover after streaming is complete/aborted

Code to reproduce

See https://github.com/AtnesNess/koa-node-fetch-issue

Checklist

  • [+] I have searched through GitHub issues for similar issues.
  • [+] I have completely read through the README and documentation.
  • [+] I have tested my code with the latest version of Node.js and this package and confirmed it is still not working.
@AtnesNess AtnesNess added the bug label Jul 11, 2024
@AtnesNess
Copy link
Author

What I found is that stream is being destroyed on client connection abort. However, node-fetch stream abortion doesn't destroy the socket. I could see that by checking heap dump, this reference is not being cleared out with garbage collector since the request is not aborted. Ideally this function should be called on stream destroy.

@AtnesNess
Copy link
Author

AtnesNess commented Jul 12, 2024

Seems like the issue in node-fetch is outlined here node-fetch/node-fetch#1762.

To conclude this issue is reproducible only when koa and nodeFetchV2 used together

@jonathanong
Copy link
Member

most likely, koa's stream pipelining could use some updates as node has introduced new pipelining features that may handle the aborts better

@jonathanong
Copy link
Member

do you know if this happens with native fetch?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants