Skip to content

Commit

Permalink
Merge pull request #678 from Icinga:fix/core_memory_leaks
Browse files Browse the repository at this point in the history
Fix: Various memory leak fixes and improvements
  • Loading branch information
LordHepipud authored Feb 2, 2024
2 parents 3a32511 + c3d3627 commit 5305f7e
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 57 deletions.
38 changes: 0 additions & 38 deletions .github/ISSUE_TEMPLATE/create-new-issue.md

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ cache/*
*.log
*.pfx
*.dll
*.DS_Store

# JEA
RoleCapabilities/IcingaForWindows.psrc
Expand Down
1 change: 1 addition & 0 deletions doc/100-General/10-Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
5 changes: 3 additions & 2 deletions lib/core/cache/Get-IcingaCacheData.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lib/core/cache/Set-IcingaCacheData.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions lib/core/logging/Write-IcingaDebugMessage.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ function Write-IcingaDebugMessage()
$DebugContent += $Objects;

Write-IcingaEventMessage -EventId 1000 -Namespace 'Debug' -ExceptionObject $ExceptionObject -Objects $DebugContent;

$DebugContent = $null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
12 changes: 9 additions & 3 deletions lib/daemons/RestAPI/daemon/New-IcingaForWindowsRESTApi.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion lib/icinga/plugin/New-IcingaCheckResult.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
31 changes: 25 additions & 6 deletions lib/webserver/Close-IcingaTCPConnection.psm1
Original file line number Diff line number Diff line change
@@ -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;
}
9 changes: 8 additions & 1 deletion lib/webserver/New-IcingaSSLStream.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 5305f7e

Please sign in to comment.