Skip to content

Commit 7996b18

Browse files
stream: close Utf8Stream fd after pending write
destroy() fsynced and closed the fd right away, even while an fs.write() on it was still running on the thread pool. The write then failed with EBADF, emitted as an 'error' that crashed the process when nothing listened for it, or landed in whatever file reused the fd number. Close the fd once the in-flight write completes, as fs.WriteStream does. Data buffered behind it is still dropped, so 'finish' is not emitted in that case. Signed-off-by: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com> PR-URL: #67355 Refs: https://github.com/nodejs/reliability/blob/main/reports/2026-11-09.md Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 291d62f commit 7996b18

2 files changed

Lines changed: 179 additions & 9 deletions

File tree

‎lib/internal/streams/fast-utf8-stream.js‎

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ class Utf8Stream extends EventEmitter {
114114
#hwm = 16387; // 16 KB
115115
#file = null;
116116
#destroyed = false;
117+
#writeInFlight = false;
118+
#closeAfterWrite = false;
117119
#minLength = 0;
118120
#maxLength = 0;
119121
#maxWrite = kMaxWrite;
@@ -229,19 +231,31 @@ class Utf8Stream extends EventEmitter {
229231
this.#flushSync = (...args) => this.#flushBufferSync(...args);
230232
this.#actualWrite = (...args) => this.#actualWriteBuffer(...args);
231233
this.#fsWriteSync = () => this.#fs.writeSync(this.#fd, this.#writingBuf);
232-
this.#fsWrite = () => this.#fs.write(this.#fd, this.#writingBuf, (...args) => {
233-
return this.#release(...args);
234-
});
234+
this.#fsWrite = () => {
235+
this.#writeInFlight = true;
236+
try {
237+
this.#fs.write(this.#fd, this.#writingBuf, (err, n) => this.#writeDone(err, n));
238+
} catch (err) {
239+
this.#writeInFlight = false;
240+
throw err;
241+
}
242+
};
235243
} else {
236244
this.#writingBuf = '';
237245
this.#write = (...args) => this.#writeUtf8(...args);
238246
this.#flush = (...args) => this.#flushUtf8(...args);
239247
this.#flushSync = (...args) => this.#flushSyncUtf8(...args);
240248
this.#actualWrite = (...args) => this.#actualWriteUtf8(...args);
241249
this.#fsWriteSync = () => this.#fs.writeSync(this.#fd, this.#writingBuf, 'utf8');
242-
this.#fsWrite = () => this.#fs.write(this.#fd, this.#writingBuf, 'utf8', (...args) => {
243-
return this.#release(...args);
244-
});
250+
this.#fsWrite = () => {
251+
this.#writeInFlight = true;
252+
try {
253+
this.#fs.write(this.#fd, this.#writingBuf, 'utf8', (err, n) => this.#writeDone(err, n));
254+
} catch (err) {
255+
this.#writeInFlight = false;
256+
throw err;
257+
}
258+
};
245259
}
246260

247261
// TODO(@jasnell): Support passing in an AbortSignal to cancel the stream.
@@ -683,13 +697,42 @@ class Utf8Stream extends EventEmitter {
683697
this.#bufs = [];
684698
this.#lens = [];
685699

700+
// fs.write() runs on the thread pool, so the fd cannot be closed while
701+
// one is in flight: the write would fail with EBADF, or land in whatever
702+
// file is opened next and reuses the fd number. #writeDone() closes it
703+
// once the write completes.
704+
if (this.#writeInFlight) {
705+
this.#closeAfterWrite = true;
706+
return;
707+
}
708+
709+
this.#closeFd();
710+
}
711+
712+
#writeDone(err, n) {
713+
this.#writeInFlight = false;
714+
try {
715+
this.#release(err, n);
716+
} finally {
717+
// #release() may have started another write, for the rest of a partial
718+
// write or from an 'error' listener that then called destroy(). The
719+
// close then waits for that one.
720+
if (this.#closeAfterWrite && !this.#writeInFlight) {
721+
this.#closeAfterWrite = false;
722+
this.#closeFd();
723+
}
724+
}
725+
}
726+
727+
#closeFd() {
686728
const done = (err) => {
687729
if (err) {
688730
this.emit('error', err);
689731
return;
690732
}
691733

692-
if (this.#ending && !this.#writing) {
734+
// destroy() may have dropped buffered data or a failed write.
735+
if (this.#ending && !this.#writing && this.#len === 0) {
693736
this.emit('finish');
694737
}
695738
this.emit('close');
@@ -727,7 +770,7 @@ class Utf8Stream extends EventEmitter {
727770
// we do it here to avoid the overhead of calculating the buffer size
728771
// in releaseWritingBuf.
729772
this.#writingBuf = Buffer.from(this.#writingBuf);
730-
this.#fs.write(this.#fd, this.#writingBuf, (...args) => this.#release(...args));
773+
this.#fsWrite();
731774
}
732775
}
733776

@@ -743,7 +786,7 @@ class Utf8Stream extends EventEmitter {
743786
this.#release(err);
744787
}
745788
} else {
746-
this.#fs.write(this.#fd, this.#writingBuf, 'utf8', (...args) => this.#release(...args));
789+
this.#fsWrite();
747790
}
748791
}
749792

‎test/parallel/test-fastutf8stream-destroy.js‎

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ const common = require('../common');
44
const tmpdir = require('../common/tmpdir');
55
const assert = require('node:assert');
66
const {
7+
close,
78
openSync,
89
readFile,
910
readFileSync,
11+
write,
1012
} = require('node:fs');
1113
const { Utf8Stream } = require('node:fs');
1214
const { isMainThread } = require('node:worker_threads');
@@ -64,3 +66,128 @@ function getTempFile() {
6466
stream.destroy();
6567
stream.on('close', common.mustCall());
6668
}
69+
70+
{
71+
// The fd is closed only after a write that is in flight at destroy() time
72+
// completes. Data buffered behind it is dropped, so 'finish' is not emitted.
73+
const dest = getTempFile();
74+
const fd = openSync(dest, 'w');
75+
let releaseWrite;
76+
const stream = new Utf8Stream({
77+
fd,
78+
fs: {
79+
write: common.mustCall((...args) => {
80+
releaseWrite = () => {
81+
releaseWrite = null;
82+
write(...args);
83+
};
84+
}),
85+
fsync: (fd, callback) => process.nextTick(callback),
86+
close: common.mustCall((fd, callback) => {
87+
assert.strictEqual(releaseWrite, null);
88+
close(fd, callback);
89+
}),
90+
},
91+
});
92+
93+
stream.on('error', common.mustNotCall());
94+
stream.on('finish', common.mustNotCall());
95+
stream.on('close', common.mustCall(() => {
96+
assert.strictEqual(readFileSync(dest, 'utf8'), 'hello world\n');
97+
}));
98+
99+
assert.ok(stream.write('hello world\n'));
100+
stream.write('dropped\n');
101+
stream.end();
102+
stream.destroy();
103+
setImmediate(() => releaseWrite());
104+
}
105+
106+
{
107+
// A write started from an 'error' listener right before destroy() also
108+
// completes before the fd is closed.
109+
const dest = getTempFile();
110+
const fd = openSync(dest, 'w');
111+
let releaseWrite;
112+
const stream = new Utf8Stream({
113+
fd,
114+
fs: {
115+
write: common.mustCall((...args) => {
116+
if (releaseWrite === undefined) {
117+
releaseWrite = null;
118+
const err = new Error('EIO');
119+
err.code = 'EIO';
120+
process.nextTick(args.at(-1), err);
121+
return;
122+
}
123+
releaseWrite = () => {
124+
releaseWrite = null;
125+
write(...args);
126+
};
127+
}, 2),
128+
fsync: (fd, callback) => process.nextTick(callback),
129+
close: common.mustCall((fd, callback) => {
130+
assert.strictEqual(releaseWrite, null);
131+
close(fd, callback);
132+
}),
133+
},
134+
});
135+
136+
stream.on('error', common.mustCall(() => {
137+
stream.write('second\n');
138+
stream.destroy();
139+
setImmediate(() => releaseWrite());
140+
}));
141+
stream.on('close', common.mustCall());
142+
143+
assert.ok(stream.write('first\n'));
144+
}
145+
146+
{
147+
// A pending write that fails after destroy() with no 'error' listener
148+
// throws, and the fd is still closed.
149+
const dest = getTempFile();
150+
const fd = openSync(dest, 'w');
151+
let failWrite;
152+
const stream = new Utf8Stream({
153+
fd,
154+
fs: {
155+
write: common.mustCall((fd, data, encoding, callback) => {
156+
failWrite = () => {
157+
const err = new Error('EIO');
158+
err.code = 'EIO';
159+
callback(err);
160+
};
161+
}),
162+
},
163+
});
164+
165+
stream.on('close', common.mustCall());
166+
167+
assert.ok(stream.write('hello world\n'));
168+
stream.destroy();
169+
setImmediate(common.mustCall(() => {
170+
process.once('uncaughtException', common.mustCall((err) => {
171+
if (err.code !== 'EIO') throw err;
172+
}));
173+
failWrite();
174+
}));
175+
}
176+
177+
{
178+
// destroy() closes the fd even if fs.write() threw synchronously.
179+
const fd = openSync(getTempFile(), 'w');
180+
const stream = new Utf8Stream({
181+
fd,
182+
fs: {
183+
write: common.mustCall(() => {
184+
throw new Error('write failed');
185+
}),
186+
},
187+
});
188+
189+
stream.on('close', common.mustCall());
190+
191+
assert.throws(() => stream.write('hello world\n'), { message: 'write failed' });
192+
stream.destroy();
193+
}

0 commit comments

Comments
 (0)