Skip to content

workers: worker doesn't write all data to stdout stream with NODE_OPTIONS --require #31777

Description

@lundibundi
  • Version: master
  • Platform: Linux (at least)
  • Subsystem: workers

What steps will reproduce the bug?

// main.js
'use strict';

const { Worker } = require('worker_threads');
const assert = require('assert');

process.on('unhandledRejection', (err) => { throw err; });

async function collectStream(readable) {
  readable.setEncoding('utf8');
  let data = '';
  for await (const chunk of readable) {
    data += chunk;
  }
  return data;
}

for (let i = 0; i < 100; i++) {
  const w = new Worker('console.log("B");', {
    env: { NODE_OPTIONS: '--require ./printA.js' },
    eval: true,
    stdout: true
  });
  w.on('exit', () => {
    collectStream(w.stdout).then((data) => {
      assert.strictEqual(data, 'A\nB\n');
    });
  });
}
// printA.js
console.log('A');

How often does it reproduce? Is there a required condition?

Quite often (2/10 runs), under heavy load the frequency is higher.

What is the expected behavior?

Worker prints all data to the stdout stream.

What do you see instead?

Worker only prints part of the written data to the stdout stream.

Example of the test case above if we replace assert with just console.log(JSON.stringify(data)):

 ➔ dev/node/node ./out/Release/node test-worker-stdout-finish.js                                                                                                                                                                             ⇡ master :: ● :: ⬡
"A\nB\n"
"A\nB\n"
"A\nB\n"
"A\nB\n"
"A\nB\n"
"A\nB\n"
"A\nB\n"
"A\n"
"A\n"
"A\nB\n"

Additional information

Weirdly enough this only happens for NODE_OPTIONS --require cases, if the worker is requireing the file by itself or just has multiple console.log statements the bug is not present.
This also happens without the eval: true if we just run a file with console.log("B").

/cc @nodejs/workers

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    workerIssues and PRs related to the worker_threads module and Worker API.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions