Skip to content

Enable-DbaAgHadr, Disable-DbaAgHadr - Report a refused service restart instead of ignoring it - #10709

Open
andreasjordan wants to merge 1 commit into
developmentfrom
fix-enable-dbaaghadr-honest-restart
Open

Enable-DbaAgHadr, Disable-DbaAgHadr - Report a refused service restart instead of ignoring it#10709
andreasjordan wants to merge 1 commit into
developmentfrom
fix-enable-dbaaghadr-honest-restart

Conversation

@andreasjordan

Copy link
Copy Markdown
Collaborator

Found through the COPY lane failure on #10705, which touches nothing near restores: Remove-DbaDatabaseSafely failed three times with "SQL Server Agent is not running" on sql2022, while the green development run of the same day ran the identical file list.

Mechanism

The prep phase of the job explains it. tests/appveyor.sql2022.ps1 runs Enable-DbaAgHadr -Force, which does Stop-DbaService -Type Agent, Engine and then Start-DbaService. This time the Agent went to StopPending and came back (Update-ServiceStatus: "The service failed to stop"), so the engine stop was refused ("other services that are running are dependent on it"), the Agent finished stopping on its own afterwards, and nothing started it again. Update-ServiceStatus reports all of that only as Status = "Failed" on the returned objects plus warnings; -EnableException does not turn it into an exception. Enable-DbaAgHadr discarded the objects, so it returned normally, the setup stage passed, and the failure surfaced six minutes later in an unrelated test. tests/appveyor.sql2019.ps1 has the same line, so the HADR lane is exposed the same way.

Changes

Enable-DbaAgHadr and Disable-DbaAgHadr (same block in both): the stop and start results are kept and any object whose Status is not Successful turns the restart into a failure, reported through the existing Stop-Function "Issue restarting ..." with the service messages as the error record. Without -EnableException that is a warning, with it an exception. Nothing changes when the restart works.

tests/appveyor.sql2019.ps1 and tests/appveyor.sql2022.ps1: the HADR step is wrapped so that a failed restart is handled instead of ignored: wait for a StopPending service to settle, start whatever is not running, and run Enable-DbaAgHadr -Force once more. The two Start-Service lines already in the scripts run before the HADR step and could not help.

Tests

The lab cannot provoke a refused service stop on demand, so the restart-failure path has no red-on-old test; the happy path is covered by the existing tests, which restart the HADR instance: Enable-DbaAgHadr 2/2 and Disable-DbaAgHadr 2/2 on both PowerShell 7 and Windows PowerShell 5.1. The setup scripts only run on the self-hosted runners; this PR's own ci-azure run is their test.

Created by Claude and reviewed by Andreas Jordan.

🤖 Generated with Claude Code

…t instead of ignoring it

With -Force both commands stop and start Agent and Engine through
Stop-DbaService and Start-DbaService and discard the results. A refused stop
or start only shows as Status "Failed" on those objects, so when the Agent was
slow to stop on a CI runner the engine restart was refused, the Agent ended up
stopped, and the command still reported success. Six minutes later an
unrelated test failed with "SQL Server Agent is not running".

The results are now checked and any failure goes through the existing
"Issue restarting" Stop-Function with the service messages as error record: a
warning without -EnableException, an exception with it.

The two CI instance setup scripts that run Enable-DbaAgHadr -Force now handle
that failure: wait for a StopPending service to settle, start what is not
running, and try once more.

(do Enable-DbaAgHadr, Disable-DbaAgHadr)

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

@potatoqualitee potatoqualitee left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The refusal path now surfaces the failed service restart instead of continuing as though HADR had been enabled or disabled. The surrounding multi-input flow and cleanup behavior remain intact, and the relevant HADR/restart CI lanes pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants