Title: Racing immediately-resolving Promises leads to memory leak · Issue #51452 · nodejs/node · GitHub
Open Graph Title: Racing immediately-resolving Promises leads to memory leak · Issue #51452 · nodejs/node
X Title: Racing immediately-resolving Promises leads to memory leak · Issue #51452 · nodejs/node
Description: Version 21.5.0 Platform Linux penguin 6.1.55-06877-gc83437f2949f #1 SMP PREEMPT_DYNAMIC Thu Dec 14 19:17:39 PST 2023 x86_64 GNU/Linux Subsystem async_hooks or async/await What steps will reproduce the bug? Run the following code. Failure...
Open Graph Description: Version 21.5.0 Platform Linux penguin 6.1.55-06877-gc83437f2949f #1 SMP PREEMPT_DYNAMIC Thu Dec 14 19:17:39 PST 2023 x86_64 GNU/Linux Subsystem async_hooks or async/await What steps will reproduce ...
X Description: Version 21.5.0 Platform Linux penguin 6.1.55-06877-gc83437f2949f #1 SMP PREEMPT_DYNAMIC Thu Dec 14 19:17:39 PST 2023 x86_64 GNU/Linux Subsystem async_hooks or async/await What steps will reproduce ...
Opengraph URL: https://github.com/nodejs/node/issues/51452
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Racing immediately-resolving Promises leads to memory leak","articleBody":"### Version\r\n\r\n21.5.0\r\n\r\n### Platform\r\n\r\nLinux penguin 6.1.55-06877-gc83437f2949f #1 SMP PREEMPT_DYNAMIC Thu Dec 14 19:17:39 PST 2023 x86_64 GNU/Linux\r\n\r\n### Subsystem\r\n\r\nasync_hooks or async/await\r\n\r\n### What steps will reproduce the bug?\r\n\r\nRun the following code. Failure is quicker and less likely to interfere with system stability if you run with a low heap ceiling like this, but it will fail without...\r\n\r\n```\r\nnode --max-old-space-size=16 test/examples/ephemeralPromiseMemoryLeak.js\r\n```\r\n\r\n```js\r\n//ephemeralPromiseMemoryLeak.js\r\n\r\nasync function promiseValue(value) {\r\n return value;\r\n}\r\n\r\nasync function run() {\r\n for (;;) {\r\n await Promise.race([promiseValue(\"foo\"), promiseValue(\"bar\")]);\r\n }\r\n}\r\n\r\nrun();\r\n```\r\n\r\nAn equivalent OOM is created if you substitute `Promise.any` for `Promise.race`...\r\n\r\n```js\r\nasync function promiseValue(value) {\r\n return value;\r\n}\r\n\r\nasync function run() {\r\n for (;;) {\r\n await Promise.any([promiseValue(\"foo\"), promiseValue(\"bar\")]);\r\n }\r\n}\r\n\r\nrun();\r\n```\r\n\r\n### How often does it reproduce? Is there a required condition?\r\n\r\nIt always fails.\r\n\r\n### What is the expected behavior? Why is that the expected behavior?\r\n\r\nI would expect it not to accumulate references in memory and fail.\r\n\r\n### What do you see instead?\r\n\r\nFails with the following error\r\n\r\n```sh\r\n✗ node test/examples/ephemeralPromiseMemoryLeak.js\r\n\r\n\u003c--- Last few GCs ---\u003e\r\n\r\n[31511:0x6de4330] 39874 ms: Mark-Compact (reduce) 2048.2 (2083.6) -\u003e 2047.3 (2083.9) MB, 1308.67 / 0.00 ms (average mu = 0.071, current mu = 0.001) allocation failure; scavenge might not succeed\r\n\r\n\r\n\u003c--- JS stacktrace ---\u003e\r\n\r\nFATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory\r\n----- Native stack trace -----\r\n\r\n 1: 0xcc062a node::OOMErrorHandler(char const*, v8::OOMDetails const\u0026) [node]\r\n 2: 0x104eb90 v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, v8::OOMDetails const\u0026) [node]\r\n 3: 0x104ee77 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, v8::OOMDetails const\u0026) [node]\r\n 4: 0x126e0b5 [node]\r\n 5: 0x126e58e [node]\r\n 6: 0x12837b6 v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::internal::GarbageCollectionReason, char const*) [node]\r\n 7: 0x12842d9 [node]\r\n 8: 0x12848e8 [node]\r\n 9: 0x19d4311 [node]\r\n[1] 31511 abort (core dumped) node test/examples/ephemeralPromiseMemoryLeak.js\r\n```\r\n\r\n### Additional information\r\n\r\nIf the promiseValue call incorporates an explicit scheduling on the event loop, there is no memory leak...\r\n\r\n```js\r\n// setImmediateNoLeak.js\r\n\r\nfunction promiseValue(value) {\r\n return new Promise((resolve) =\u003e {\r\n setImmediate(() =\u003e resolve(value));\r\n });\r\n}\r\n\r\nasync function run() {\r\n for (;;) {\r\n await Promise.race([promiseValue(\"foo\"), promiseValue(\"bar\")]);\r\n }\r\n}\r\n\r\nrun();\r\n```\r\n\r\nIf the promiseValue call isn't composed via a Promise.race there is no leak...\r\n\r\n```js\r\n// noRaceNoLeak.js\r\n\r\nasync function promiseValue(value) {\r\n return value;\r\n}\r\n\r\nasync function run() {\r\n for (;;) {\r\n await promiseValue(\"foo\");\r\n await promiseValue(\"bar\");\r\n }\r\n}\r\n\r\nrun();\r\n```\r\n\r\nMaybe obviously, but putting it here for completeness, if you don't use an async await loop, but compose the loop itself with setImmediate there is no leak...\r\n\r\n\r\n```js\r\n// noLoopNoLeak.js\r\n\r\nasync function promiseValue(value) {\r\n return value;\r\n}\r\n\r\nasync function doRace() {\r\n await Promise.race([promiseValue(\"foo\"), promiseValue(\"bar\")]);\r\n}\r\n\r\nfunction run() {\r\n doRace().then(() =\u003e setImmediate(run));\r\n}\r\n\r\nrun();\r\n```","author":{"url":"https://github.com/cefn","@type":"Person","name":"cefn"},"datePublished":"2024-01-13T22:50:04.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":12},"url":"https://github.com/51452/node/issues/51452"}
| 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:f7c5cf34-2765-4f9a-c585-b82e4b7daeb3 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CDE2:1171B:116627C:174EB14:6A4D8E73 |
| html-safe-nonce | 6fd5e4c1ac6597d12eac7d43dd8941f9913bd96732234e63dc57808bbbe5cd86 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDREUyOjExNzFCOjExNjYyN0M6MTc0RUIxNDo2QTREOEU3MyIsInZpc2l0b3JfaWQiOiIzMjE5MTc5NTM5MjEzNTUzNzkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 03d487d2a4e6690d18b255fe0b1887dd943ed91c3c20397522c03fc2c943d59b |
| hovercard-subject-tag | issue:2080502320 |
| 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/51452/issue_layout |
| twitter:image | https://opengraph.githubassets.com/298d89319a69901202aa93d5de6cccbbe4d306563ca9108384f4f6fa161ba080/nodejs/node/issues/51452 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/298d89319a69901202aa93d5de6cccbbe4d306563ca9108384f4f6fa161ba080/nodejs/node/issues/51452 |
| og:image:alt | Version 21.5.0 Platform Linux penguin 6.1.55-06877-gc83437f2949f #1 SMP PREEMPT_DYNAMIC Thu Dec 14 19:17:39 PST 2023 x86_64 GNU/Linux Subsystem async_hooks or async/await What steps will reproduce ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | cefn |
| hostname | github.com |
| expected-hostname | github.com |
| None | 06b8a6144231bf3a234f1c2e9993861e07ce98a905912b114aa386c2d7e84b33 |
| 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 | 74e2a9c78f1e38588de4f25885878349d721eedf |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width