vscode-powershell icon indicating copy to clipboard operation
vscode-powershell copied to clipboard

PowerShell extension doesn't stop powershell Integrated Console process when project is closed

Open peetrike opened this issue 3 years ago • 24 comments

Prerequisites

  • [X] I have written a descriptive issue title.
  • [X] I have searched all issues to ensure it has not already been reported.
  • [X] I have read the troubleshooting guide.
  • [X] I have verified that I am using the latest version of Visual Studio Code and the PowerShell extension.

Summary

Every time I open new project folder, the PowerShell extension starts new PowerShell process. Closing folder doesn't stop now redundant PowerShell process(es). Opening next project (folder), starts new process. Opening scirpt in separate VS Code window starts yet again new PowerShell process (and closing that window leaves PowerShell process running).

After switching between several projects, that makes quite some bit of memory.

The same occurred with PowerShell-Preview extension. It also doesn't matter, which version of PowerShell is used by extension: Windows PowerShell 5.1 is loaded and left running the same way.

All processes are stopped, when last VS Code window will be closed.

PowerShell Version

PS ❯ $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.1.3
PSEdition                      Core
GitCommitId                    7.1.3
OS                             Microsoft Windows 10.0.19042
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}       
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visual Studio Code Version

PS ❯ code --version
1.58.2
c3f126316369cd610563c75b1b1725e0679adfb3
x64

Extension Version

PS ❯ code --list-extensions --show-versions | Select-String powershell

[email protected] (enabled)
[email protected] (disabled)

Steps to Reproduce

  1. Open project containing PowerShell files. Have some PowerShell files opened (to get PowerShell extension started)
  2. Open another project (and some PowerShell files)
  3. Open code with .ps1 file from command line (code -n file.ps1)

Now there will be 3 PowerShell processes visible in VS Code Project Explorer and in Windows Task Manager.

Visuals

This is VS Code Process Explorer after opening 2 project folders and one file in separate window: image

Logs

No response

peetrike avatar Jul 30 '21 14:07 peetrike

It might be connected to the Workspace Trust feature (of VS Code) introduced recently. I have added next configuration to my settings.json:

  "extensions.supportUntrustedWorkspaces": {
    "ms-vscode.powershell": {
      "supported": true
    },
    "ms-vscode.powershell-preview": {
      "supported": true
    },
  },

peetrike avatar Jul 30 '21 14:07 peetrike

@peetrike Can you please clarify: do you have these projects marked "trusted" or "untrusted"? I have to assume "trusted" or the extension wouldn't/shouldn't be starting in the first place at all.

andyleejordan avatar Aug 03 '21 22:08 andyleejordan

@peetrike Can you please clarify: do you have these projects marked "trusted" or "untrusted"? I have to assume "trusted" or the extension wouldn't/shouldn't be starting in the first place at all.

I have those projects marked as trusted. And configuration mentioned above marks the PowerShell extension always trusted.

I usually switch between projects several times a day and in the past there was no problems. But then recently I noticed, that ~20 GB of my machine memory was taken by pwsh processes, while I had only couple of them running (to my knowledge). So it might be connected to recent (within couple of months) change in either VS Code or PowerShell extension. Or change in my configuration (the one connected to workspace trust).

peetrike avatar Aug 04 '21 05:08 peetrike

Sounds like the changes made upstream to VS Code isn't allowing for spawned processes to be closed :/

andyleejordan avatar Aug 10 '21 21:08 andyleejordan

@TylerLeonhardt is this something VS Code could look at?

andyleejordan avatar Aug 24 '21 21:08 andyleejordan

@Tyriar @meganrogge

TylerLeonhardt avatar Aug 24 '21 21:08 TylerLeonhardt

Does this still happen? Can you link where the process in question gets created?

Tyriar avatar Oct 14 '21 15:10 Tyriar

Yes, VS Code v1.61.1 + PowerShell extension v2021.10.0 still have same issue

peetrike avatar Oct 18 '21 06:10 peetrike

Thanks @peetrike, we had a few clarifying questions we want to get answered to help us get a better picture of the issue

  • Which processes the integrated console can get to? You can work out which process by executing $pid and will give you the pid's back. We want to work out which process is the "zombie" one.

-Are you able to access the third process through the drop down of the vscode terminal display?

-What, if anything happens, when you kill that third process that is not using a lot of memory. We suspect it might show nothing. The low memory usage to us, makes us suspect it might be trying to shut down but has some handle that it can't shut down.

StevenBucher98 avatar Oct 18 '21 17:10 StevenBucher98

The pwsh process that's left running is the one that is started by PowerShell extension to run it's functionality. Every next open of folder with PowerShell files opens next instance of PowerShell extension. The same occurs when starting .ps1 script file from command line (with code -n). Closing folder (or separate script window) doesn't end the PowerShell extension pwsh process.

I can kill all those processes, either from VS Code Process Explorer, from command line or from Task Manager. It's easier to do from VS Code Process Explorer, when there are more pwsh processes from Windows Terminal and other programs.

It seems that something prevents PowerShell Extension to end it's process or it forgets about that. That wasn't a problem some time ago, so I assume that this result was introduced in spring/summer updates either to PowerShell Extension or VS Code.

peetrike avatar Oct 18 '21 21:10 peetrike

The pwsh process that's left running is the one that is started by PowerShell extension to run it's functionality

That much is apparent from the startup string displayed in the process explorer. But we need more information:

  • Of the three processes, which is the one not running an Integrated Console? Is it the one with low memory consumption?
  • Is the zombie PowerShell process still attached or accessible from VSCode in any other way? For example, does it appear as a terminal in any other pane in VSCode? Does the VSCode process explorer imply any kind of attachment or registration with VSCode, or is it just a survey of subprocesses of VSCode?
  • Does anything happen if you kill the zombie process?
  • It looks like you didn't include logs here. If you can collect them (they will have the PID in them to confirm you have the right process), they might offer insight into what's going on in the process

It seems that something prevents PowerShell Extension to end it's process or it forgets about that

Exactly, and we need to work out what that PowerShell process is doing or waiting on when it should be shutting down. In addition to the questions above, the next step is probably to collect a dump from the process to see where it's stalling.

rjmholt avatar Oct 18 '21 22:10 rjmholt

There will be as many processes as many times the PowerShell extension spawns new instance. It means that every time I open new folder (or open script in new window), the new process is created. And these processes don't go away.

I haven't tried to connect to those processes. When i kill it, nothing notable happens in VS Code (I usually close all folders and separate windows before starting to kill processes). All those processes are also closed, when VS Code is closed.

I will try to turn on logging and post the resultant logs here.

peetrike avatar Oct 19 '21 06:10 peetrike

The captured logs are here. Let me know if You need more.

1634625748-80020a2b-ead1-460a-8497-192267392d681634625746108.zip

peetrike avatar Oct 19 '21 06:10 peetrike

FYI this might be related to https://github.com/microsoft/vscode/issues/118726, right now extension created terminals are persisted across reloads. Closing a window should close it though. Does "terminal.integrated.enablePersistentSessions": false fix the problem?

Tyriar avatar Oct 19 '21 13:10 Tyriar

No change with that setting. Maybe because I have in my config: "powershell.integratedConsole.showOnStartup": false

I try to avoid using Integrated console as a terminal window. I prefer separate pwsh processes for that. And those are terminated as expected.

peetrike avatar Oct 19 '21 14:10 peetrike

I removed config line "powershell.integratedConsole.showOnStartup": false and the problem ceased to exist. And now every time when I close project folder or script file (that triggered loading PowerShell extension), I get a message (for a short time) that PowerShell Integrated Console (PSIC) terminated, do I want to restart it.

I also tested, what happens when I close PSIC panel myself, and then the PowerShell extension process is terminated as expected.

When PSIC is hidden on startup and I make it visible, then PowerShell extension process is terminated as expected. Even when I close the PSIC panel after making it visible.

peetrike avatar Oct 19 '21 15:10 peetrike

I wonder if this is related to https://github.com/PowerShell/vscode-powershell/issues/3360, so when we fix that bug, let's test this again.

andyleejordan avatar Nov 09 '21 21:11 andyleejordan

@Tyriar Hey, just FYI, I see this same bug exist with the C# extension where the spawned dotnet processes aren't closed. I'm frequently having to go to my activity manager and force close a dozen of them, even though Code is no longer open. So I don't think this problem is unique to the PowerShell extension. Code is losing track of its child processes.

andyleejordan avatar Dec 29 '21 01:12 andyleejordan

If this still happens after #3360 you could try the Terminal: Attach to Session command, if the process is classified as an "orphan" without a window you should be able to reattach to it.

Tyriar avatar Mar 15 '22 15:03 Tyriar

@peetrike with the PR Tyriar mentioned and the latest version of VS Code, there's a good chance this has been fixed. Can you try the the latest PowerShell Preview for VS Code and let us know?

andyleejordan avatar Mar 15 '22 16:03 andyleejordan

Still the same. When I open folder and one PowerShell file, and then close the folder, the PowerShell Integrated Console protsess still remains in Process explorer. And when I open next folder (and .ps1 file within), then new process will be launched and previous process just sits in Process Explorer and takes memory.

peetrike avatar Mar 18 '22 14:03 peetrike

VS Code version 1.65.2, PowerShell Extension Preview version v2022.3.0

peetrike avatar Mar 18 '22 14:03 peetrike

and as soon as I make PSIC visible (and then I can close the PSIC terminal window), when closing folder, the PSIC process is correctly terminated. I have PSIC hidden by default

peetrike avatar Mar 18 '22 14:03 peetrike

Hey, that's a good find and will help us narrow it down.

andyleejordan avatar Mar 21 '22 16:03 andyleejordan

This issue has been marked as external. It has been automatically closed for housekeeping purposes.

ghost avatar Aug 11 '22 20:08 ghost