Skip to content

Commit 7d5a86c

Browse files
ronagTrott
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 57bd715 commit 7d5a86c

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lib/internal/fs/streams.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ function closeFsStream(stream, cb, err) {
272272
er = er || err;
273273
cb(er);
274274
stream.closed = true;
275-
if (!er)
275+
const s = stream._writableState || stream._readableState;
276+
if (!er && !s.emitClose)
276277
stream.emit('close');
277278
});
278279

+21
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)