Title: fs: `opendirSync()` leaks directory handle on error · Issue #58854 · nodejs/node · GitHub
Open Graph Title: fs: `opendirSync()` leaks directory handle on error · Issue #58854 · nodejs/node
X Title: fs: `opendirSync()` leaks directory handle on error · Issue #58854 · nodejs/node
Description: Version v25.0.0-pre Platform Linux tumba 6.15.3-gentoo-yuran #1 SMP Sat Jun 21 21:02:19 +08 2025 x86_64 Intel(R) Core(TM)2 Quad CPU Q8200 @ 2.33GHz GenuineIntel GNU/Linux Subsystem fs What steps will reproduce the bug? test.mjs script: i...
Open Graph Description: Version v25.0.0-pre Platform Linux tumba 6.15.3-gentoo-yuran #1 SMP Sat Jun 21 21:02:19 +08 2025 x86_64 Intel(R) Core(TM)2 Quad CPU Q8200 @ 2.33GHz GenuineIntel GNU/Linux Subsystem fs What steps wi...
X Description: Version v25.0.0-pre Platform Linux tumba 6.15.3-gentoo-yuran #1 SMP Sat Jun 21 21:02:19 +08 2025 x86_64 Intel(R) Core(TM)2 Quad CPU Q8200 @ 2.33GHz GenuineIntel GNU/Linux Subsystem fs What steps wi...
Opengraph URL: https://github.com/nodejs/node/issues/58854
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"fs: `opendirSync()` leaks directory handle on error","articleBody":"### Version\n\nv25.0.0-pre\n\n### Platform\n\n```text\nLinux tumba 6.15.3-gentoo-yuran #1 SMP Sat Jun 21 21:02:19 +08 2025 x86_64 Intel(R) Core(TM)2 Quad CPU Q8200 @ 2.33GHz GenuineIntel GNU/Linux\n```\n\n### Subsystem\n\nfs\n\n### What steps will reproduce the bug?\n\n`test.mjs` script:\n```mjs\nimport fs from 'node:fs';\n{\n let dir;\n try {\n dir = fs.opendirSync(import.meta.dirname, { bufferSize: Infinity });\n console.log('successfully got', dir);\n } catch(e) {\n console.log('dir is:', dir, 'error is:', e);\n } finally {\n dir?.closeSync();\n }\n}\nglobalThis.gc();\nsetImmediate(() =\u003e {});\n```\nRun it with expose GC flag:\n```console\n$ node --expose-gc test.mjs\n```\n\n### How often does it reproduce? Is there a required condition?\n\nAlways, assuming GC runs.\n\n### What is the expected behavior? Why is that the expected behavior?\n\nThe \"successfully got\" should not be printed.\nThe catched error must be `ERR_OUT_OF_RANGE` because of bad `bufferSize`.\nThe `dir` should be either:\n- An instance of `fs.Dir` that we can `closeSync()` gracefully\n- `undefined` assuming `opendirSync` internally did cleanup\n\n**And most importantly, this code must not emit any warnings.**\n\n### What do you see instead?\n\n```js\ndir is: undefined error is: RangeError [ERR_OUT_OF_RANGE]: The value of \"options.bufferSize\" is out of range. It must be an integer. Received Infinity\n at new Dir (node:internal/fs/dir:61:5)\n at Object.opendirSync (node:internal/fs/dir:351:10)\n at file:///tmp/test.mjs:5:14\n at ModuleJob.run (node:internal/modules/esm/module_job:372:25)\n at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:665:26)\n at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:99:5) {\n code: 'ERR_OUT_OF_RANGE'\n}\n(node:8286) Warning: Closing directory handle on garbage collection\n(Use `node --trace-warnings ...` to show where the warning was created)\n```\nWhich means: `dir` stays `undefined` so we can not do anything about it, but the internal directory handle remains open until GC hunts it down.\n\n### Additional information\n\nI haven't checked if `fsPromises.opendir` nor `fs.opendir` have similar problem but most likely they do.\n\nI think this can be fixed on JS side, the patch is WIP.","author":{"url":"https://github.com/LiviaMedeiros","@type":"Person","name":"LiviaMedeiros"},"datePublished":"2025-06-26T21:06:30.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/58854/node/issues/58854"}
| 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:5ef7d986-a8fa-abbe-9c02-cf97184bf992 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EDD2:19E229:B69125:FC22EB:6A4DC408 |
| html-safe-nonce | 4590e2b9c7ca5fde7a314beb996c52f00bad229c149f32fd962e411051091764 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFREQyOjE5RTIyOTpCNjkxMjU6RkMyMkVCOjZBNERDNDA4IiwidmlzaXRvcl9pZCI6IjcxMDU2MTcwMTYwOTQ4MzU3MjAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | e9bb918f43306ee1743269c81a6b813e6631e4cd757c5cf19fcc591449e63b17 |
| hovercard-subject-tag | issue:3180438257 |
| 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/58854/issue_layout |
| twitter:image | https://opengraph.githubassets.com/6643793c2ec3c0d5fcd8daf0eb1f7be2329a018b10a314c46ac9d5cc7f9f2f51/nodejs/node/issues/58854 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/6643793c2ec3c0d5fcd8daf0eb1f7be2329a018b10a314c46ac9d5cc7f9f2f51/nodejs/node/issues/58854 |
| og:image:alt | Version v25.0.0-pre Platform Linux tumba 6.15.3-gentoo-yuran #1 SMP Sat Jun 21 21:02:19 +08 2025 x86_64 Intel(R) Core(TM)2 Quad CPU Q8200 @ 2.33GHz GenuineIntel GNU/Linux Subsystem fs What steps wi... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | LiviaMedeiros |
| 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 | 1d344bdb7547fe6bca17a59bb2b8aac3dc9532a0 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width