Skip to content
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

console.log stops printing in while loop #44577

Closed
kenorb opened this issue Sep 8, 2022 · 3 comments
Closed

console.log stops printing in while loop #44577

kenorb opened this issue Sep 8, 2022 · 3 comments
Labels
console Issues and PRs related to the console subsystem.

Comments

@kenorb
Copy link

kenorb commented Sep 8, 2022

Version

v16.13.1

Platform

Linux Ubuntu-ROG 5.15.0-47-generic #51-Ubuntu SMP Thu Aug 11 07:51:15 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

I'm using fake-words module (npm install fake-words) with the following simple code:

#!/usr/bin/env node
const fake = require("fake-words");
while (true) {
  console.log(fake.sentence());
}

When I run ./genwords.js, everything works as expected.

However when I pipe into external program (on Ubuntu shell), the generation of words stops after a second.

$ ./genwords.js | cat
...
(output generation stops after a second)
$ ./genwords.js | tee
...
(stuck as well)
$ ./genwords.js | pv -l
...
4.64k 0:00:13 [0.00 /s]

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

Always, on Linux and macOS, but not on Windows.

What is the expected behavior?

console.log printing lines constantly.

What do you see instead?

Nothing. Printing stops after a second.

Additional information

I've posted a question here, but this seems more like a bug.

@himself65 himself65 added the console Issues and PRs related to the console subsystem. label Sep 8, 2022
@Trott
Copy link
Member

Trott commented Sep 8, 2022

Duplicate of #6379?

@Trott
Copy link
Member

Trott commented Sep 8, 2022

The behavior of console.log() in code (such as the while loop in your example) that never relinquishes control to the event loop (especially when piped to another process) in Node.js is a longstanding...uh...quirk. It's a lot harder to fix than you might think. Here's the relevant issue in the tracker: #6379

Or more specifically on the whole handling-when-piped-to-another-process issue: #1741

You can work around the issue by restructuring the code to relinquish control to the event loop. Here is one possibility.

#!/usr/bin/env node
const fake = require("fake-words");

function getWords() {
  console.log(fake.sentence());
  setImmediate(getWords);
}

getWords()

@kenorb
Copy link
Author

kenorb commented Sep 8, 2022

You can work around the issue by restructuring the code to relinquish control to the event loop.

Thank you, that works as expected.

@Trott Trott closed this as completed Sep 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
console Issues and PRs related to the console subsystem.
Projects
None yet
Development

No branches or pull requests

3 participants