Title: `stream.pipeline()` leaks file descriptors when it throws synchronously (ERR_STREAM_UNABLE_TO_PIPE) · Issue #65063 · nodejs/node · GitHub
Open Graph Title: `stream.pipeline()` leaks file descriptors when it throws synchronously (ERR_STREAM_UNABLE_TO_PIPE) · Issue #65063 · nodejs/node
X Title: `stream.pipeline()` leaks file descriptors when it throws synchronously (ERR_STREAM_UNABLE_TO_PIPE) · Issue #65063 · nodejs/node
Description: Version v24.11.1, and main (735a09f999a) Platform Reproduced on Windows 11 x64; the code path is platform independent. Subsystem stream What steps will reproduce the bug? stream.pipeline() wires the streams together in a loop, and that l...
Open Graph Description: Version v24.11.1, and main (735a09f999a) Platform Reproduced on Windows 11 x64; the code path is platform independent. Subsystem stream What steps will reproduce the bug? stream.pipeline() wires th...
X Description: Version v24.11.1, and main (735a09f999a) Platform Reproduced on Windows 11 x64; the code path is platform independent. Subsystem stream What steps will reproduce the bug? stream.pipeline() wires th...
Opengraph URL: https://github.com/nodejs/node/issues/65063
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`stream.pipeline()` leaks file descriptors when it throws synchronously (ERR_STREAM_UNABLE_TO_PIPE)","articleBody":"### Version\n\nv24.11.1, and `main` (`735a09f999a`)\n\n### Platform\n\nReproduced on Windows 11 x64; the code path is platform independent.\n\n### Subsystem\n\nstream\n\n### What steps will reproduce the bug?\n\n`stream.pipeline()` wires the streams together in a loop, and that loop can throw synchronously. The most common case is `ERR_STREAM_UNABLE_TO_PIPE`, raised when the destination is already closed or destroyed. When it throws, every stream `pipeline()` had already taken ownership of is left undestroyed, so its resources leak. For a `fs.ReadStream` source that means a leaked file descriptor.\n\nThe trigger is an ordinary production condition: piping to a destination that has already gone away, e.g. `pipeline(fs.createReadStream(file), res)` after the HTTP client disconnected.\n\n```js\nimport { pipeline, PassThrough, Writable } from 'node:stream';\nimport { pipeline as pipelinePromise } from 'node:stream/promises';\nimport { once, getEventListeners } from 'node:events';\nimport fs from 'node:fs';\nimport os from 'node:os';\nimport path from 'node:path';\n\nconst tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'pipeline-leak-'));\nconst file = path.join(tmp, 'data.bin');\nfs.writeFileSync(file, Buffer.alloc(4096, 'x'));\n\nconst openStream = async () => {\n const rs = fs.createReadStream(file);\n await once(rs, 'open'); // ensure the fd is really allocated\n return rs;\n};\nconst deadWritable = () => {\n const w = new Writable({ write(c, e, cb) { cb(); } });\n w.destroy(); // destination already gone\n return w;\n};\n\n// 1. callback form\n{\n const sources = [];\n for (let i = 0; i \u003c 50; i++) {\n const rs = await openStream();\n sources.push(rs);\n try {\n pipeline(rs, new PassThrough(), deadWritable(), () => {});\n } catch (err) {\n if (i === 0) console.log('callback form throws :', err.code);\n }\n }\n await new Promise((r) => setTimeout(r, 150));\n console.log(' sources undestroyed :', sources.filter((s) => !s.destroyed).length, '/ 50 (expected 0)');\n console.log(' fds still open :', sources.filter((s) => s.fd != null).length, '/ 50 (expected 0)');\n for (const s of sources) s.destroy();\n}\n\n// 2. promise form, plus the caller's AbortSignal\n{\n const ac = new AbortController(); // long-lived, e.g. a server shutdown signal\n const sources = [];\n for (let i = 0; i \u003c 50; i++) {\n const rs = await openStream();\n sources.push(rs);\n try {\n await pipelinePromise(rs, new PassThrough(), deadWritable(), { signal: ac.signal });\n } catch (err) {\n if (i === 0) console.log('promise form rejects :', err.code);\n }\n }\n await new Promise((r) => setTimeout(r, 150));\n console.log(' sources undestroyed :', sources.filter((s) => !s.destroyed).length, '/ 50 (expected 0)');\n console.log(' fds still open :', sources.filter((s) => s.fd != null).length, '/ 50 (expected 0)');\n console.log(' abort listeners :', getEventListeners(ac.signal, 'abort').length, '/ 50 (expected 0)');\n for (const s of sources) s.destroy();\n}\n\n// 3. control: an ordinary asynchronous failure does clean up correctly\n{\n const rs = fs.createReadStream(path.join(tmp, 'nope'));\n const mid = new PassThrough();\n await new Promise((resolve) => pipeline(rs, mid, new PassThrough(), () => resolve()));\n console.log('control (ENOENT) : rs.destroyed =', rs.destroyed, '| mid.destroyed =', mid.destroyed, ' (both expected true)');\n}\n\nfs.rmSync(tmp, { recursive: true, force: true });\n```\n\n### How often does it reproduce? Is there a required condition?\n\nEvery time. The only condition is that `pipeline()` throws while wiring the streams up, after at least one stream has already been wired.\n\n### What is the expected behavior? Why is that the expected behavior?\n\nAll the streams should be destroyed and the file descriptors released, and the listener added to the caller's `AbortSignal` should be removed.\n\n`doc/api/stream.md` states:\n\n> `stream.pipeline()` closes all the streams when an error is raised.\n\nand\n\n> `stream.pipeline()` will call `stream.destroy(err)` on all streams except: `Readable` streams which have emitted `'end'` or `'close'`; `Writable` streams which have emitted `'finish'` or `'close'`.\n\nThe sources here emitted none of those events. The control case in the reproduction shows that an *asynchronous* pipeline error does destroy everything, so the two paths disagree.\n\n### What do you see instead?\n\n```\ncallback form throws : ERR_STREAM_UNABLE_TO_PIPE\n sources undestroyed : 50 / 50 (expected 0)\n fds still open : 50 / 50 (expected 0)\npromise form rejects : ERR_STREAM_UNABLE_TO_PIPE\n sources undestroyed : 50 / 50 (expected 0)\n fds still open : 50 / 50 (expected 0)\n abort listeners : 50 / 50 (expected 0)\ncontrol (ENOENT) : rs.destroyed = true | mid.destroyed = true (both expected true)\n```\n\n### Additional information\n\nThe wiring loop in `pipelineImpl()` ([`lib/internal/streams/pipeline.js`](https://github.com/nodejs/node/blob/main/lib/internal/streams/pipeline.js)) pushes a destroy function into `destroys` for each stream it adopts, and `finishImpl()` is the only code that drains `destroys`, disposes the `AbortSignal` listener and calls `ac.abort()`.\n\nThe loop is not wrapped in `try`/`finally`, and it can throw at six places:\n\n* `ERR_STREAM_UNABLE_TO_PIPE` when the next stream is already closed or destroyed\n* `ERR_INVALID_RETURN_VALUE` (x3) when a transform function returns something that is not iterable\n* `ERR_INVALID_ARG_TYPE` (x2) when a value cannot be piped into the next stream\n\nWhen any of these fire, `finishImpl()` never runs, so nothing in `destroys` is ever called.\n\nThe `ERR_INVALID_RETURN_VALUE` path leaks in exactly the same way:\n\n```js\nconst source = fs.createReadStream(file);\npipeline(source, () => 42, new PassThrough(), () => {}); // throws\n// source is left open\n```\n\nI have a fix and will open a PR shortly.\n","author":{"url":"https://github.com/shani-singh1","@type":"Person","name":"shani-singh1"},"datePublished":"2026-08-06T01:48:01Z","url":"https://github.com/nodejs/node/issues/65063"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| 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:efa5b586-3fe3-7834-7d72-5ef5320a688a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8B9A:2FF8F1:E6E51B:12D4E07:6AED5644 |
| html-safe-nonce | 34dfe8ac0491d3571d31460fe0f41c4932fa5d97d4739838da8f1b9537237f64 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QjlBOjJGRjhGMTpFNkU1MUI6MTJENEUwNzo2QUVENTY0NCIsInZpc2l0b3JfaWQiOiIzOTgyMDk1OTExNTQ2NjA5MzIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 5ae2153db86723246e97d2b50a1bb61927acb87642fe1b68cbf9086fea94b5c6 |
| hovercard-subject-tag | issue:5076780702 |
| 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/65063/issue_layout |
| twitter:image | https://opengraph.githubassets.com/eacf2c0c0123d971a49c149c5be4f1f065d8794abb908b71d0559b94e763ea7a/nodejs/node/issues/65063 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/eacf2c0c0123d971a49c149c5be4f1f065d8794abb908b71d0559b94e763ea7a/nodejs/node/issues/65063 |
| og:image:alt | Version v24.11.1, and main (735a09f999a) Platform Reproduced on Windows 11 x64; the code path is platform independent. Subsystem stream What steps will reproduce the bug? stream.pipeline() wires th... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | shani-singh1 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 56fb42677ae87ed3e64410323d67980aa65d0485568a7858f874e7edfc6d1c11 |
| 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 | 9c9a37e2423ed382cf39f70b5aa4b4858b40a3b0 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width