Skip to content

Commit c3d3627

Browse files
committed
Various memory leak fixes and improvements
1 parent 3a32511 commit c3d3627

File tree

12 files changed

+62
-57
lines changed

12 files changed

+62
-57
lines changed

.github/ISSUE_TEMPLATE/create-new-issue.md

Lines changed: 0 additions & 38 deletions
This file was deleted.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ cache/*
88
*.log
99
*.pfx
1010
*.dll
11+
*.DS_Store
1112

1213
# JEA
1314
RoleCapabilities/IcingaForWindows.psrc

doc/100-General/10-Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
1818
### Bugfixes
1919

2020
* [#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
21+
* [#678](https://github.com/Icinga/icinga-powershell-framework/pull/678) Fixes various memory leaks in Icinga for Windows API core and check handler
2122

2223
## 1.11.1 (2023-11-07)
2324

lib/core/cache/Get-IcingaCacheData.psm1

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ function Get-IcingaCacheData()
3333
[string]$Space,
3434
[string]$CacheStore,
3535
[string]$KeyName,
36-
[switch]$TempFile = $FALSE
36+
[switch]$TempFile = $FALSE,
37+
[switch]$AsObject = $FALSE
3738
);
3839

3940
$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()
6263
return $null;
6364
}
6465

65-
if ([string]::IsNullOrEmpty($KeyName)) {
66+
if ($AsObject -Or [string]::IsNullOrEmpty($KeyName)) {
6667
return $cacheData;
6768
} else {
6869
return $cacheData.$KeyName;

lib/core/cache/Set-IcingaCacheData.psm1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function Set-IcingaCacheData()
4343
}
4444

4545
if ((Test-Path $CacheFile)) {
46-
$cacheData = Get-IcingaCacheData -Space $Space -CacheStore $CacheStore;
46+
$cacheData = Get-IcingaCacheData -Space $Space -CacheStore $CacheStore -KeyName $KeyName -AsObject;
4747
} else {
4848
try {
4949
New-Item -ItemType File -Path $CacheTmpFile -Force -ErrorAction Stop | Out-Null;

lib/core/logging/Write-IcingaDebugMessage.psm1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ function Write-IcingaDebugMessage()
1818
$DebugContent += $Objects;
1919

2020
Write-IcingaEventMessage -EventId 1000 -Namespace 'Debug' -ExceptionObject $ExceptionObject -Objects $DebugContent;
21+
22+
$DebugContent = $null;
2123
}

lib/daemons/RestAPI/client/Test-IcingaRESTClientConnection.psm1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function Test-IcingaRESTClientConnection()
1111
-Client $Connection.Client `
1212
-ClientList $Global:Icinga.Public.Daemons.RESTApi.ClientBlacklist;
1313
Write-IcingaEventMessage -EventId 1501 -Namespace 'Framework' -Objects $Connection.Client.Client;
14-
Close-IcingaTCPConnection -Client $Connection.Client;
14+
Close-IcingaTCPConnection -Connection $Connection;
1515
$Connection = $null;
1616
return $FALSE;
1717
}

lib/daemons/RestAPI/daemon/New-IcingaForWindowsRESTApi.psm1

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,15 @@ function New-IcingaForWindowsRESTApi()
8484
-Client (New-IcingaTCPClient -Socket $Socket) `
8585
-Certificate $Certificate;
8686

87+
if ($Connection.Client -eq $null -Or $Connection.Stream -eq $null) {
88+
Close-IcingaTCPConnection -Connection $Connection;
89+
$Connection = $null;
90+
continue;
91+
}
92+
8793
if (Test-IcingaRESTClientBlacklisted -Client $Connection.Client -ClientList $Global:Icinga.Public.Daemons.RESTApi.ClientBlacklist) {
8894
Write-IcingaDebugMessage -Message 'A remote client which is trying to connect was blacklisted' -Objects $Connection.Client.Client;
89-
Close-IcingaTCPConnection -Client $Connection.Client;
95+
Close-IcingaTCPConnection -Connection $Connection;
9096
$Connection = $null;
9197
continue;
9298
}
@@ -98,7 +104,7 @@ function New-IcingaForWindowsRESTApi()
98104

99105
# API not yet ready
100106
if ($Global:Icinga.Public.Daemons.RESTApi.ApiRequests.Count -eq 0) {
101-
Close-IcingaTCPConnection -Client $Connection.Client;
107+
Close-IcingaTCPConnection -Connection $Connection;
102108
$Connection = $null;
103109
continue;
104110
}
@@ -107,7 +113,7 @@ function New-IcingaForWindowsRESTApi()
107113
$NextRESTApiThreadId = (Get-IcingaNextRESTApiThreadId);
108114

109115
if ($Global:Icinga.Public.Daemons.RESTApi.ApiRequests.ContainsKey($NextRESTApiThreadId) -eq $FALSE) {
110-
Close-IcingaTCPConnection -Client $Connection.Client;
116+
Close-IcingaTCPConnection -Connection $Connection;
111117
$Connection = $null;
112118
continue;
113119
}

lib/daemons/RestAPI/threads/New-IcingaForWindowsRESTThread.psm1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function New-IcingaForWindowsRESTThread()
3838
# Send the authentication prompt
3939
Send-IcingaWebAuthMessage -Connection $Connection;
4040
# Close the connection
41-
Close-IcingaTCPConnection -Client $Connection.Client;
41+
Close-IcingaTCPConnection -Connection $Connection;
4242
$Connection = $null;
4343
continue;
4444
}
@@ -56,14 +56,14 @@ function New-IcingaForWindowsRESTThread()
5656
# Re-send the authentication prompt
5757
Send-IcingaWebAuthMessage -Connection $Connection;
5858
# Close the connection
59-
Close-IcingaTCPConnection -Client $Connection.Client;
59+
Close-IcingaTCPConnection -Connection $Connection;
6060
$Connection = $null;
6161
continue;
6262
}
6363
}
6464

6565
# Set our thread being active
66-
Set-IcingaForWindowsThreadAlive -ThreadName $Global:Icinga.Protected.ThreadName -Active -TerminateAction @{ 'Command' = 'Close-IcingaTCPConnection'; 'Arguments' = @{ 'Client' = $Connection.Client } };
66+
Set-IcingaForWindowsThreadAlive -ThreadName $Global:Icinga.Protected.ThreadName -Active -TerminateAction @{ 'Command' = 'Close-IcingaTCPConnection'; 'Arguments' = @{ 'Connection' = $Connection } };
6767

6868
# We should remove clients from the blacklist who are sending valid requests
6969
Remove-IcingaRESTClientBlacklist -Client $Connection.Client -ClientList $Global:Icinga.Public.Daemons.RESTApi.ClientBlacklist;
@@ -100,7 +100,7 @@ function New-IcingaForWindowsRESTThread()
100100

101101
# Finally close the clients connection as we are done here and
102102
# ensure this thread will close by simply leaving the function
103-
Close-IcingaTCPConnection -Client $Connection.Client;
103+
Close-IcingaTCPConnection -Connection $Connection;
104104
$Connection = $null;
105105

106106
# Force Icinga for Windows Garbage Collection

lib/icinga/plugin/New-IcingaCheckResult.psm1

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,17 @@ function New-IcingaCheckResult()
4040

4141
Set-IcingaInternalPluginExitCode -ExitCode $ExitCode;
4242

43+
$this.Check = $null;
44+
4345
return $ExitCode;
4446
}
4547

4648
if ($Compile) {
47-
return $IcingaCheckResult.Compile();
49+
$IcingaExitCode = $IcingaCheckResult.Compile();
50+
$IcingaCheckResult = $null;
51+
$Check = $null;
52+
53+
return $IcingaExitCode;
4854
}
4955

5056
return $IcingaCheckResult;

0 commit comments

Comments
 (0)