Skip to content

fs: remove FileHandle stream close listener#64229

Open
ishaanlabs-gg wants to merge 2 commits into
nodejs:mainfrom
ishaanlabs-gg:fix-filehandle-readstream-listener-leak
Open

fs: remove FileHandle stream close listener#64229
ishaanlabs-gg wants to merge 2 commits into
nodejs:mainfrom
ishaanlabs-gg:fix-filehandle-readstream-listener-leak

Conversation

@ishaanlabs-gg

Copy link
Copy Markdown

Fixes: #64214

This removes the extra FileHandle close listener that can be left behind by
streams created with autoClose: false.

The listener still lets the stream close if the FileHandle is closed first,
but autoClose: false streams now remove that listener and release the extra
stream reference when the stream finishes or closes on its own.

The change also keeps the default autoClose behavior covered so read and
write streams still close their FileHandle as before.

Tests:

  • ./out/Release/node --check lib/internal/fs/streams.js
  • ./out/Release/node test/parallel/test-fs-promises-file-handle-stream.js
  • python3 tools/test.py -J --mode=release parallel/test-fs-promises-file-handle-stream

@nodejs-github-bot nodejs-github-bot added fs Issues and PRs related to the fs subsystem / file system. needs-ci PRs that need a full CI run. labels Jul 1, 2026

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Comment thread lib/internal/fs/streams.js Outdated
options.fd.on('close', FunctionPrototypeBind(stream.close, stream));
stream[kHandleCloseListener] = FunctionPrototypeBind(stream.close, stream);
stream[kHandleCloseListenerCleanup] = FunctionPrototypeBind(
cleanupHandleCloseListener, stream);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not needed if not autoclosing. move it inside the if.

@ishaanlabs-gg ishaanlabs-gg force-pushed the fix-filehandle-readstream-listener-leak branch from f937b9a to 98b30c4 Compare July 1, 2026 14:10
@ishaanlabs-gg ishaanlabs-gg force-pushed the fix-filehandle-readstream-listener-leak branch from 98b30c4 to 59c7463 Compare July 1, 2026 14:11
@ishaanlabs-gg

Copy link
Copy Markdown
Author

Addressed the review feedback by only installing the FileHandle close listener for autoClose: false streams.\n\nLocal verification:\n\n\npython3 tools/test.py -J parallel/test-fs-promises-file-handle-stream\n

Comment thread lib/internal/fs/streams.js Outdated
Comment on lines +162 to +165
stream.once('close', stream[kHandleCloseListenerCleanup]);
stream.once('end', stream[kHandleCloseListenerCleanup]);
stream.once('finish', stream[kHandleCloseListenerCleanup]);
stream.once('error', stream[kHandleCloseListenerCleanup]);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure you don't need to listen for close here, since there will always be either an end (reader) or finish (writer), or an error. Listening for close just doubles-up the call (which is fine because the function protects against being called multiple times, but unnecessary)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fs Issues and PRs related to the fs subsystem / file system. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Close listener leak in fs/promises createReadStream

4 participants