Skip to content

PowerShell Kernel : Large number of exceptions causes vscode to hang cant save file or cancel rendering each error. #3205

@0xNinshu

Description

@0xNinshu

Describe the bug

I found a situation while writing PowerShell, which causes several unpleasant things to occur.

  • The cell output cannot be stopped.
  • Hundreds of exceptions are slowly rendered in order in the cell output.
  • The cell output bypasses the normal limits set by the extension.
  • Memory usage rises a lot over the course of minutes (15 minutes for 7 / 32gb of RAM).
  • VSCode must be closed to be able to use the notebook properly again.
  • When attempting to close vscode it hangs trying to save any changes.

Here is a notebook that if you run it will cause the issue every time. KernelProblem.ipynb - Gist

The issue is because of the way that exceptions are handled in PowerShell by default. The PowerShell engine by default when creating a runspace has the $ErrorActionPreference variable set the default value of Continue. I am not sure how you would stop the errors from being rendered one by one on the extensions side. But a simple solution to fix it temporarily or potentially permanently would be to set the ErrorActionPreference variable to Stop when initializing the runspace. This wouldnt stop users from changing the variable themselves if required.

Work Around

private PowerShell CreatePowerShell()
{
        // Set the distribution channel so telemetry can be distinguished in PS7+ telemetry
        Environment.SetEnvironmentVariable(PSTelemetryEnvName, PSTelemetryChannel);

        // Create PowerShell instance
        var iss = InitialSessionState.CreateDefault2();
        if (Platform.IsWindows)
        {
            // This sets the execution policy on Windows to RemoteSigned.
            iss.ExecutionPolicy = ExecutionPolicy.RemoteSigned;
        }

        // Set $PROFILE.
        var profileValue = DollarProfileHelper.GetProfileValue();
        iss.Variables.Add(new SessionStateVariableEntry("PROFILE", profileValue, "The $PROFILE."));

        var runspace = RunspaceFactory.CreateRunspace(_psHost, iss);
        runspace.Open();
		// This is the addition.
 		runspace.SessionStateProxy.SetVariable("ErrorActionPreference", ActionPreference.Stop);
        var pwsh = PowerShell.Create(runspace);

        // Add Modules directory that contains the helper modules
        var psJupyterModulePath = Path.Join(
            Path.GetDirectoryName(typeof(PowerShellKernel).Assembly.Location),
            "Modules");

        AddModulePath(psJupyterModulePath);
        RegisterForDisposal(pwsh);
        return pwsh;
}

Where: private PowerShell CreatePowerShell()

Please complete the following:

Which version of .NET Interactive are you using? (In a notebook, run the #!about magic command. ):

.NET Interactive

© 2020 Microsoft Corporation

Version: 1.0.446104+34cc09c68f37568da9c8151ba5c51d264d0375d5

Library version: 1.0.0-beta.23461.4+34cc09c68f37568da9c8151ba5c51d264d0375d5

Build date: 2023-09-16T03:08:19.4290313Z

https://github.com/dotnet/interactive

  • OS
    • Windows 11
    • Windows 10
    • macOS
    • Linux (Please specify distro)
    • iOS
    • Android
  • Browser
    • Chrome
    • Edge
    • Firefox
    • Safari
  • Frontend
    • Jupyter Notebook
    • Jupyter Lab
    • nteract
    • [ x] Visual Studio Code
    • Visual Studio Code Insiders
    • Visual Studio
    • Other (please specify)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-PowerShellSpecific to PowerShellbugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions