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: #31383
Fixes: #31366
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
ronag authored and codebytere committed Mar 17, 2020
1 parent c7f03ad commit fc9c6c3
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
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,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
Original file line number Diff line number Diff line change
@@ -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 fc9c6c3

Please sign in to comment.