Various memory leak fixes and improvements

This commit is contained in:
Lord Hepipud 2023-11-15 14:31:40 +01:00
parent 3a32511685
commit c3d3627634
12 changed files with 62 additions and 57 deletions

View file

@ -1,38 +0,0 @@
---
name: Create new issue
about: Create a detailed issue to help us to fix them
labels:
---
<!--- Provide a general summary of the issue in the Title above -->
## Expected Behavior
<!--- If you're describing a bug, tell us what should happen -->
<!--- If you're suggesting a change/improvement, tell us how it should work -->
## Current Behavior
<!--- If describing a bug, tell us what happens instead of the expected behavior -->
<!--- If suggesting a change/improvement, explain the difference from current behavior -->
## Possible Solution
<!--- Not obligatory, but suggest a fix/reason for the bug, -->
<!--- or ideas how to implement: the addition or change -->
## Steps to Reproduce (for bugs)
<!--- Provide a link to a live example, or an unambiguous set of steps to -->
<!--- reproduce this bug. Include configuration, logs, etc. to reproduce, if relevant -->
1.
2.
3.
4.
## Context
<!--- How has this issue affected you? What are you trying to accomplish? -->
<!--- Providing context helps us come up with a solution that is most useful in the real world -->
## Your Environment
<!--- Include as many relevant details about the environment you experienced the problem in -->
* PowerShell Version used (`$PSVersionTable.PSVersion`):
* Operating System and version (`Get-IcingaWindowsInformation Win32_OperatingSystem | Select-Object Version, BuildNumber, Caption`):

1
.gitignore vendored
View file

@ -8,6 +8,7 @@ cache/*
*.log
*.pfx
*.dll
*.DS_Store
# JEA
RoleCapabilities/IcingaForWindows.psrc

View file

@ -18,6 +18,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
### Bugfixes
* [#673](https://github.com/Icinga/icinga-powershell-framework/pull/673) Fixes a memory leak while fetching Windows EventLog information by using CLI tools and inside the Hyper-V provide
* [#678](https://github.com/Icinga/icinga-powershell-framework/pull/678) Fixes various memory leaks in Icinga for Windows API core and check handler
## 1.11.1 (2023-11-07)

View file

@ -33,7 +33,8 @@ function Get-IcingaCacheData()
[string]$Space,
[string]$CacheStore,
[string]$KeyName,
[switch]$TempFile = $FALSE
[switch]$TempFile = $FALSE,
[switch]$AsObject = $FALSE
);
$CacheFile = Join-Path -Path (Join-Path -Path (Join-Path -Path (Get-IcingaCacheDir) -ChildPath $Space) -ChildPath $CacheStore) -ChildPath ([string]::Format('{0}.json', $KeyName));
@ -62,7 +63,7 @@ function Get-IcingaCacheData()
return $null;
}
if ([string]::IsNullOrEmpty($KeyName)) {
if ($AsObject -Or [string]::IsNullOrEmpty($KeyName)) {
return $cacheData;
} else {
return $cacheData.$KeyName;

View file

@ -43,7 +43,7 @@ function Set-IcingaCacheData()
}
if ((Test-Path $CacheFile)) {
$cacheData = Get-IcingaCacheData -Space $Space -CacheStore $CacheStore;
$cacheData = Get-IcingaCacheData -Space $Space -CacheStore $CacheStore -KeyName $KeyName -AsObject;
} else {
try {
New-Item -ItemType File -Path $CacheTmpFile -Force -ErrorAction Stop | Out-Null;

View file

@ -18,4 +18,6 @@ function Write-IcingaDebugMessage()
$DebugContent += $Objects;
Write-IcingaEventMessage -EventId 1000 -Namespace 'Debug' -ExceptionObject $ExceptionObject -Objects $DebugContent;
$DebugContent = $null;
}

View file

@ -11,7 +11,7 @@ function Test-IcingaRESTClientConnection()
-Client $Connection.Client `
-ClientList $Global:Icinga.Public.Daemons.RESTApi.ClientBlacklist;
Write-IcingaEventMessage -EventId 1501 -Namespace 'Framework' -Objects $Connection.Client.Client;
Close-IcingaTCPConnection -Client $Connection.Client;
Close-IcingaTCPConnection -Connection $Connection;
$Connection = $null;
return $FALSE;
}

View file

@ -84,9 +84,15 @@ function New-IcingaForWindowsRESTApi()
-Client (New-IcingaTCPClient -Socket $Socket) `
-Certificate $Certificate;
if ($Connection.Client -eq $null -Or $Connection.Stream -eq $null) {
Close-IcingaTCPConnection -Connection $Connection;
$Connection = $null;
continue;
}
if (Test-IcingaRESTClientBlacklisted -Client $Connection.Client -ClientList $Global:Icinga.Public.Daemons.RESTApi.ClientBlacklist) {
Write-IcingaDebugMessage -Message 'A remote client which is trying to connect was blacklisted' -Objects $Connection.Client.Client;
Close-IcingaTCPConnection -Client $Connection.Client;
Close-IcingaTCPConnection -Connection $Connection;
$Connection = $null;
continue;
}
@ -98,7 +104,7 @@ function New-IcingaForWindowsRESTApi()
# API not yet ready
if ($Global:Icinga.Public.Daemons.RESTApi.ApiRequests.Count -eq 0) {
Close-IcingaTCPConnection -Client $Connection.Client;
Close-IcingaTCPConnection -Connection $Connection;
$Connection = $null;
continue;
}
@ -107,7 +113,7 @@ function New-IcingaForWindowsRESTApi()
$NextRESTApiThreadId = (Get-IcingaNextRESTApiThreadId);
if ($Global:Icinga.Public.Daemons.RESTApi.ApiRequests.ContainsKey($NextRESTApiThreadId) -eq $FALSE) {
Close-IcingaTCPConnection -Client $Connection.Client;
Close-IcingaTCPConnection -Connection $Connection;
$Connection = $null;
continue;
}

View file

@ -38,7 +38,7 @@ function New-IcingaForWindowsRESTThread()
# Send the authentication prompt
Send-IcingaWebAuthMessage -Connection $Connection;
# Close the connection
Close-IcingaTCPConnection -Client $Connection.Client;
Close-IcingaTCPConnection -Connection $Connection;
$Connection = $null;
continue;
}
@ -56,14 +56,14 @@ function New-IcingaForWindowsRESTThread()
# Re-send the authentication prompt
Send-IcingaWebAuthMessage -Connection $Connection;
# Close the connection
Close-IcingaTCPConnection -Client $Connection.Client;
Close-IcingaTCPConnection -Connection $Connection;
$Connection = $null;
continue;
}
}
# Set our thread being active
Set-IcingaForWindowsThreadAlive -ThreadName $Global:Icinga.Protected.ThreadName -Active -TerminateAction @{ 'Command' = 'Close-IcingaTCPConnection'; 'Arguments' = @{ 'Client' = $Connection.Client } };
Set-IcingaForWindowsThreadAlive -ThreadName $Global:Icinga.Protected.ThreadName -Active -TerminateAction @{ 'Command' = 'Close-IcingaTCPConnection'; 'Arguments' = @{ 'Connection' = $Connection } };
# We should remove clients from the blacklist who are sending valid requests
Remove-IcingaRESTClientBlacklist -Client $Connection.Client -ClientList $Global:Icinga.Public.Daemons.RESTApi.ClientBlacklist;
@ -100,7 +100,7 @@ function New-IcingaForWindowsRESTThread()
# Finally close the clients connection as we are done here and
# ensure this thread will close by simply leaving the function
Close-IcingaTCPConnection -Client $Connection.Client;
Close-IcingaTCPConnection -Connection $Connection;
$Connection = $null;
# Force Icinga for Windows Garbage Collection

View file

@ -40,11 +40,17 @@ function New-IcingaCheckResult()
Set-IcingaInternalPluginExitCode -ExitCode $ExitCode;
$this.Check = $null;
return $ExitCode;
}
if ($Compile) {
return $IcingaCheckResult.Compile();
$IcingaExitCode = $IcingaCheckResult.Compile();
$IcingaCheckResult = $null;
$Check = $null;
return $IcingaExitCode;
}
return $IcingaCheckResult;

View file

@ -1,21 +1,40 @@
function Close-IcingaTCPConnection()
{
param(
[System.Net.Sockets.TcpClient]$Client = $null
[hashtable]$Connection = @{ }
);
if ($null -eq $Client) {
if ($null -eq $Connection -Or $Connection.Count -eq 0) {
$Connection = $null;
return;
}
Write-IcingaDebugMessage -Message (
[string]::Format(
'Closing client connection for endpoint {0}',
(Get-IcingaTCPClientRemoteEndpoint -Client $Client)
(Get-IcingaTCPClientRemoteEndpoint -Client $Connection.Client)
)
);
$Client.Close();
$Client.Dispose();
$Client = $null;
try {
if ($null -ne $Connection.Stream) {
$Connection.Stream.Close();
$Connection.Stream.Dispose();
$Connection.Stream = $null;
}
} catch {
$Connection.Stream = $null;
}
try {
if ($null -ne $Connection.Client) {
$Connection.Client.Close();
$Connection.Client.Dispose();
$Connection.Client = $null;
}
} catch {
$Connection.Client = $null;
}
$Connection = $null;
}

View file

@ -9,10 +9,17 @@ function New-IcingaSSLStream()
return $null;
}
[System.Net.Security.SslStream]$SSLStream = $null;
try {
$SSLStream = New-Object System.Net.Security.SslStream($Client.GetStream(), $false)
$SSLStream = New-Object System.Net.Security.SslStream($Client.GetStream(), $false);
$SSLStream.AuthenticateAsServer($Certificate, $false, [System.Security.Authentication.SslProtocols]::Tls12, $true) | Out-Null;
} catch {
if ($null -ne $SSLStream) {
$SSLStream.Close();
$SSLStream.Dispose();
$SSLStream = $null;
}
Write-IcingaEventMessage -EventId 1500 -Namespace 'Framework' -ExceptionObject $_ -Objects $Client.Client;
return $null;
}