Windows psrp connections are not being closed

We have an ansible pipeline that connects to a windows system. After it runs, there are many “Host process for WinRM plug-ins” processes still hanging there and eating memory. We have to end the tasks manually or reboot the system.

We connect to the system via psrp with these settings:

ansible_psrp_auth: basic
ansible_psrp_configuration_name: switchUserSession
ansible_psrp_cert_validation: ignore
ansible_psrp_read_timeout: 60
ansible_psrp_connection_timeout: 60
wsman_idletimeout: 60000

The switchUserSession is a saved PowerShell session we register with register-pssessionconfiguration to run as a different user. Our connecting user doesn’t have administrative privileges.

In this example we see that the ansible process was done in 2 seconds, but the shell stayed on (ShellRunTime and ShellInactivity).

PS C:\WINDOWS\system32> Get-WSManInstance -ResourceURI `
>>   http://schemas.microsoft.com/wbem/wsman/1/windows/shell `
>>   -Enumerate


rsp             : http://schemas.microsoft.com/wbem/wsman/1/windows/shell
lang            : en-US
ShellId         : 7208E7C0-B308-4FEE-BE0C-8D84223BCAC3
ResourceUri     : http://schemas.microsoft.com/powershell/switchUserSession
Owner           : ansibleUser
ClientIP        : 192.168.1.1
ProcessId       : 7624
IdleTimeOut     : PT300.000S
InputStreams    : stdin pr
OutputStreams   : stdout
MaxIdleTimeOut  : PT2147483.647S
Locale          : en-US
DataLocale      : en-US
CompressionMode : NoCompression
ProfileLoaded   : Yes
Encoding        : UTF8
BufferMode      : Block
State           : Disconnected
ShellRunTime    : P0DT0H5M57S
ShellInactivity : P0DT0H5M55S
MemoryUsed      : 128MB
ChildProcesses  : 0

We already set out IdleTimeout for WinRM to 5 minutes, this helped. We would still like to figure out why those sessions are not being cleared on disconnect. This sometimes leads to failures in our tasks, because there isn’t enough RAM left on the system to start processes.

Are you potentially using loops and/or with a templated ansible_host variable or with delegation? There is an issue Templated ansible_host causes a new connection (and a new runspace/shell) to be created on every loop iteration · Issue #87019 · ansible/ansible · GitHub with a potential fix Fix loop connection reuse logic by jborean93 · Pull Request #87650 · ansible/ansible · GitHub where the templated ansible_host causes a looped connection to build a new connection on every task for that host and it doesn’t explicitly clean up the shells.

If you don’t I would try and do some very basic playbooks to try and determine a small and reproducible way to see when this happens and open a bug report.