-
Notifications
You must be signed in to change notification settings - Fork 29.1k
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
Workers only pipe one message to stdout before they stop running #53701
Comments
@nodejs/workers Possible duplicate of #40961 |
Not really the duplicate of #40961 If we turns in something that hold the import { Worker } from 'node:worker_threads';
let worker = new Worker(`function print_three_and_work(){
console.log("a");
console.log("b");
console.log("c");
// hold the thread, but allow async I/O
setTimeout(() => {}, 1000)
}
print_three_and_work();`, {eval: true, stdout: true});
worker.stdout.on("data", function(chunk){
process.stdout.write(chunk);
}); Or use sync operation and write to fd. import { Worker } from 'node:worker_threads';
let worker = new Worker(`
const { writeSync } = require('node:fs')
function print_three_and_work(){
writeSync(1, "a\\n")
writeSync(1, "b\\n")
writeSync(1, "c\\n")
while(true) {}
}
print_three_and_work();`, {eval: true, stdout: true});
worker.stdout.on("data", function(chunk){
process.stdout.write(chunk);
}); |
Why is there such a difference between the behaviour of console.log inside of workers and on the main thread? If i run the same function normally instead of giving it to a worker, it works at expected (if it didn't this weird behaviour would have been noticed by many before me): Code: function print_three_and_work(){
console.log("a");
console.log("b");
console.log("c");
while(true){}
}
print_three_and_work(); Output:
|
Oh ok, i didn't know that, thanks. Is there a way to force the console to flush all messages before continuing with sync work? |
You should use |
I recommend using Use pino for logging. Internally, it uses sonic-boom. Both can be set up to log synchronously, like so: const pino = require('pino')
const logger = pino(pino.destination({
dest: 1, // stdout
sync: true // Synchronous logging
}))
logger.info({ foo: 'bar' }, 'my message from %s', 'Italy') |
Version
22.4.0 (i also tried earlier versions and the bug is always present)
Platform
Both in Linux and Windows, all OS versions.
Subsystem
worker_threads
What steps will reproduce the bug?
or
How often does it reproduce? Is there a required condition?
It always happens.
What is the expected behavior? Why is that the expected behavior?
All logged messages should be piped to the main thread's stdout while the worker is running.
What do you see instead?
Only the first message is piped. All other messages are not piped while the worker is running. They get piped all at once once the worker stops running.
(I see "a" instead of seeing "a \n b \n c".)
Additional information
#40961
I believe this other issue has a similar problem but it was misunderstood as having something to do with "process.exit", while instead it must be because of something else.
The text was updated successfully, but these errors were encountered: