Skip to content

fs: fix close listener leak in FileHandle streams#64227

Open
Y1D7NG wants to merge 1 commit into
nodejs:mainfrom
Y1D7NG:fix-stream-leak
Open

fs: fix close listener leak in FileHandle streams#64227
Y1D7NG wants to merge 1 commit into
nodejs:mainfrom
Y1D7NG:fix-stream-leak

Conversation

@Y1D7NG

@Y1D7NG Y1D7NG commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

fix: #64214

When autoClose is false, FileHandle.createReadStream registers a close listener on the handle. That listener is not removed after the stream finishes normally, so creating streams repeatedly on the same handle accumulates listeners.

In importFd, when autoClose is false, remove that listener and perform the corresponding unref cleanup when the stream ends (on end for read streams, on finish for write streams) or on error.

@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
@Y1D7NG Y1D7NG force-pushed the fix-stream-leak branch 5 times, most recently from b433fb3 to c4a165a Compare July 1, 2026 11:11
Fixes: nodejs#64214
Signed-off-by: y1d7ng <y1d7ng@yeah.net>
@Y1D7NG Y1D7NG force-pushed the fix-stream-leak branch from c4a165a to 96cae83 Compare July 1, 2026 11:18
Comment on lines +51 to +58
for (let i = 0; i < 11; i++) {
await buffer(fileHandle.createReadStream({
start: i,
end: i,
autoClose: false,
}));
assert.strictEqual(fileHandle.listenerCount('close'), 0);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

this doesn't need to be in a loop - I just used a loop in my report to demonstrate that it would trigger MaxListenersExceededWarning, but checking listenerCount = 0 after a single read stream is enough for a test (maybe 2, to prove the handle is still open for creating a second stream)

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

3 participants