Visitar URL original
Memory+Thread leak when using WinPSCompatSession and RunspacePool · Issue #17473 · PowerShell/PowerShell · GitHub
The Wayback Machine - https://web.archive.org/web/20220607124853/https://github.com/PowerShell/PowerShell/issues/17473
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory+Thread leak when using WinPSCompatSession and RunspacePool #17473

Open
5 tasks done
joolive opened this issue Jun 3, 2022 · 3 comments
Open
5 tasks done

Memory+Thread leak when using WinPSCompatSession and RunspacePool #17473

joolive opened this issue Jun 3, 2022 · 3 comments
Labels
Needs-Triage WG-Engine-Module WG-Engine-Performance

Comments

@joolive
Copy link

@joolive joolive commented Jun 3, 2022 •

Prerequisites

Steps to reproduce

When running WindowsPowerShell-only cmdlets via WinPSCompat implicit and leveraging a RunspacePool (runspaces running in parallel), memory leaks + thread leaks will occur.

Consider the following snippet:

$ScriptBlocks = @(
    "Get-WMIObject -Class Win32_Bios",
    "Get-WMIObject -Class Win32_ComputerSystemProduct",
    "Get-WMIObject -Class Win32_ComputerSystem",
    "Get-WMIObject -Class Win32_ComputerSystemProcessor",
    "Get-WMIObject -Class Win32_CurrentTime"
)
1..50 | ForEach-Object {
    $result = $ScriptBlocks | ForEach-Object -Parallel { & ([scriptblock]::Create($_)); } -ThrottleLimit 3 
    Write-Host $result.Count
    Start-Sleep -Seconds 10
}

Items to consider:

  • A ForEach-Object -Parallel internally uses a runspace pool to orchestrate requests across multiple runspaces in parallel; This issue is also reproducible when a RunspacePool is used directly with PowerShell objects to run script blocks.
  • Get-WMIObject is a WindowsPowerShell-only cmdlet, so its invocation will use the Windows PowerShell compatibility (WinPSCompat session) implicitly.

Expected behavior

No memory leaks or thread leaks occur: PowerShell 7 is able to handle the implicit creation + disposal of the WinPSCompat session and corresponding remoteIpMoProxy_* stub modules automatically.

Actual behavior

On the PowerShell script share above, Memory leaks + thread leaks occur:

  • Threads are leaked (remain locked in OutOfProcessClientSessionTransportManagerBase.ProcessMessageProc())
  • Memory leak of objects of OutOfProcessClientSessionTransportManager occur, since they usually are large objects referenced the call stack of the leaked threads.
  • remoteIpMoProxy_* folders in the temp folder of the current user keep being created but are not deleted.

Error details

No response

Environment data

PS C:\Program Files\PowerShell\7> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      7.1.7
PSEdition                      Core
GitCommitId                    7.1.7
OS                             Microsoft Windows 10.0.22598
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

  • Folders remoteIpMoProxy_* keep being added but not deleted.
    image

  • Theads and overall memory of the Pwsh.exe process keeps increasing as the above script runs. This is a snapshot after running for just a few minutes:
    image

@joolive joolive added the Needs-Triage label Jun 3, 2022
@iSazonov
Copy link
Collaborator

@iSazonov iSazonov commented Jun 3, 2022

As for remoteIpMoProxy_ see #13198.

@iSazonov iSazonov added WG-Engine-Module WG-Engine-Performance labels Jun 3, 2022
@kasini3000
Copy link

@kasini3000 kasini3000 commented Jun 4, 2022 •

My environment is win+ ps7.3pr4, 7.2.4,
I can't reproduce the problem, code:

$ScriptBlocks = @(
    "Get-CimInstance -Class Win32_Bios",
    "Get-CimInstance -Class Win32_ComputerSystemProduct",
    "Get-CimInstance -Class Win32_ComputerSystem",
    "Get-CimInstance -Class Win32_ComputerSystemProcessor",
    "Get-CimInstance -Class Win32_CurrentTime"
)

1..50 | ForEach-Object {
    $result = $ScriptBlocks | ForEach-Object  -UseNewRunspace -Parallel { & ([scriptblock]::Create($_)); } -ThrottleLimit 3 
    Write-Host $result.Count
    Start-Sleep -Seconds 10
}

i tested without -UseNewRunspace too,Thread less than 35.

@PaulHigin
Copy link
Collaborator

@PaulHigin PaulHigin commented Jun 6, 2022

I am unable to reproduce the thread leak, with or without the -UseNewRunspace switch. But if the local remote sessions (created by WinPSCompatSession) are not closed, then I would expect the kind of client-side message processing leaks noted above. But I don't see the thread leak even when I interrupt the foreach -parallel with Ctrl+C, and I don't see any leaked runspaces or pwsh processes staying around. It would help if the originator provided information about how the leaks were observed.

Note that there is a memory leak issue with implicit remoting (#5244), and I believe WinPSCompatSession also uses implicit remoting. But this leak is due to duplicate scriptblocks (created by implicit remoting) being cached on the client, and the cache size is limited so the memory usage is limited.

/cc: @anmenaga

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs-Triage WG-Engine-Module WG-Engine-Performance
Projects
None yet
Development

No branches or pull requests

4 participants