From d388a3586d3c0a72057131d2dfd506cef7cc3dbd Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Thu, 16 Jan 2020 18:42:54 +0100 Subject: [PATCH 1/3] fs: don't 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. Fixes: https://github.com/nodejs/node/issues/31366 --- lib/internal/fs/streams.js | 3 ++- test/parallel/test-file-write-stream4.js | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-file-write-stream4.js diff --git a/lib/internal/fs/streams.js b/lib/internal/fs/streams.js index d1603f6a024b81..561216c353c605 100644 --- a/lib/internal/fs/streams.js +++ b/lib/internal/fs/streams.js @@ -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'); }); diff --git a/test/parallel/test-file-write-stream4.js b/test/parallel/test-file-write-stream4.js new file mode 100644 index 00000000000000..9b5bc2419916f0 --- /dev/null +++ b/test/parallel/test-file-write-stream4.js @@ -0,0 +1,17 @@ +'use strict'; +const common = require('../common'); +const path = require('path'); +const fs = require('fs'); + +const tmpdir = require('../common/tmpdir'); + +const filepath = path.join(tmpdir.path, 'write_pos.txt'); +fs.mkdirSync(tmpdir.path, { recursive: true }); + +const fileReadStream = fs.createReadStream(process.execPath); +const fileWriteStream = fs.createWriteStream(filepath, { + emitClose: true +}); + +fileReadStream.pipe(fileWriteStream); +fileWriteStream.on('close', common.mustCall()); From 5fac02f97a79e2dec6bcd8e74276a5e84bd7975b Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Fri, 17 Jan 2020 08:56:26 +0100 Subject: [PATCH 2/3] fixup: test --- test/parallel/test-file-write-stream4.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-file-write-stream4.js b/test/parallel/test-file-write-stream4.js index 9b5bc2419916f0..a52a0e995c6e37 100644 --- a/test/parallel/test-file-write-stream4.js +++ b/test/parallel/test-file-write-stream4.js @@ -4,9 +4,9 @@ const path = require('path'); const fs = require('fs'); const tmpdir = require('../common/tmpdir'); +tmpdir.refresh(); const filepath = path.join(tmpdir.path, 'write_pos.txt'); -fs.mkdirSync(tmpdir.path, { recursive: true }); const fileReadStream = fs.createReadStream(process.execPath); const fileWriteStream = fs.createWriteStream(filepath, { From 26febd6f6068f4d4c7b686685882825927434c0d Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Sat, 18 Jan 2020 14:53:24 +0100 Subject: [PATCH 3/3] fixup --- test/parallel/test-file-write-stream4.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/parallel/test-file-write-stream4.js b/test/parallel/test-file-write-stream4.js index a52a0e995c6e37..2a81efcb66756f 100644 --- a/test/parallel/test-file-write-stream4.js +++ b/test/parallel/test-file-write-stream4.js @@ -1,4 +1,8 @@ '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');