vscode icon indicating copy to clipboard operation
vscode copied to clipboard

Infinite loop sending output to an output channel from an extension makes VS Code stop responding

Open digeff opened this issue 10 years ago • 13 comments

image

Previous statement was wrong: Constantly sending output to an output channel (as in an infinite loop printing data) from an extension makes VS Code stop responding. This happened both with the stable and alpha release.

digeff avatar Mar 02 '16 02:03 digeff

@alexandrudima I would believe this is a general issue and not so much about output. Any extension can trigger API in an endless loop and that will likely spam the renderer enough to make it unresponsive. Do we do some throttling with regards to data traveling from a spammy extension back to the main side?

bpasero avatar Mar 02 '16 11:03 bpasero

If an extension enters an infinite loop, AFAIK we have no graceful way to terminate it besides killing the extension host process.

alexdima avatar Mar 03 '16 08:03 alexdima

I will investigate if buffering the output service is something that helps here but it would not help in all cases of abuse.

bpasero avatar Mar 03 '16 08:03 bpasero

@digeff I am not able to reproduce with a simple example, is it possible to share the extension with me to reproduce?

bpasero avatar Mar 03 '16 10:03 bpasero

@bpasero Sorry for the delay, I meant something like this (This makes my VS Code unresponsive consistently):

export function activate(context: vscode.ExtensionContext) {
    let disposable = vscode.commands.registerCommand('extension.sayHello', () => {
        vscode.window.showInformationMessage('Crashing!');
        lotsOfOutput();
    });

    context.subscriptions.push(disposable);
}

function lotsOfOutput() {
    setTimeout(() => {
        const outputChannel = vscode.window.createOutputChannel("Test");
        outputChannel.show();
        outputChannel.appendLine("Stop working please...");
        lotsOfOutput();
        lotsOfOutput();
    }, 100);
}

digeff avatar Mar 08 '16 22:03 digeff

@isidorn can you try this out? asking because you recently pushed a change to handle output updates with a 300ms delay directly in the UI layer and maybe we are good to go already.

bpasero avatar Mar 09 '16 06:03 bpasero

So the example that the user provided still can crash vscode because it is a quadratic function which creates new output, shows the output channel and appends the output. However the following can no longer crash / slow down vscode

const outputChannel = vscode.window.createOutputChannel("Test");
setInterval(() => {
    outputChannel.appendLine("Stop working please...");
}, 10);

isidorn avatar Mar 09 '16 16:03 isidorn

Sorry, have to reopen, would like to understand why we still crash and if we can prevent that.

bpasero avatar Mar 09 '16 16:03 bpasero

@isidorn What if you set the interval time to 0?

digeff avatar Mar 09 '16 16:03 digeff

Ok, the cause of the crash was the multiple calls to show(), now I protect against that. Please note that the original sample provided by @digeff still crashes after some time since it converges to infinity.

@digeff regarding interval 0 https://stackoverflow.com/questions/7648557/setinterval-behaviour-with-0-milliseconds-in-javascript

isidorn avatar Mar 09 '16 16:03 isidorn

@isidorn What if we use process.nextTick instead of interval 0? (Basically, I want to simulate adding output as fast as possible, while not yet converging to infinity).

digeff avatar Mar 09 '16 16:03 digeff

@digeff if I setInterval for 1ms it does not crash any more with the latest fixes

isidorn avatar Mar 09 '16 16:03 isidorn

:heart:

bpasero avatar Mar 09 '16 16:03 bpasero