Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: don't emit 'close' twice if emitClose enabled #31383

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/internal/fs/streams.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';
ronag marked this conversation as resolved.
Show resolved Hide resolved

// 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();

ronag marked this conversation as resolved.
Show resolved Hide resolved
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());