From eefe35b148262a3470063a9f0d721155d0e11302 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 12 Mar 2026 20:15:43 +0000 Subject: [PATCH 1/2] Initial plan From ac0eca44a2cb8aba110531a00e5a44901e1198d0 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 12 Mar 2026 20:18:37 +0000 Subject: [PATCH 2/2] Fix Win32Exception in Clean-ElasticAgentService when service is in transient state Co-authored-by: v1v <2871786+v1v@users.noreply.github.com> --- src/agent-qa/helpers.ps1 | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/agent-qa/helpers.ps1 b/src/agent-qa/helpers.ps1 index 0ed3abe..3d3f3b3 100644 --- a/src/agent-qa/helpers.ps1 +++ b/src/agent-qa/helpers.ps1 @@ -334,8 +334,26 @@ Function Clean-ElasticAgentInstaller { Function Clean-ElasticAgentService { - if (Get-Service 'Elastic Agent' -ErrorAction SilentlyContinue) { - Stop-Service 'Elastic Agent' + $service = Get-Service 'Elastic Agent' -ErrorAction SilentlyContinue + if ($service) { + # Wait up to 30 seconds for any transient state (e.g. StartPending, StopPending) to resolve + # before attempting to stop the service, otherwise Stop-Service throws a Win32Exception. + $timeout = (Get-Date).AddSeconds(30) + $service.Refresh() + while ($service.Status -notin @('Running', 'Stopped') -and (Get-Date) -lt $timeout) { + Start-Sleep -Seconds 1 + $service.Refresh() + } + if ($service.Status -notin @('Running', 'Stopped')) { + Write-Warning "Timed out waiting for 'Elastic Agent' service to reach a stable state. Current status: $($service.Status)" + } + if ($service.Status -ne 'Stopped') { + try { + Stop-Service 'Elastic Agent' -Force + } catch { + Write-Warning "Failed to stop 'Elastic Agent' service: $_" + } + } Remove-Service 'Elastic Agent' } }