Skip to content

Commit 24ca7f2

Browse files
Merge fix/blockviewer-recursive-cte-cycle-guard into dev
2 parents d7d74b0 + c01543b commit 24ca7f2

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

sp_HumanEvents/sp_HumanEventsBlockViewer.sql

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2193,6 +2193,15 @@ WITH
21932193
JOIN #blocking AS bg
21942194
ON bg.monitor_loop = h.monitor_loop
21952195
AND bg.blocking_desc = h.blocked_desc
2196+
/*
2197+
Cycle guard: skip a row whose blocked_desc already appears in the
2198+
accumulated sort_order. Two sessions can briefly appear to block each
2199+
other in the same monitor_loop (before the deadlock monitor fires),
2200+
and without a guard the recursion has no exit. The sort_order string
2201+
contains every (SPID:ECID) we've visited on this branch; checking for
2202+
the candidate blocked_desc before we follow it prevents the cycle.
2203+
*/
2204+
WHERE h.sort_order NOT LIKE '%' + bg.blocked_desc + '%'
21962205
)
21972206
UPDATE
21982207
#blocked
@@ -2204,7 +2213,13 @@ JOIN hierarchy AS h
22042213
ON h.monitor_loop = b.monitor_loop
22052214
AND h.blocking_desc = b.blocking_desc
22062215
AND h.blocked_desc = b.blocked_desc
2207-
OPTION(RECOMPILE, MAXRECURSION 0);
2216+
/*
2217+
MAXRECURSION 100 (the default) is plenty for real blocking chains and
2218+
still acts as a backstop if the cycle guard above is ever bypassed by
2219+
a blocked_desc that doesn't format the same way as expected. Reverted
2220+
from MAXRECURSION 0 which gave the runaway case no ceiling at all.
2221+
*/
2222+
OPTION(RECOMPILE, MAXRECURSION 100);
22082223

22092224
IF @debug = 1
22102225
BEGIN

0 commit comments

Comments
 (0)