koajs / koa

Expressive middleware for node.js using ES2017 async functions
https://koajs.com
MIT License
35.07k stars 3.22k forks source link

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

Open AtnesNess opened 1 month ago

AtnesNess commented 1 month ago

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

AtnesNess commented 1 month ago

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 commented 1 month ago

Seems like the issue in node-fetch is outlined here https://github.com/node-fetch/node-fetch/issues/1762.

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