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 <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
  • Loading branch information
ronag authored and codebytere committed Feb 17, 2020
1 parent 843c5c6 commit 2c2b3ba
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 @@ -269,7 +269,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 2c2b3ba

Please sign in to comment.