diff --git a/lib/internal/worker/io.js b/lib/internal/worker/io.js index 63c8a8d9763220..ba2150e530b5be 100644 --- a/lib/internal/worker/io.js +++ b/lib/internal/worker/io.js @@ -169,8 +169,10 @@ class ReadableWorkerStdio extends Readable { this[kIncrementsPortRef] = true; this[kStartedReading] = false; this.on('end', () => { - if (this[kIncrementsPortRef] && --this[kPort][kWaitingStreams] === 0) - this[kPort].unref(); + if (this[kStartedReading] && this[kIncrementsPortRef]) { + if (--this[kPort][kWaitingStreams] === 0) + this[kPort].unref(); + } }); } diff --git a/test/parallel/test-worker-no-stdin-stdout-interaction.js b/test/parallel/test-worker-no-stdin-stdout-interaction.js new file mode 100644 index 00000000000000..6febbd07b566fa --- /dev/null +++ b/test/parallel/test-worker-no-stdin-stdout-interaction.js @@ -0,0 +1,20 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const { Worker, isMainThread } = require('worker_threads'); + +// Regression test for https://github.com/nodejs/node/issues/28144. + +if (isMainThread) { + const w = new Worker(__filename); + w.on('exit', common.mustCall((status) => { + assert.strictEqual(status, 0); + })); + w.stdout.on('data', common.mustCall(10)); +} else { + process.stdin.on('data', () => {}); + + for (let i = 0; i < 10; ++i) { + process.stdout.write(`processing(${i})\n`, common.mustCall()); + } +}