Skip to content

New-DbaAvailabilityGroup - Complete the progress bar in a finally block - #10764

Merged
potatoqualitee merged 1 commit into
developmentfrom
new-dbaavailabilitygroup-progress-finally
Sep 30, 2026
Merged

potatoqualitee merged 1 commit into
developmentfrom
new-dbaavailabilitygroup-progress-finally

Conversation

@andreasjordan

Copy link
Copy Markdown
Collaborator

Part of RFC #10548: the try/finally leak fix for New-DbaAvailabilityGroup. Independent of #10761, #10762 and #10763.

Problem

New-DbaAvailabilityGroup completed its bar by hand: one Write-Progress -Completed in front of each of the 20 Stop-Function calls or early returns, and one at the end. That covers the throws of Stop-Function under -EnableException, but not a stopped pipeline and not an exception outside Stop-Function. A stopped pipeline is what Ctrl+C does, and the command is likely to be stopped while it waits for the replicas to connect. In both cases the bar stayed on screen for the rest of the script.

Change

From the first progress step on, the process block runs in try/finally, and the finally completes the bar. The 20 hand-written completions go away.

Most of the diff is re-indentation: git diff -w shows 4 lines added and 20 removed.

Test

A new integration context runs the real command with -WhatIf, so the instance stays unchanged. It runs in a runspace of its own, created by the PowerShell API without a host, where Write-Progress puts every record into Streams.Progress. The test stops the pipeline as soon as the first record arrives, the same as Ctrl+C. It asserts that the run really was stopped (so a run that finished first cannot pass by accident) and that every progress Id got a completed record.

  • Timing: on the lab, the first record arrives after about 80 ms and the -WhatIf run would end after about 0.9 to 1.5 s, so the 10 ms poll leaves a wide margin.
  • Lab (InstanceHadr, SQL Server 2025): 6/6 on PowerShell 5.1 and 7.6.
  • On the old command: exactly the new "Completes its progress bar" test fails on both editions, with bar 0 left open.
  • TestTestfiles.Tests.ps1 and TestTestLayout.ps1 report nothing for the file.

🤖 Generated with Claude Code

A stopped pipeline (Ctrl+C, for example while the command waits for the
replicas) or an exception outside Stop-Function left the bar on screen. The
process block now completes it in finally, which replaces the completions
that were written before every Stop-Function.

(do New-DbaAvailabilityGroup)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@andreasjordan
andreasjordan marked this pull request as ready for review September 27, 2026 18:22

@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.

Reviewed the complete exact-head patch, surrounding progress/control flow, real-boundary cancellation test, CI, and existing discussion. No material correctness, cleanup, compatibility, or multi-input defect found.

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