Merge dev to main: add ProtectSession watchdog#796
Merged
Merged
Conversation
A non-canonical helper procedure that watches one designated session and decides what to do with whoever is blocking it. Classification reads sys.dm_tran_locks (not blocker.command), so SELECT-INTO-permanent and sessions that ran an UPDATE before their current statement are correctly identified as modifications. By default only reads and tempdb-only modifications are killed; permanent-data modifications are spared unless @kill_modification_blockers = 1 (kill them anyway) or @abort_on_modification_block = 1 (kill the protected session instead). Walks the blocking chain upstream for chain_depth + lead_blocker_session_id diagnostics; KILL target stays the immediate blocker. Resolves the contended resource (database + type + object name when OBJECT) for the debug output and kill/abort message. Defends against spid-reuse misfires by fingerprinting (session_id, login_time) and re-verifying before any KILL. Not added to Install-All; non-canonical helper. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.
Adds
ProtectSession, a non-canonical helper procedure that watches one designated session and decides what to do with whoever is blocking it.What is in this PR
ProtectSession/ProtectSession.sql-- the procedure (~770 lines)ProtectSession/README.md-- usage docs, parameter table, classification detailsNot added to Install-All; intentional, this is a non-canonical helper.
Highlights
Classification is held-lock-based, not command-based.
sys.dm_tran_locksis the source of truth for "is this blocker durably modifying permanent data" -- checkingcommandwould misclassifySELECT INTO dbo.RealTable(looks like a SELECT), and a session whose current statement is a SELECT but whose transaction still holds X locks from an earlier UPDATE. The proc looks for modification or schema-change locks (X / IX / U / SIX / BU / Sch-M) on OBJECT resources outside tempdb.Default behavior: kill SELECT blockers and tempdb-only modifications past
@block_threshold_seconds; leave permanent-data modifications alone. Override with@kill_modification_blockers = 1(kill them anyway, rolls back their work) or@abort_on_modification_block = 1(kill the protected session instead).Chain-walk diagnostics. Each cycle walks the blocking chain upstream to capture
chain_depth+lead_blocker_session_id-- surfaced in@debugoutput and in every kill/abort message. The KILL target stays the immediate blocker, since that is the only kill that directly frees the protected session.Contended-resource naming. From
sys.dm_tran_locksfor the protected session's WAIT row: database name, resource type, and object name (for OBJECT-type contention).Spid-reuse defense. SQL Server recycles freed
session_ids aggressively (confirmed empirically: 6 fresh connections all got the same recycled spid in sequence). The proc fingerprints sessions by(session_id, login_time)and re-verifies before any KILL, skipping with a log message on mismatch rather than misfiring on a recycled spid.🤖 Generated with Claude Code