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

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

Open
rjmholt opened this issue Oct 26, 2021 · 1 comment
Open

Comments

@rjmholt
Copy link
Collaborator

rjmholt commented Oct 26, 2021

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...
@ghost ghost added the Needs: Triage Maintainer attention needed! label Oct 26, 2021
@StevenBucher98 StevenBucher98 added Area-Engine Area-Threading Issue-Bug A bug to squash. and removed Needs: Triage Maintainer attention needed! labels Nov 16, 2021
@andyleejordan
Copy link
Member

andyleejordan commented Jan 20, 2022

We should consider this for telemetry. @StevenBucher98

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants