Skip to content

Commit 4ffacfa

Browse files
Anchor sp_HumanEvents cleanup LIKE to its own session names
The @cleanup = 1 path built a DROP EVENT SESSION list for every row matching N'%HumanEvents_%'. Two problems: 1. Leading % — unanchored, so N'MyHumanEvents_AppSession' (a user's own XE session whose name happens to include "HumanEvents") matched and got dropped. 2. Unescaped _ — LIKE treats _ as a single-character wildcard, so N'HumanEventsMonitor' (no literal underscore at all) matched via the trailing % plus the _ wildcard absorbing any one character. Changed to two anchored patterns with the underscore escaped via a bracket class, matching exactly what sp_HumanEvents creates: LIKE N'HumanEvents[_]%' OR LIKE N'keeper[_]HumanEvents[_]%' Verified against a synthetic session-name set: OLD matched 4 of 5 names (3 legitimate + HumanEventsMonitor + MyHumanEvents_AppSession — 2 collateral-damage matches) NEW matched exactly the 2 sp_HumanEvents sessions (HumanEvents_waits_abc123, keeper_HumanEvents_blocking) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent fe8189f commit 4ffacfa

1 file changed

Lines changed: 20 additions & 3 deletions

File tree

sp_HumanEvents/sp_HumanEvents.sql

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4831,7 +4831,22 @@ BEGIN
48314831

48324832
SET @executer = QUOTENAME(@output_database_name) + N'.sys.sp_executesql ';
48334833

4834-
/*Clean up sessions*/
4834+
/*
4835+
Clean up sessions. Match only what sp_HumanEvents itself creates:
4836+
HumanEvents_<event_type>_<guid> (one-shot, @keep_alive = 0)
4837+
keeper_HumanEvents_<event_type> (@keep_alive = 1)
4838+
4839+
Previous pattern N'%HumanEvents_%' had two issues:
4840+
- unanchored leading % — a user session named "MyHumanEventsFoo"
4841+
would match and get dropped.
4842+
- unescaped _ — LIKE treats _ as a single-char wildcard, so
4843+
"HumanEventsMonitor" (no literal underscore) would match via the
4844+
trailing % + the _ wildcard eating any one character.
4845+
4846+
Anchored to the prefix and escaped the literal underscore with a
4847+
bracket class so an operator using HumanEvents-adjacent names for
4848+
their own XE sessions isn't collateral damage.
4849+
*/
48354850
IF @azure = 0
48364851
BEGIN
48374852
SELECT
@@ -4843,7 +4858,8 @@ BEGIN
48434858
FROM sys.server_event_sessions AS ses
48444859
LEFT JOIN sys.dm_xe_sessions AS dxs
48454860
ON dxs.name = ses.name
4846-
WHERE ses.name LIKE N'%HumanEvents_%';
4861+
WHERE ses.name LIKE N'HumanEvents[_]%'
4862+
OR ses.name LIKE N'keeper[_]HumanEvents[_]%';
48474863
END;
48484864
ELSE
48494865
BEGIN
@@ -4856,7 +4872,8 @@ BEGIN
48564872
FROM sys.database_event_sessions AS ses
48574873
LEFT JOIN sys.dm_xe_database_sessions AS dxs
48584874
ON dxs.name = ses.name
4859-
WHERE ses.name LIKE N'%HumanEvents_%';
4875+
WHERE ses.name LIKE N'HumanEvents[_]%'
4876+
OR ses.name LIKE N'keeper[_]HumanEvents[_]%';
48604877
END;
48614878

48624879
EXECUTE sys.sp_executesql

0 commit comments

Comments
 (0)