Skip to content

Commit 44058a7

Browse files
committed
Prevent RemoteFileManager from registering psedit in invalid sessions
This change prevents RemoteFileManager from registering the psedit function in any runspace that isn't an original remote runspace (not an attached process or debugged runspace) since events will only propagate back from an original session. Fixes PowerShell/vscode-powershell#575.
1 parent 47c3a4e commit 44058a7

File tree

1 file changed

+48
-40
lines changed

1 file changed

+48
-40
lines changed

src/PowerShellEditorServices/Session/RemoteFileManager.cs

+48-40
Original file line numberDiff line numberDiff line change
@@ -375,64 +375,72 @@ private void HandlePSEventReceived(object sender, PSEventArgs args)
375375

376376
private void RegisterPSEditFunction(RunspaceDetails runspaceDetails)
377377
{
378-
try
378+
if (runspaceDetails.Location == RunspaceLocation.Remote &&
379+
runspaceDetails.Context == RunspaceContext.Original)
379380
{
380-
runspaceDetails.Runspace.Events.ReceivedEvents.PSEventReceived += HandlePSEventReceived;
381+
try
382+
{
383+
runspaceDetails.Runspace.Events.ReceivedEvents.PSEventReceived += HandlePSEventReceived;
381384

382-
var createScript =
383-
string.Format(
384-
CreatePSEditFunctionScript,
385-
(runspaceDetails.Location == RunspaceLocation.Local &&
386-
runspaceDetails.Context == RunspaceContext.Original)
387-
? string.Empty : "-Forward");
385+
var createScript =
386+
string.Format(
387+
CreatePSEditFunctionScript,
388+
(runspaceDetails.Location == RunspaceLocation.Local &&
389+
runspaceDetails.Context == RunspaceContext.Original)
390+
? string.Empty : "-Forward");
388391

389-
PSCommand createCommand = new PSCommand();
390-
createCommand
391-
.AddScript(createScript)
392-
.AddParameter("PSEditFunction", PSEditFunctionScript);
392+
PSCommand createCommand = new PSCommand();
393+
createCommand
394+
.AddScript(createScript)
395+
.AddParameter("PSEditFunction", PSEditFunctionScript);
393396

394-
if (runspaceDetails.Context == RunspaceContext.DebuggedRunspace)
395-
{
396-
this.powerShellContext.ExecuteCommand(createCommand).Wait();
397-
}
398-
else
399-
{
400-
using (var powerShell = System.Management.Automation.PowerShell.Create())
397+
if (runspaceDetails.Context == RunspaceContext.DebuggedRunspace)
401398
{
402-
powerShell.Runspace = runspaceDetails.Runspace;
403-
powerShell.Commands = createCommand;
404-
powerShell.Invoke();
399+
this.powerShellContext.ExecuteCommand(createCommand).Wait();
400+
}
401+
else
402+
{
403+
using (var powerShell = System.Management.Automation.PowerShell.Create())
404+
{
405+
powerShell.Runspace = runspaceDetails.Runspace;
406+
powerShell.Commands = createCommand;
407+
powerShell.Invoke();
408+
}
405409
}
406410
}
407-
}
408-
catch (RemoteException e)
409-
{
410-
Logger.WriteException("Could not create psedit function.", e);
411+
catch (RemoteException e)
412+
{
413+
Logger.WriteException("Could not create psedit function.", e);
414+
}
411415
}
412416
}
413417

414418
private void RemovePSEditFunction(RunspaceDetails runspaceDetails)
415419
{
416-
try
420+
if (runspaceDetails.Location == RunspaceLocation.Remote &&
421+
runspaceDetails.Context == RunspaceContext.Original)
417422
{
418-
if (runspaceDetails.Runspace.Events != null)
423+
try
419424
{
420-
runspaceDetails.Runspace.Events.ReceivedEvents.PSEventReceived -= HandlePSEventReceived;
421-
}
425+
if (runspaceDetails.Runspace.Events != null)
426+
{
427+
runspaceDetails.Runspace.Events.ReceivedEvents.PSEventReceived -= HandlePSEventReceived;
428+
}
422429

423-
if (runspaceDetails.Runspace.RunspaceStateInfo.State == RunspaceState.Opened)
424-
{
425-
using (var powerShell = System.Management.Automation.PowerShell.Create())
430+
if (runspaceDetails.Runspace.RunspaceStateInfo.State == RunspaceState.Opened)
426431
{
427-
powerShell.Runspace = runspaceDetails.Runspace;
428-
powerShell.Commands.AddScript(RemovePSEditFunctionScript);
429-
powerShell.Invoke();
432+
using (var powerShell = System.Management.Automation.PowerShell.Create())
433+
{
434+
powerShell.Runspace = runspaceDetails.Runspace;
435+
powerShell.Commands.AddScript(RemovePSEditFunctionScript);
436+
powerShell.Invoke();
437+
}
430438
}
431439
}
432-
}
433-
catch (Exception e) when (e is RemoteException || e is PSInvalidOperationException)
434-
{
435-
Logger.WriteException("Could not remove psedit function.", e);
440+
catch (Exception e) when (e is RemoteException || e is PSInvalidOperationException)
441+
{
442+
Logger.WriteException("Could not remove psedit function.", e);
443+
}
436444
}
437445
}
438446

0 commit comments

Comments
 (0)