Skip to content

Commit 7f202ea

Browse files
committed
Various memory leak fixes and improvements
1 parent 36e8198 commit 7f202ea

15 files changed

+96
-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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
1111

1212
[Issues and PRs](https://github.com/Icinga/icinga-powershell-framework/milestone/28)
1313

14+
## 1.11.2 (tbd)
15+
16+
[Issues and PRs](https://github.com/Icinga/icinga-powershell-framework/milestone/30)
17+
18+
### Bugfixes
19+
20+
* [#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+
1422
## 1.11.1 (2023-11-07)
1523

1624
[Issues and PRs](https://github.com/Icinga/icinga-powershell-framework/milestone/29)

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/framework/Read-IcingaWindowsEventLog.psm1

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ function Read-IcingaWindowsEventLog()
5252
$CollectedEvents += $event;
5353
}
5454

55+
if ($null -ne $IcingaEvents) {
56+
$IcingaEvents.Dispose();
57+
$IcingaEvents = $null;
58+
}
59+
5560
$CollectedEvents = $CollectedEvents | Sort-Object { $_.TimeCreated };
5661

5762
foreach ($event in $CollectedEvents) {
@@ -71,6 +76,8 @@ function Read-IcingaWindowsEventLog()
7176
Write-IcingaConsolePlain -Message '[{0}] {1}' -Objects $event.TimeCreated, $event.Message -ForeColor $ForeColor;
7277
}
7378

79+
$CollectedEvents = $null;
80+
7481
Start-Sleep -Seconds 1;
7582
# Force Icinga for Windows Garbage Collection
7683
Optimize-IcingaForWindowsMemory -ClearErrorStack;

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/core/tools/Show-IcingaEventLogAnalysis.psm1

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ function Show-IcingaEventLogAnalysis()
1111
try {
1212
[array]$BasicLogArray = Get-WinEvent -ListLog $LogName -ErrorAction Stop;
1313
$BasicLogData = $BasicLogArray[0];
14+
15+
if ($null -ne $BasicLogArray) {
16+
$BasicLogArray.Dispose();
17+
$BasicLogArray = $null;
18+
}
1419
} catch {
1520
Write-IcingaConsoleError 'Failed to fetch data for EventLog "{0}". Probably this log does not exist.' -Objects $LogName;
1621
return;
@@ -81,6 +86,11 @@ function Show-IcingaEventLogAnalysis()
8186
$LogAnalysis.Minute.Average = [math]::Ceiling($LogAnalysis.Minute.Count / $LogAnalysis.Minute.Entries.Count);
8287
}
8388

89+
if ($null -ne $LogData) {
90+
$LogData.Dispose();
91+
$LogData = $null;
92+
}
93+
8494
foreach ($value in $LogAnalysis.Day.Entries.Values) {
8595
$LogAnalysis.Day.Maximum = Get-IcingaValue -Value $value -Compare $LogAnalysis.Day.Maximum -Maximum;
8696
}

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
}

0 commit comments

Comments
 (0)