@@ -63,6 +63,8 @@ internal class PsesInternalHost : PSHost, IHostSupportsInteractiveSession, IRuns
63
63
64
64
private bool _skipNextPrompt = false ;
65
65
66
+ private bool _resettingRunspace = false ;
67
+
66
68
public PsesInternalHost (
67
69
ILoggerFactory loggerFactory ,
68
70
ILanguageServerFacade languageServer ,
@@ -343,13 +345,19 @@ public Task SetInitialWorkingDirectoryAsync(string path, CancellationToken cance
343
345
344
346
private void Run ( )
345
347
{
346
- SMA . PowerShell pwsh = CreateInitialPowerShell ( _hostInfo , _readLineProvider ) ;
347
- RunspaceInfo localRunspaceInfo = RunspaceInfo . CreateFromLocalPowerShell ( _logger , pwsh ) ;
348
+ ( PowerShell pwsh , RunspaceInfo localRunspaceInfo ) = CreateInitialPowerShellSession ( ) ;
348
349
_localComputerName = localRunspaceInfo . SessionDetails . ComputerName ;
349
350
_runspaceStack . Push ( new RunspaceFrame ( pwsh . Runspace , localRunspaceInfo ) ) ;
350
351
PushPowerShellAndRunLoop ( pwsh , PowerShellFrameType . Normal , localRunspaceInfo ) ;
351
352
}
352
353
354
+ private ( PowerShell , RunspaceInfo ) CreateInitialPowerShellSession ( )
355
+ {
356
+ PowerShell pwsh = CreateInitialPowerShell ( _hostInfo , _readLineProvider ) ;
357
+ RunspaceInfo localRunspaceInfo = RunspaceInfo . CreateFromLocalPowerShell ( _logger , pwsh ) ;
358
+ return ( pwsh , localRunspaceInfo ) ;
359
+ }
360
+
353
361
private void PushPowerShellAndRunLoop ( SMA . PowerShell pwsh , PowerShellFrameType frameType , RunspaceInfo newRunspaceInfo = null )
354
362
{
355
363
// TODO: Improve runspace origin detection here
@@ -392,14 +400,7 @@ private RunspaceInfo GetRunspaceInfoForPowerShell(SMA.PowerShell pwsh, out bool
392
400
393
401
private void PushPowerShellAndRunLoop ( PowerShellContextFrame frame )
394
402
{
395
- if ( _psFrameStack . Count > 0 )
396
- {
397
- RemoveRunspaceEventHandlers ( CurrentFrame . PowerShell . Runspace ) ;
398
- }
399
-
400
- AddRunspaceEventHandlers ( frame . PowerShell . Runspace ) ;
401
-
402
- _psFrameStack . Push ( frame ) ;
403
+ PushPowerShell ( frame ) ;
403
404
404
405
try
405
406
{
@@ -422,7 +423,19 @@ private void PushPowerShellAndRunLoop(PowerShellContextFrame frame)
422
423
}
423
424
}
424
425
425
- private void PopPowerShell ( )
426
+ private void PushPowerShell ( PowerShellContextFrame frame )
427
+ {
428
+ if ( _psFrameStack . Count > 0 )
429
+ {
430
+ RemoveRunspaceEventHandlers ( CurrentFrame . PowerShell . Runspace ) ;
431
+ }
432
+
433
+ AddRunspaceEventHandlers ( frame . PowerShell . Runspace ) ;
434
+
435
+ _psFrameStack . Push ( frame ) ;
436
+ }
437
+
438
+ private void PopPowerShell ( RunspaceChangeAction runspaceChangeAction = RunspaceChangeAction . Exit )
426
439
{
427
440
_shouldExit = false ;
428
441
PowerShellContextFrame frame = _psFrameStack . Pop ( ) ;
@@ -436,7 +449,7 @@ private void PopPowerShell()
436
449
RunspaceFrame currentRunspaceFrame = _runspaceStack . Peek ( ) ;
437
450
RemoveRunspaceEventHandlers ( previousRunspaceFrame . Runspace ) ;
438
451
AddRunspaceEventHandlers ( currentRunspaceFrame . Runspace ) ;
439
- RunspaceChanged ? . Invoke ( this , new RunspaceChangedEventArgs ( RunspaceChangeAction . Exit , previousRunspaceFrame . RunspaceInfo , currentRunspaceFrame . RunspaceInfo ) ) ;
452
+ RunspaceChanged ? . Invoke ( this , new RunspaceChangedEventArgs ( runspaceChangeAction , previousRunspaceFrame . RunspaceInfo , currentRunspaceFrame . RunspaceInfo ) ) ;
440
453
}
441
454
}
442
455
finally
@@ -730,37 +743,40 @@ private void OnBreakpointUpdated(object sender, BreakpointUpdatedEventArgs break
730
743
731
744
private void OnRunspaceStateChanged ( object sender , RunspaceStateEventArgs runspaceStateEventArgs )
732
745
{
733
- if ( ! _shouldExit && ! runspaceStateEventArgs . RunspaceStateInfo . IsUsable ( ) )
746
+ if ( ! _shouldExit && ! _resettingRunspace && ! runspaceStateEventArgs . RunspaceStateInfo . IsUsable ( ) )
734
747
{
735
- //PopOrReinitializeRunspaceAsync();
748
+ _resettingRunspace = true ;
749
+ PopOrReinitializeRunspaceAsync ( ) . HandleErrorsAsync ( _logger ) ;
736
750
}
737
751
}
738
752
739
- /*
740
- private void PopOrReinitializeRunspace()
753
+ private Task PopOrReinitializeRunspaceAsync ( )
741
754
{
742
- SetExit ();
755
+ _cancellationContext . CancelCurrentTaskStack ( ) ;
743
756
RunspaceStateInfo oldRunspaceState = CurrentPowerShell . Runspace . RunspaceStateInfo ;
744
757
745
758
// Rather than try to lock the PowerShell executor while we alter its state,
746
759
// we simply run this on its thread, guaranteeing that no other action can occur
747
- _executor.InvokeDelegate (
748
- nameof(PopOrReinitializeRunspace ),
760
+ return ExecuteDelegateAsync (
761
+ nameof ( PopOrReinitializeRunspaceAsync ) ,
749
762
new ExecutionOptions { InterruptCurrentForeground = true } ,
750
763
( cancellationToken ) =>
751
764
{
752
765
while ( _psFrameStack . Count > 0
753
766
&& ! _psFrameStack . Peek ( ) . PowerShell . Runspace . RunspaceStateInfo . IsUsable ( ) )
754
767
{
755
- PopPowerShell();
768
+ PopPowerShell ( RunspaceChangeAction . Shutdown ) ;
756
769
}
757
770
771
+ _resettingRunspace = false ;
772
+
758
773
if ( _psFrameStack . Count == 0 )
759
774
{
760
775
// If our main runspace was corrupted,
761
776
// we must re-initialize our state.
762
777
// TODO: Use runspace.ResetRunspaceState() here instead
763
- PushInitialPowerShell();
778
+ ( PowerShell pwsh , RunspaceInfo runspaceInfo ) = CreateInitialPowerShellSession ( ) ;
779
+ PushPowerShell ( new PowerShellContextFrame ( pwsh , runspaceInfo , PowerShellFrameType . Normal ) ) ;
764
780
765
781
_logger . LogError ( $ "Top level runspace entered state '{ oldRunspaceState . State } ' for reason '{ oldRunspaceState . Reason } ' and was reinitialized."
766
782
+ " Please report this issue in the PowerShell/vscode-PowerShell GitHub repository with these logs." ) ;
@@ -776,7 +792,6 @@ private void PopOrReinitializeRunspace()
776
792
} ,
777
793
CancellationToken . None ) ;
778
794
}
779
- */
780
795
781
796
private record RunspaceFrame (
782
797
Runspace Runspace ,
0 commit comments