Skip to content

Commit 0a453c3

Browse files
Coerce sp_HumanEvents @seconds_sample = 0/NULL to 1 second
@seconds_sample drives the WAITFOR DELAY that paces the XE session. The duration-math block only populates @waitfor when @seconds_sample > 0, so passing 0 or NULL left @waitfor at its default of N'' and the unconditional WAITFOR DELAY @waitfor later crashed with a syntax error. Tried a RAISERROR/THROW validation first, but the proc body is wrapped in a TRY/CATCH whose CATCH block tries to ALTER EVENT SESSION ... STOP on a session that was never created when the failure happens before session creation. That overrides the original message with a "session does not exist" error and makes the true problem unguessable. Coerce to 1 second instead: NULL silently, 0 with a warning pointing the caller at a more useful value. The proc then completes its normal create / start / 1-second wait / stop / drop flow and returns empty result sets. Callers still get a running proc and a discoverable warning instead of a cryptic crash. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent ca2dbd8 commit 0a453c3

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

sp_HumanEvents/sp_HumanEvents.sql

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,25 @@ BEGIN
890890
RETURN;
891891
END;
892892

893+
/*
894+
@seconds_sample drives the WAITFOR DELAY that controls how long the XE session
895+
samples before we shred results. When NULL it's treated as unset. When
896+
explicitly 0 the user is asking for no sampling, which would hit the
897+
unconditional WAITFOR below with an empty @waitfor string and raise a
898+
syntax error. Coerce NULL and 0 to 1 second — the minimum meaningful
899+
value — and warn if 0 was explicit.
900+
*/
901+
IF @debug = 1 BEGIN RAISERROR(N'Checking seconds_sample parameter', 0, 1) WITH NOWAIT; END;
902+
IF @seconds_sample IS NULL
903+
BEGIN
904+
SET @seconds_sample = 1;
905+
END;
906+
ELSE IF @seconds_sample = 0
907+
BEGIN
908+
RAISERROR(N'@seconds_sample = 0 is not meaningful (nothing would be sampled). Using 1 second instead. Pass a larger value for real sampling.', 0, 1) WITH NOWAIT;
909+
SET @seconds_sample = 1;
910+
END;
911+
893912

894913
IF @debug = 1 BEGIN RAISERROR(N'Checking query sort order', 0, 1) WITH NOWAIT; END;
895914
IF @query_sort_order NOT IN

0 commit comments

Comments
 (0)