Skip to content

Commit 5397a5b

Browse files
committed
ut only released either when the
s/node/pull/64229 Signed-off-by: Rasad Regmi <[email protected]> Title: fs: fix FileHandle close listener/ref leak in streams Description: `fileHandle.createReadStream()`/`createWriteStream()` with `autoClose: false` leaks a `'close'` listener (and an internal reference) on the `FileHandle` every time the stream finishes on its own, because `autoClose: false` also disables `autoDestroy`, so the stream never reaches `_destroy()` — the only place that previously released them. Enough such streams on one long-lived handle trips `MaxListenersExceededWarning`. `importFd()` in `lib/internal/fs/streams.js` now builds a single idempotent `releaseHandleRef` shared between `FileHandleOperations .close()` (the normal destroy path, unchanged for the default `autoClose: true` behavior) and a `finished()`-based safety net registered after the stream's readable/writable state is initialized, for streams that finish without ever calling `destroy()`. `finished()` correctly resolves on `'end'`/`'finish'` alone when the stream won't emit `'close'` (see `willEmitClose()` in `internal/streams/utils.js`), and the shared idempotency guard means it's safe even if a stream is also explicitly closed after finishing on its own — the exact scenario that caused a prior fix (nodejs#64227) to fs: fix FileHandle close listener/ref leak in streams createReadStream()/createWriteStream({ autoClose: false }) created from a FileHandle attached a 'close' listener and took a reference on the handle in importFd(), but only released either when the stream went through _destroy(). Since autoClose: false also disables autoDestroy, a stream that finishes on its own never reaches _destroy(), so the listener and reference leaked. Creating enough such streams on a long-lived handle (e.g. repeated ranged reads) eventually tripped MaxListenersExceededWarning. A prior fix (fixed in 64227, reverted in 65387) released the reference again on the stream's 'end'/'finish'/'error' independent of the destroy path, which could unref the handle twice if the stream was later also explicitly closed/destroyed - a normal thing to do after a stream naturally ends. This introduces a single idempotent release function shared by both paths (the normal destroy path and a finished()-based safety net for autoClose: false streams that never reach _destroy()), so the reference and listener are released exactly once regardless of how the stream ends. Fixes: nodejs#64214 Refs: nodejs#64227 Refs: nodejs#65387 Refs: nodejs#64229 Signed-off-by: Rasad Regmi <[email protected]> Manual Git Steps (for you to run) cd "/home/rasadregmi/Desktop/Open Source Contributions Repos/node" git checkout -b fix-filehandle-stream-close-listener-leak main git add lib/internal/fs/streams.js test/parallel/test-fs-promises-file-hand git commit -s git push fork fix-filehandle-stream-close-listener-leak Commit Message fs: fix FileHandle close listener/ref leak in streams createReadStream()/createWriteStream({ autoClose: false }) created from a FileHandle attached a 'close' listener and took a reference on the handle in importFd(), but only released either when the stream went through _destroy(). Since autoClose: false also disables autoDestroy, a stream that finishes on its own never reaches _destroy(), so the listener and reference leaked. Creating enough such streams on a long-lived handle (e.g. repeated ranged reads) eventually tripped MaxListenersExceededWarning. A prior fix (fixed in 64227, reverted in 65387) released the reference again on the stream's 'end'/'finish'/'error' independent of the destroy path, which could unref the handle twice if the stream was later also explicitly closed/destroyed - a normal thing to do after a stream naturally ends. This introduces a single idempotent release function shared by both paths (the normal destroy path and a finished()-based safety net for autoClose: false streams that never reach _destroy()), so the reference and listener are released exactly once regardless of how the stream ends. Fixes: nodejs#64214 Refs: nodejs#64227 Refs: nodejs#65387 Refs: nodejs#64229 Signed-off-by: Rasad Regmi <[email protected]>
1 parent 67e66b8 commit 5397a5b

2 files changed

Lines changed: 163 additions & 7 deletions

File tree

‎lib/internal/fs/streams.js‎

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const kIsPerformingIO = Symbol('kIsPerformingIO');
5151

5252
const kFs = Symbol('kFs');
5353
const kHandle = Symbol('kHandle');
54+
const kReleaseHandleRef = Symbol('kReleaseHandleRef');
5455

5556
function _construct(callback) {
5657
const stream = this;
@@ -90,13 +91,13 @@ function _construct(callback) {
9091
}
9192

9293
// This generates an fs operations structure for a FileHandle
93-
const FileHandleOperations = (handle) => {
94+
const FileHandleOperations = (handle, releaseHandleRef) => {
9495
return {
9596
open: (path, flags, mode, cb) => {
9697
throw new ERR_METHOD_NOT_IMPLEMENTED('open()');
9798
},
9899
close: (fd, cb) => {
99-
handle[kUnref]();
100+
releaseHandleRef();
100101
PromisePrototypeThen(handle.close(),
101102
() => cb(), cb);
102103
},
@@ -155,11 +156,32 @@ function importFd(stream, options) {
155156
// FileHandle is not supported with custom fs operations
156157
throw new ERR_METHOD_NOT_IMPLEMENTED('FileHandle with fs');
157158
}
158-
stream[kHandle] = options.fd;
159-
stream[kFs] = FileHandleOperations(stream[kHandle]);
160-
stream[kHandle][kRef]();
161-
options.fd.on('close', FunctionPrototypeBind(stream.close, stream));
162-
return options.fd.fd;
159+
const handle = options.fd;
160+
stream[kHandle] = handle;
161+
handle[kRef]();
162+
163+
// Release the ref/listener exactly once, whichever comes first: the
164+
// stream being destroyed (FileHandleOperations.close(), below) or, for
165+
// `autoClose: false` streams that finish without ever being destroyed,
166+
// finished() below (see willEmitClose() in internal/streams/utils.js).
167+
let handleRefReleased = false;
168+
const onHandleClose = FunctionPrototypeBind(stream.close, stream);
169+
function releaseHandleRef() {
170+
if (handleRefReleased) return;
171+
handleRefReleased = true;
172+
handle.removeListener('close', onHandleClose);
173+
handle[kUnref]();
174+
}
175+
176+
stream[kFs] = FileHandleOperations(handle, releaseHandleRef);
177+
handle.on('close', onHandleClose);
178+
// finished() needs the stream's readable/writable state, which isn't
179+
// initialized until Readable.call()/Writable.call() runs later in the
180+
// constructor, so defer registering it until then (see kReleaseHandleRef
181+
// below).
182+
stream[kReleaseHandleRef] = releaseHandleRef;
183+
184+
return handle.fd;
163185
}
164186

165187
throw new ERR_INVALID_ARG_TYPE('options.fd',
@@ -255,6 +277,10 @@ function ReadStream(path, options) {
255277
}
256278

257279
FunctionPrototypeCall(Readable, this, options);
280+
281+
if (this[kReleaseHandleRef]) {
282+
finished(this, this[kReleaseHandleRef]);
283+
}
258284
}
259285
ObjectSetPrototypeOf(ReadStream.prototype, Readable.prototype);
260286
ObjectSetPrototypeOf(ReadStream, Readable);
@@ -425,6 +451,10 @@ function WriteStream(path, options) {
425451

426452
if (options.encoding)
427453
this.setDefaultEncoding(options.encoding);
454+
455+
if (this[kReleaseHandleRef]) {
456+
finished(this, this[kReleaseHandleRef]);
457+
}
428458
}
429459
ObjectSetPrototypeOf(WriteStream.prototype, Writable.prototype);
430460
ObjectSetPrototypeOf(WriteStream, Writable);

‎test/parallel/test-fs-promises-file-handle-stream.js‎

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,133 @@ async function validateRead() {
4242
);
4343
}
4444

45+
// Regression test for https://github.com/nodejs/node/issues/64214: every
46+
// createReadStream({ autoClose: false }) call used to leave behind a 'close'
47+
// listener on the FileHandle (and an un-released internal ref), because
48+
// autoClose: false disables autoDestroy, so the stream never goes through
49+
// _destroy() when it finishes on its own. Repeating this past 10 iterations
50+
// used to trigger a MaxListenersExceededWarning.
51+
async function validateReadStreamAutoCloseFalseReleasesListener() {
52+
const filePathForHandle = path.resolve(tmpDir, 'tmp-read-autoclose-false.txt');
53+
const buf = Buffer.from('Hello world', 'utf8');
54+
55+
fs.writeFileSync(filePathForHandle, buf);
56+
57+
const fileHandle = await open(filePathForHandle);
58+
try {
59+
for (let i = 0; i < buf.length; i++) {
60+
const chunk = await buffer(fileHandle.createReadStream({
61+
start: i,
62+
end: i,
63+
autoClose: false,
64+
}));
65+
assert.strictEqual(chunk[0], buf[i]);
66+
assert.strictEqual(fileHandle.listenerCount('close'), 0);
67+
}
68+
} finally {
69+
await fileHandle.close();
70+
}
71+
}
72+
73+
// Same leak, but for createWriteStream({ autoClose: false }).
74+
async function validateWriteStreamAutoCloseFalseReleasesListener() {
75+
const filePathForHandle =
76+
path.resolve(tmpDir, 'tmp-write-autoclose-false.txt');
77+
const buf = Buffer.from('Hello world', 'utf8');
78+
79+
const fileHandle = await open(filePathForHandle, 'w');
80+
try {
81+
for (let i = 0; i < buf.length; i++) {
82+
const stream = fileHandle.createWriteStream({
83+
start: i,
84+
autoClose: false,
85+
});
86+
stream.end(buf.subarray(i, i + 1));
87+
await finished(stream);
88+
assert.strictEqual(fileHandle.listenerCount('close'), 0);
89+
}
90+
} finally {
91+
await fileHandle.close();
92+
}
93+
94+
assert.deepStrictEqual(fs.readFileSync(filePathForHandle), buf);
95+
}
96+
97+
// Regression test for the fix that was reverted in
98+
// https://github.com/nodejs/node/pull/65387: a previous attempt released the
99+
// FileHandle ref both when the stream finished on its own *and* again when
100+
// the stream was explicitly closed/destroyed afterwards, unreffing the
101+
// handle twice for a single stream. Explicitly closing a stream after it has
102+
// already finished on its own (autoClose: false) is a normal thing to do and
103+
// must not double-release the handle's reference count.
104+
async function validateAutoCloseFalseExplicitCloseDoesNotDoubleRelease() {
105+
const filePathForHandle =
106+
path.resolve(tmpDir, 'tmp-read-autoclose-false-explicit-close.txt');
107+
const buf = Buffer.from('Hello world', 'utf8');
108+
109+
fs.writeFileSync(filePathForHandle, buf);
110+
111+
const fileHandle = await open(filePathForHandle);
112+
// Register this before anything closes the handle: FileHandleOperations
113+
// .close() unconditionally closes the handle once the stream is
114+
// destroyed (that's how `autoClose: true` implicitly closes the handle),
115+
// so the explicit stream.close() below is expected to trigger it. This
116+
// listener itself accounts for one 'close' listener throughout, on top of
117+
// whatever the stream adds/removes.
118+
const closed = new Promise((resolve) => {
119+
fileHandle.once('close', common.mustCall(resolve));
120+
});
121+
122+
const stream = fileHandle.createReadStream({
123+
start: 0,
124+
end: 0,
125+
autoClose: false,
126+
});
127+
await buffer(stream);
128+
// Only the listener registered above remains; the stream's own listener
129+
// was released when it finished on its own.
130+
assert.strictEqual(fileHandle.listenerCount('close'), 1);
131+
132+
// The stream already finished on its own (which already released its
133+
// handle ref); closing it again must be a safe no-op with respect to that
134+
// ref, and must not corrupt the handle's internal reference count.
135+
await new Promise((resolve, reject) => {
136+
stream.close((err) => (err ? reject(err) : resolve()));
137+
});
138+
await closed;
139+
assert.strictEqual(fileHandle.listenerCount('close'), 0);
140+
141+
// The handle is already fully closed at this point; closing it again must
142+
// remain a safe, immediately-resolving no-op (it would hang or throw if
143+
// the ref count had gone negative).
144+
await fileHandle.close();
145+
}
146+
147+
// The default (autoClose: true) behavior must be unaffected: finishing the
148+
// stream still implicitly closes the FileHandle exactly once, and leaves no
149+
// listener behind.
150+
async function validateAutoCloseTrueStillClosesFileHandle() {
151+
const filePathForHandle =
152+
path.resolve(tmpDir, 'tmp-read-autoclose-true.txt');
153+
const buf = Buffer.from('Hello world', 'utf8');
154+
155+
fs.writeFileSync(filePathForHandle, buf);
156+
157+
const fileHandle = await open(filePathForHandle);
158+
const closed = new Promise((resolve) => {
159+
fileHandle.once('close', common.mustCall(resolve));
160+
});
161+
162+
assert.deepStrictEqual(await buffer(fileHandle.createReadStream()), buf);
163+
await closed;
164+
assert.strictEqual(fileHandle.listenerCount('close'), 0);
165+
}
166+
45167
Promise.all([
46168
validateWrite(),
47169
validateRead(),
170+
validateReadStreamAutoCloseFalseReleasesListener(),
171+
validateWriteStreamAutoCloseFalseReleasesListener(),
172+
validateAutoCloseFalseExplicitCloseDoesNotDoubleRelease(),
173+
validateAutoCloseTrueStillClosesFileHandle(),
48174
]).then(common.mustCall());

0 commit comments

Comments
 (0)