@@ -36,6 +36,8 @@ internal class EditorServicesConsolePSHost : PSHost, IHostSupportsInteractiveSes
36
36
37
37
private readonly ReadLineProvider _readLineProvider ;
38
38
39
+ private readonly Stack < KeyValuePair < Runspace , RunspaceInfo > > _runspacesInUse ;
40
+
39
41
private string _localComputerName ;
40
42
41
43
private int _hostStarted = 0 ;
@@ -48,20 +50,22 @@ public EditorServicesConsolePSHost(
48
50
_logger = loggerFactory . CreateLogger < EditorServicesConsolePSHost > ( ) ;
49
51
_psFrameStack = new Stack < PowerShellContextFrame > ( ) ;
50
52
_psFactory = new PowerShellFactory ( loggerFactory , this ) ;
53
+ _runspacesInUse = new Stack < KeyValuePair < Runspace , RunspaceInfo > > ( ) ;
51
54
_hostInfo = hostInfo ;
52
55
Name = hostInfo . Name ;
53
56
Version = hostInfo . Version ;
54
57
55
58
_readLineProvider = new ReadLineProvider ( loggerFactory ) ;
56
59
_pipelineExecutor = new PipelineThreadExecutor ( loggerFactory , hostInfo , this , _readLineProvider ) ;
57
60
ExecutionService = new PowerShellExecutionService ( loggerFactory , this , _pipelineExecutor ) ;
58
- DebugContext = new PowerShellDebugContext ( loggerFactory , languageServer , this , _consoleReplRunner ) ;
59
61
UI = new EditorServicesConsolePSHostUserInterface ( loggerFactory , _readLineProvider , hostInfo . PSHost . UI ) ;
60
62
61
63
if ( hostInfo . ConsoleReplEnabled )
62
64
{
63
65
_consoleReplRunner = new ConsoleReplRunner ( loggerFactory , this , _readLineProvider , ExecutionService ) ;
64
66
}
67
+
68
+ DebugContext = new PowerShellDebugContext ( loggerFactory , languageServer , this , _consoleReplRunner ) ;
65
69
}
66
70
67
71
public override CultureInfo CurrentCulture => _hostInfo . PSHost . CurrentCulture ;
@@ -217,9 +221,28 @@ private void SetExit()
217
221
218
222
private void PushPowerShellAndRunLoop ( SMA . PowerShell pwsh , PowerShellFrameType frameType )
219
223
{
224
+ RunspaceInfo runspaceInfo = null ;
225
+ if ( _runspacesInUse . Count > 0 )
226
+ {
227
+ // This is more than just an optimization.
228
+ // When debugging, we cannot execute PowerShell directly to get this information;
229
+ // trying to do so will block on the command that called us, deadlocking execution.
230
+ // Instead, since we are reusing the runspace, we reuse that runspace's info as well.
231
+ KeyValuePair < Runspace , RunspaceInfo > currentRunspace = _runspacesInUse . Peek ( ) ;
232
+ if ( currentRunspace . Key == pwsh . Runspace )
233
+ {
234
+ runspaceInfo = currentRunspace . Value ;
235
+ }
236
+ }
237
+
238
+ if ( runspaceInfo is null )
239
+ {
240
+ RunspaceOrigin runspaceOrigin = pwsh . Runspace . RunspaceIsRemote ? RunspaceOrigin . EnteredProcess : RunspaceOrigin . Local ;
241
+ runspaceInfo = RunspaceInfo . CreateFromPowerShell ( _logger , pwsh , runspaceOrigin , _localComputerName ) ;
242
+ _runspacesInUse . Push ( new KeyValuePair < Runspace , RunspaceInfo > ( pwsh . Runspace , runspaceInfo ) ) ;
243
+ }
244
+
220
245
// TODO: Improve runspace origin detection here
221
- RunspaceOrigin runspaceOrigin = pwsh . Runspace . RunspaceIsRemote ? RunspaceOrigin . EnteredProcess : RunspaceOrigin . Local ;
222
- var runspaceInfo = RunspaceInfo . CreateFromPowerShell ( _logger , pwsh , runspaceOrigin , _localComputerName ) ;
223
246
PushPowerShellAndRunLoop ( new PowerShellContextFrame ( pwsh , runspaceInfo , frameType ) ) ;
224
247
}
225
248
0 commit comments