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

vm: timeout raised by vm.runInContext breaks console.log #34678

Open
bchabrier opened this issue Aug 8, 2020 · 0 comments
Open

vm: timeout raised by vm.runInContext breaks console.log #34678

bchabrier opened this issue Aug 8, 2020 · 0 comments
Labels
console Issues and PRs related to the console subsystem. stdio Issues and PRs related to stdio. vm Issues and PRs related to the vm subsystem.

Comments

@bchabrier
Copy link

What steps will reproduce the bug?

I have created the following snippet to reproduce the problem:

const vm = require('vm');

let context = vm.createContext({ process: process });

let countErr = 0;
while (process.stdout._writableState.bufferedRequestCount == 0 && countErr < 10) {
    try {
        vm.runInContext("while (true) {process.stdout.write('');}",
            context,
            { timeout: 100 });
    } catch (err) {
        countErr++;
        console.warn(err.message);
    }
}
console.error("stdout bufferedRequestCount:", process.stdout._writableState.bufferedRequestCount);
console.error('Needed to raise that many timeouts to show the problem:', countErr);
console.log('Log in main still working'); // after some timeouts, this line does not print anymore
console.error("The previous line should be 'Log in main still working', if it is not, stdout seems to be broken...");

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

Issue happens systematically, after a couple of timeouts raised (not always the same number)

What is the expected behavior?

Script execution timed out.
Script execution timed out.
Script execution timed out.
Script execution timed out.
Script execution timed out.
Script execution timed out.
Script execution timed out.
Script execution timed out.
Script execution timed out.
Script execution timed out.
stdout bufferedRequestCount: 0
Needed to raise that many timeouts to show the problem: 10
Log in main still working
The previous line should be 'Log in main still working', if it is not, stdout seems to be broken...

What do you see instead?

Script execution timed out.
Script execution timed out.
Script execution timed out.
stdout bufferedRequestCount: 4564
Needed to raise that many timeouts to show the problem: 3
The previous line should be 'Log in main still working', if it is not, stdout seems to be broken...

Additional information

Some points that can be helpful:

  • The issue is raised with vm.runInContext, vm.runInNewContext, vm.runInThisContext.
  • The same issue happens with process.stderr, so it's not specific to process.stdout.
  • No event (error, drain...) is emitted by process.stdout.
  • I could figure out that, when a timeout is raised (might not be the first one), process.stdout starts to buffer the process.stdout.write requests and process.stdout.write calls will return false. When too many, process.stdout will eventually enter in needDrain mode. However, the drain event is never emitted. It looks like something prevents the data to be output on the terminal, the buffer of process.stdout is never processed, leaving process.stdout in a needDrain mode forever.

Note that the actual code I want to run in my project is not exactly the snippet. It has no infinite loop, it just runs a piece of code that makes use of console.log to trace different steps, and it happens regularly that it timeouts. Hence, my project requires the capability to use console.log, have a timeout raised, and still be able to use console.log after that.

Any idea about this? Any workaround?

@targos targos added console Issues and PRs related to the console subsystem. vm Issues and PRs related to the vm subsystem. labels Dec 27, 2020
@BridgeAR BridgeAR added the stdio Issues and PRs related to stdio. label Nov 29, 2021
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. stdio Issues and PRs related to stdio. vm Issues and PRs related to the vm subsystem.
Projects
None yet
Development

No branches or pull requests

3 participants