Skip to content

Commit

Permalink
fs: do not emit 'close' twice if emitClose enabled
Browse files Browse the repository at this point in the history
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: nodejs#31383
Fixes: nodejs#31366
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
ronag authored and Trott committed Jan 20, 2020
1 parent 57bd715 commit 7d5a86c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/internal/fs/streams.js
Expand Up @@ -272,7 +272,8 @@ function closeFsStream(stream, cb, err) {
er = er || err;
cb(er);
stream.closed = true;
if (!er)
const s = stream._writableState || stream._readableState;
if (!er && !s.emitClose)
stream.emit('close');
});

Expand Down
21 changes: 21 additions & 0 deletions test/parallel/test-file-write-stream4.js
@@ -0,0 +1,21 @@
'use strict';

// Test that 'close' emits once and not twice when `emitClose: true` is set.
// Refs: https://github.com/nodejs/node/issues/31366

const common = require('../common');
const path = require('path');
const fs = require('fs');

const tmpdir = require('../common/tmpdir');
tmpdir.refresh();

const filepath = path.join(tmpdir.path, 'write_pos.txt');

const fileReadStream = fs.createReadStream(process.execPath);
const fileWriteStream = fs.createWriteStream(filepath, {
emitClose: true
});

fileReadStream.pipe(fileWriteStream);
fileWriteStream.on('close', common.mustCall());

0 comments on commit 7d5a86c

Please sign in to comment.