Title: Memory leak when using fetch and not consuming the response body · Issue #51162 · nodejs/node · GitHub
Open Graph Title: Memory leak when using fetch and not consuming the response body · Issue #51162 · nodejs/node
X Title: Memory leak when using fetch and not consuming the response body · Issue #51162 · nodejs/node
Description: Version v21.4.0 Platform Linux 6.6.7-arch1-1 #1 SMP PREEMPT_DYNAMIC Thu, 14 Dec 2023 03:45:42 +0000 x86_64 GNU/Linux Subsystem fetch API What steps will reproduce the bug? Execute the following sample script with node --expose-gc: const ...
Open Graph Description: Version v21.4.0 Platform Linux 6.6.7-arch1-1 #1 SMP PREEMPT_DYNAMIC Thu, 14 Dec 2023 03:45:42 +0000 x86_64 GNU/Linux Subsystem fetch API What steps will reproduce the bug? Execute the following sam...
X Description: Version v21.4.0 Platform Linux 6.6.7-arch1-1 #1 SMP PREEMPT_DYNAMIC Thu, 14 Dec 2023 03:45:42 +0000 x86_64 GNU/Linux Subsystem fetch API What steps will reproduce the bug? Execute the following sam...
Opengraph URL: https://github.com/nodejs/node/issues/51162
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Memory leak when using fetch and not consuming the response body","articleBody":"### Version\n\nv21.4.0\n\n### Platform\n\nLinux 6.6.7-arch1-1 #1 SMP PREEMPT_DYNAMIC Thu, 14 Dec 2023 03:45:42 +0000 x86_64 GNU/Linux\n\n### Subsystem\n\nfetch API\n\n### What steps will reproduce the bug?\n\nExecute the following sample script with `node --expose-gc`:\r\n```\r\nconst repro = async () => {\r\n for (let i = 0; i \u003c 1000; i++) {\r\n await fetch('https://raw.githubusercontent.com/nodejs/node/main/doc/changelogs/CHANGELOG_V20.md');\r\n console.log(process.memoryUsage().rss);\r\n }\r\n}\r\n\r\nrepro()\r\n .then(() => console.log('Finished'))\r\n .catch(err => console.log('Error', err));\r\n```\n\n### How often does it reproduce? Is there a required condition?\n\nIt always happen\n\n### What is the expected behavior? Why is that the expected behavior?\n\nThe logged memory usage of the process should be consistent and not increase over every execution of the fetch\n\n### What do you see instead?\n\nThe memory usage of the process increases with every request, and it's not ever garbage collected (Even with calling `global.gc()` on every execution.\n\n### Additional information\n\nIf you add a `.then(r => r.text())` or something else that uses the body, the memory leak goes away. I found a similar issue in the `node-fetch` library (https://github.com/node-fetch/node-fetch/issues/83), but I wasn't able to find this leak in nodejs itself. \r\n\r\nFor a workaround, I found that using a Finalization Register, I can create a wrapper function that detects evictions of unneeded response objects, and, if the body was unused, empty the body like this:\r\n```\r\nconst registry = new FinalizationRegistry(response => {\r\n if (!response || response.bodyUsed) return;\r\n response.arrayBuffer().catch(console.log);\r\n});\r\n```","author":{"url":"https://github.com/mcat95","@type":"Person","name":"mcat95"},"datePublished":"2023-12-15T08:12:23Z","url":"https://github.com/nodejs/node/issues/51162"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:346f14d0-0dbd-9c71-ba11-785916d40f89 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8E5A:2B774B:5B6A58:80BABF:6B041840 |
| html-safe-nonce | 7e22340cd967d65b027e4eefd5e9cbf89a66346f446e4fdbe8900fbd0211a89e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RTVBOjJCNzc0Qjo1QjZBNTg6ODBCQUJGOjZCMDQxODQwIiwidmlzaXRvcl9pZCI6Ijg1NjIwOTc2MzUxNDQ3Njc1NTIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | c9daf115aeeee01997446e70b48d1f7616015ae7f2d81b9637c87b19f574a350 |
| hovercard-subject-tag | issue:2043115029 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/nodejs/node/51162/issue_layout |
| twitter:image | https://opengraph.githubassets.com/54bb125f4e01811ed7008ddb97dacdf934805481e237cf9703bba0d537b28842/nodejs/node/issues/51162 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/54bb125f4e01811ed7008ddb97dacdf934805481e237cf9703bba0d537b28842/nodejs/node/issues/51162 |
| og:image:alt | Version v21.4.0 Platform Linux 6.6.7-arch1-1 #1 SMP PREEMPT_DYNAMIC Thu, 14 Dec 2023 03:45:42 +0000 x86_64 GNU/Linux Subsystem fetch API What steps will reproduce the bug? Execute the following sam... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mcat95 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/nodejs/node git https://github.com/nodejs/node.git |
| octolytics-dimension-user_id | 9950313 |
| octolytics-dimension-user_login | nodejs |
| octolytics-dimension-repository_id | 27193779 |
| octolytics-dimension-repository_nwo | nodejs/node |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 27193779 |
| octolytics-dimension-repository_network_root_nwo | nodejs/node |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width