Skip to content

Make SQL service restart in cert generation resilient to start-pending state - #274

Merged
David Engel (David-Engel) merged 2 commits into
mainfrom
david/ci-cert-restart-flake
Aug 13, 2026
Merged

Make SQL service restart in cert generation resilient to start-pending state#274
David Engel (David-Engel) merged 2 commits into
mainfrom
david/ci-cert-restart-flake

Conversation

@David-Engel

@David-Engel David Engel (David-Engel) commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes an intermittent CI failure in the Windows SQL setup step where Generate-SqlCertificates.ps1 fails with CouldNotStopService ("service cannot be stopped"). On freshly-provisioned images SQL Server can still be in a start-pending state (startup database recovery) when the script tries to restart it to pick up the new TLS certificate thumbprint, and a plain Restart-Service fails outright.

Adds Restart-SqlServiceSafely, which:

  • waits (up to ~150s) for the service to settle into Running or Stopped before touching it,
  • then stops/starts with explicit WaitForStatus and up to 5 retries,
  • throws a clear error if all attempts fail.

Also stops hardcoding MSSQLSERVER as the service name: the service is now derived from the $instanceName already passed into New-And-Install-Certificates, mapping named instances to MSSQL$<instance> the same way Enable-SqlProtocols.ps1 and Configure-ExtendedProtection.ps1 do. Behavior is unchanged for the default instance.

This change was split out of #142 so the flake fix can merge independently of the NVMe pool retarget.

Linked work item

None.

Testing

No local test run — this only executes on Windows CI agents with a real SQL Server service. Verification is a green Windows validation run; the failure mode it addresses is intermittent, so the signal is the absence of CouldNotStopService in the "Generate SQL certificates" step across runs.

Breaking changes / migration notes

None. Script parameters and outputs are unchanged.

…g state

Freshly-provisioned CI images can leave SQL Server in a start-pending state, so a plain Restart-Service intermittently fails with CouldNotStopService. Wait for a steady state, then stop/start with retries, and target the passed-in instance name instead of hardcoding MSSQLSERVER.
@David-Engel
David Engel (David-Engel) marked this pull request as ready for review August 13, 2026 17:06
@David-Engel
David Engel (David-Engel) requested a review from a team as a code owner August 13, 2026 17:06
Copilot AI balanced review requested due to automatic review settings August 13, 2026 17:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Improves SQL Server certificate setup reliability during transient service states.

Changes:

  • Adds retry-based SQL service restart logic.
  • Uses the configured instance when resolving the service.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread .pipeline/scripts/Generate-SqlCertificates.ps1 Outdated
@saurabh500

Copy link
Copy Markdown
Contributor

Except the bot comment this looks good to go.

@github-actions

Copy link
Copy Markdown

📊 Code Coverage Report

🔥 Diff Coverage

100%

🎯 Overall Coverage

91.5%

📦 Project: mssql-tds + mssql-odbc + mssql-py-core
ℹ️ Note: diff coverage is reported, not enforced.


Diff Coverage

Diff: main...HEAD, staged and unstaged changes

No lines with coverage information in this diff.


🔗 Quick Links

View Azure DevOps Build · Coverage Report

@David-Engel
David Engel (David-Engel) enabled auto-merge (squash) August 13, 2026 20:33
@David-Engel
David Engel (David-Engel) merged commit e24bce5 into main Aug 13, 2026
20 checks passed
@David-Engel
David Engel (David-Engel) deleted the david/ci-cert-restart-flake branch August 13, 2026 23:21
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.

3 participants