Skip to content

Commit 5237aaa

Browse files
committed
Created a nested PowerShell for the top-level root
So that events registered with the `OnIdle` engine event handler will always work.
1 parent f234a8e commit 5237aaa

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs

+13-2
Original file line numberDiff line numberDiff line change
@@ -475,8 +475,19 @@ private void Run()
475475
(PowerShell pwsh, RunspaceInfo localRunspaceInfo, EngineIntrinsics engineIntrinsics) = CreateInitialPowerShellSession();
476476
_mainRunspaceEngineIntrinsics = engineIntrinsics;
477477
_localComputerName = localRunspaceInfo.SessionDetails.ComputerName;
478-
_runspaceStack.Push(new RunspaceFrame(pwsh.Runspace, localRunspaceInfo));
479-
PushPowerShellAndRunLoop(pwsh, PowerShellFrameType.Normal | PowerShellFrameType.Repl, localRunspaceInfo);
478+
479+
// NOTE: In order to support running events registered to PowerShell's OnIdle
480+
// handler, we have to have our top-level PowerShell instance be nested (otherwise
481+
// we get a PSInvalidOperationException because pipelines cannot be run
482+
// concurrently). Specifically this bug cropped up when a profile loaded code which
483+
// registered (and subsequently ran) on the OnIdle handler since it was hitting the
484+
// non-nested PowerShell instance. So now we just start with a nested instance.
485+
PowerShell nestedPwsh = CreateNestedPowerShell(localRunspaceInfo);
486+
_runspaceStack.Push(new RunspaceFrame(nestedPwsh.Runspace, localRunspaceInfo));
487+
488+
// TODO: Should this now be marked as a Nested frame, even though for our purposes
489+
// it isn't really nested?
490+
PushPowerShellAndRunLoop(nestedPwsh, PowerShellFrameType.Normal | PowerShellFrameType.Repl, localRunspaceInfo);
480491
}
481492
catch (Exception e)
482493
{

0 commit comments

Comments
 (0)