Skip to content

Get-DbaDbExtentDiff - Leave the connection of the caller alone - #10567

Merged
potatoqualitee merged 1 commit into
developmentfrom
fix-extentdiff-connection-ownership
Aug 13, 2026
Merged

Get-DbaDbExtentDiff - Leave the connection of the caller alone#10567
potatoqualitee merged 1 commit into
developmentfrom
fix-extentdiff-connection-ownership

Conversation

@andreasjordan

Copy link
Copy Markdown
Collaborator

Second of the sites listed in #10554, after #10564 brought the mechanism.

The disconnect

The command opened its connection with -NonPooledConnection and closed it at the end, without asking whether it had opened it. When a caller passes their own server object, Connect-DbaInstance hands the same object back and the command then closed the connection of the caller, which takes the session with it. It now only closes what it opened itself, via -IsNewConnectionReference.

The leak the disconnect was hiding

This is the part worth a second look. The extents query ran in the examined database:

$DBCCPageResults = $server.Query($DBCCPageQueryDMV, $db.Name)

That two argument form routes through $server.Databases[$Database].ExecuteWithResults(), and SMO leaves the connection in that database - the defect in #10555. Until now the disconnect covered it up: the connection was closed right afterwards, and the reconnect landed back on the database from the connection string. Removing the disconnect without touching the query would have left the connection of the caller in whichever database was examined last.

So the query now names the database instead of running in it:

FROM [$dbNameEscaped].sys.dm_db_file_space_usage

Verified that this returns the same numbers as the context switch, per database:

master   three-part=71     with context switch=71
msdb     three-part=297    with context switch=297
tempdb   three-part=512    with context switch=512

The branch for SQL Server 2016 before SP2 needs no change: it queries master.sys.master_files and runs DBCC PAGE with the database as an argument, neither of which moves the connection.

Tests

Two assertions on a connection the caller opened: the session survives the call, and the connection is still on the database it was on. Each one covers one of the two changes - reverting the disconnect guard fails the first, reverting the query change fails the second with Expected: 'master' But was: 'dbatoolsci_test_1751040644'.

Get-DbaDbExtentDiff passes 9 of 9 against SQL Server 2019.


This text was created by Claude and reviewed by Andreas Jordan.

The command closed the connection when it was done, no matter whether it had
opened it or whether it was handed one. Connect-DbaInstance now reports that
through -IsNewConnectionReference, so only a connection the command opened
itself is closed.

The disconnect was also hiding a second problem. The query for the extents
ran in the database that was examined, through the two argument form of
$server.Query, and SMO leaves the connection there. Closing the connection
made that invisible, because the reconnect went back to the database of the
connection string. The query now names the database instead of running in
it, which gives the same numbers without touching the connection.

See #10554 and #10555

(do Get-DbaDbExtentDiff)

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@potatoqualitee

Copy link
Copy Markdown
Member

I digt it, thanks

@potatoqualitee
potatoqualitee merged commit e2965d7 into development Aug 13, 2026
21 checks passed
@potatoqualitee
potatoqualitee deleted the fix-extentdiff-connection-ownership branch August 13, 2026 18:28
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