Skip to content


fs: Support `close` option for streams (default=true) #4389

isaacs opened this Issue · 8 comments

5 participants


It's annoying that there's no way to make a fs stream not close when it reaches the end, especially when you're providing a fd from a pool that you intend to re-use.

Add a close flag. This defaults to true if no fd is passed in, or false otherwise. If it's anything other than boolean false, then call fs.close(fd) when the EOF is reached.

I'm unsure how this should interact with #4387. Should it still close on error if you've set close: false?


I'd suggest a more descriptive name -- perhaps something like close_on_eof.

Then, if it's desirable to control the behavior when an error is encountered, then a separate flag (close_on_error) could be used.

These names have the additional benefit of being mostly self-documenting.


Their names should probably contain actual spaces instead of underscores, such as {"close on eof": "true"}.


You guys are crazy. No, we are not putting underscores in option names, and definitely not putting spaces in option names. Our options shall be camelCase as god intended.


But really, I want a single "close" option that covers both. If you're sharing fd's between multiple streams, you definitely don't want them to be closed.


camelCase is good. If it's going to be one single option, then I'd still suggest something more descriptive than close. How about: autoClose?

var r = fs.createReadStream("somefile", {autoClose: false});

then, from the name alone, it's clear: if I disable automatic closing then I'll need to close the fd manually. Otherwise, presumably, it'll be managed for me. Even if I'm unfamiliar with this particular feature, I can understand enough from the option's name to grasp what its function must be.

Node.js Foundation member

autoClose is fine. First one to submit the PR gets a cookie.


If the pull request isn't based on the streams2 branch, then the first one to submit a PR gets their commit clobbered ;)

@alFReD-NSH alFReD-NSH added a commit to alFReD-NSH/node that referenced this issue
@alFReD-NSH alFReD-NSH Add autoClose=true option to fs.createReadStream
Closes #4389.
Node.js Foundation member

Appears to have landed already

@jasnell jasnell closed this
@joaocgreis joaocgreis pushed a commit to janeasystems/node-v0.x-archive that referenced this issue
@estliberitas estliberitas doc: add `servername` parameter docs
Add `servername` parameter docs for `https.request()` method.

Follows #4389

PR-URL: nodejs/node#4729
Reviewed-By: James M Snell <>
Reviewed-By: Fedor Indutny <>
Reviewed-By: Roman Reiss <>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.