koajs / koa

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

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

Open AtnesNess opened 4 months ago

AtnesNess commented 4 months 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 4 months 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 4 months 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

jonathanong commented 3 months ago

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

jonathanong commented 3 months ago

do you know if this happens with native fetch?