nodejs / node-v0.x-archive

Moved to https://github.com/nodejs/node

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

console.log / console.error are supposedly synchronous, but huge amounts of trailing data can be lost on process.exit

opened this issue · comments

I read on various sites that console.log is supposed to be synchronous in recent node.js versions. I just want to write a simple application that writes things to the terminal and exits.

However, as you can see with this simple example, huge amounts of output can be missing when simply using console.log and exiting:

let s = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."

for (var i = 0; i < 99999; i++) {
    console.log (i + "  " + s);
}
console.log ("pink");
console.error ("punk");
process.exit(0);

If you run it multiple times, you'll also get very random cutoffs for the stdout data. That goes completely against my expectations of a supposedly synchronous function..

How can console.log be used for safe output that actually arrives even when exiting the program? I would do a flush before exit, if I knew how..

Sorry I filed this on the wrong project on accident (this was supposed to be filed with the recent node.js version).