Skip to content

Commit 2c2b3ba

Browse files
ronagcodebytere
authored andcommitted
fs: do not emit 'close' twice if emitClose enabled
fs streams have some backwards compat behavior that does not behave well if emitClose: true is passed in options. This fixes this edge case until the backwards compat is removed. PR-URL: #31383 Fixes: #31366 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 843c5c6 commit 2c2b3ba

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lib/internal/fs/streams.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,8 @@ function closeFsStream(stream, cb, err) {
269269
er = er || err;
270270
cb(er);
271271
stream.closed = true;
272-
if (!er)
272+
const s = stream._writableState || stream._readableState;
273+
if (!er && !s.emitClose)
273274
stream.emit('close');
274275
});
275276

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
// Test that 'close' emits once and not twice when `emitClose: true` is set.
4+
// Refs: https://github.com/nodejs/node/issues/31366
5+
6+
const common = require('../common');
7+
const path = require('path');
8+
const fs = require('fs');
9+
10+
const tmpdir = require('../common/tmpdir');
11+
tmpdir.refresh();
12+
13+
const filepath = path.join(tmpdir.path, 'write_pos.txt');
14+
15+
const fileReadStream = fs.createReadStream(process.execPath);
16+
const fileWriteStream = fs.createWriteStream(filepath, {
17+
emitClose: true
18+
});
19+
20+
fileReadStream.pipe(fileWriteStream);
21+
fileWriteStream.on('close', common.mustCall());

0 commit comments

Comments
 (0)