Skip to content

Verify the main runspace reset method in the PSES pipeline thread consumer #1595

Open
@rjmholt

Description

@rjmholt

We have a kind of primitive stab at a method to clean up a faulty runspace state in the PSES pipeline thread consumer here:

private Task PopOrReinitializeRunspaceAsync()
{
_cancellationContext.CancelCurrentTaskStack();
RunspaceStateInfo oldRunspaceState = CurrentPowerShell.Runspace.RunspaceStateInfo;
// Rather than try to lock the PowerShell executor while we alter its state,
// we simply run this on its thread, guaranteeing that no other action can occur
return ExecuteDelegateAsync(
nameof(PopOrReinitializeRunspaceAsync),
new ExecutionOptions { InterruptCurrentForeground = true },
(cancellationToken) =>
{
while (_psFrameStack.Count > 0
&& !_psFrameStack.Peek().PowerShell.Runspace.RunspaceStateInfo.IsUsable())
{
PopPowerShell(RunspaceChangeAction.Shutdown);
}
_resettingRunspace = false;
if (_psFrameStack.Count == 0)
{
// If our main runspace was corrupted,
// we must re-initialize our state.
// TODO: Use runspace.ResetRunspaceState() here instead
(PowerShell pwsh, RunspaceInfo runspaceInfo, EngineIntrinsics engineIntrinsics) = CreateInitialPowerShellSession();
_mainRunspaceEngineIntrinsics = engineIntrinsics;
PushPowerShell(new PowerShellContextFrame(pwsh, runspaceInfo, PowerShellFrameType.Normal));
_logger.LogError($"Top level runspace entered state '{oldRunspaceState.State}' for reason '{oldRunspaceState.Reason}' and was reinitialized."
+ " Please report this issue in the PowerShell/vscode-PowerShell GitHub repository with these logs.");
UI.WriteErrorLine("The main runspace encountered an error and has been reinitialized. See the PowerShell extension logs for more details.");
}
else
{
_logger.LogError($"Current runspace entered state '{oldRunspaceState.State}' for reason '{oldRunspaceState.Reason}' and was popped.");
UI.WriteErrorLine($"The current runspace entered state '{oldRunspaceState.State}' for reason '{oldRunspaceState.Reason}'."
+ " If this occurred when using Ctrl+C in a Windows PowerShell remoting session, this is expected behavior."
+ " The session is now returning to the previous runspace.");
}
},
CancellationToken.None);
}

We need to induce a fault and see if this works as expected, as well as ensure it doesn't run at other times...

My suspicion is that it will need some refinement and could have some significant bugs in it. But it's currently a corner case and a worse-case scenario handler anyway (if it didn't exist, we are essentially crashed).

So we need to investigate this and see how we can improve:

  • detection of bad runspace state
  • restoration of good runspace state
  • how our threading mechanisms work in this scenario...

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions