diff --git a/Sense/scripts/t014-capacity.ps1 b/Sense/scripts/t014-capacity.ps1 index 585ba8d..b1131bc 100644 --- a/Sense/scripts/t014-capacity.ps1 +++ b/Sense/scripts/t014-capacity.ps1 @@ -146,6 +146,29 @@ function Stop-AllManagedProcesses($Items) { if ($remaining.Count -gt 0) { Write-Warning "managed processes did not exit within the shared cleanup deadline: $($remaining.Count)" } + foreach ($item in $processes) { + if ($item.Process.HasExited) { + $item.Stdout.Wait(2000) | Out-Null + $item.Stderr.Wait(2000) | Out-Null + $item.Process.Dispose() + } + } +} + +function Remove-SessionDirectory([string]$Path) { + Get-ChildItem -LiteralPath $Path -Recurse -Force -ErrorAction SilentlyContinue | + ForEach-Object { $_.Attributes = 'Normal' } + (Get-Item -LiteralPath $Path -Force).Attributes = 'Directory' + $deleteWatch = [Diagnostics.Stopwatch]::StartNew() + while (Test-Path -LiteralPath $Path) { + try { + Remove-Item -LiteralPath $Path -Recurse -Force -ErrorAction Stop + } + catch { + if ($deleteWatch.Elapsed.TotalSeconds -ge 10) { throw } + Start-Sleep -Milliseconds 200 + } + } } function Assert-Alive($Managed) { @@ -920,10 +943,7 @@ finally { throw 'refusing to clean an unexpected T-014 session path' } if (Test-Path -LiteralPath $resolvedSession) { - Get-ChildItem -LiteralPath $resolvedSession -Recurse -Force -ErrorAction SilentlyContinue | - ForEach-Object { $_.Attributes = 'Normal' } - (Get-Item -LiteralPath $resolvedSession -Force).Attributes = 'Directory' - Remove-Item -LiteralPath $resolvedSession -Recurse -Force + Remove-SessionDirectory $resolvedSession } } else { diff --git a/tests/test_sense_capacity_contract.py b/tests/test_sense_capacity_contract.py index e80112a..dfba927 100644 --- a/tests/test_sense_capacity_contract.py +++ b/tests/test_sense_capacity_contract.py @@ -46,6 +46,8 @@ class SenseCapacityContractTests(unittest.TestCase): self.assertIn("the existing PostgreSQL listener on port 5432 changed", self.script) self.assertNotRegex(self.script, r"Invoke-Checked \$script:pgCtl .* 'start'") self.assertIn("$cleanupWatch.Elapsed.TotalSeconds -lt 10", self.script) + self.assertIn("$item.Process.Dispose()", self.script) + self.assertIn("$deleteWatch.Elapsed.TotalSeconds -ge 10", self.script) def test_runtime_secrets_and_sensitive_inventory_are_not_reported(self): self.assertIn("New-SecretToken", self.script)