Enable-DbaAgHadr, Disable-DbaAgHadr - Report a refused service restart instead of ignoring it - #10709
Open
andreasjordan wants to merge 1 commit into
Open
Enable-DbaAgHadr, Disable-DbaAgHadr - Report a refused service restart instead of ignoring it#10709andreasjordan wants to merge 1 commit into
andreasjordan wants to merge 1 commit into
Conversation
…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>
andreasjordan
requested review from
niphlod and
potatoqualitee
as code owners
September 12, 2026 16:39
potatoqualitee
approved these changes
Sep 13, 2026
potatoqualitee
left a comment
Member
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Found through the COPY lane failure on #10705, which touches nothing near restores:
Remove-DbaDatabaseSafelyfailed 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.ps1runsEnable-DbaAgHadr -Force, which doesStop-DbaService -Type Agent, Engineand thenStart-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 asStatus = "Failed"on the returned objects plus warnings;-EnableExceptiondoes 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.ps1has 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-EnableExceptionthat 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 -Forceonce more. The twoStart-Servicelines 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