From 3042bd7ae65b92f2968c115c11c8c76bdbd6b336 Mon Sep 17 00:00:00 2001 From: John Salem Date: Fri, 13 Aug 2021 14:07:51 -0700 Subject: [PATCH] Handle early wakeup of counter loop (#57170) * Fix #56695 * prevent a negative delta value * PR feedback + bug fix * bug fix is preexisting: if a user other than the one who is listening sets the interval to 0 while the counter is enabled, it makes the counter continuously submit values. Changed this so that an interval value of <= 0 results in the counter being disabled * Add assert after offline conversation --- .../src/System/Diagnostics/Tracing/CounterGroup.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/CounterGroup.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/CounterGroup.cs index d60be1b83bd..aec84922437 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/CounterGroup.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/CounterGroup.cs @@ -127,7 +127,7 @@ private void EnableTimer(float pollingIntervalInSeconds) Debug.Assert(Monitor.IsEntered(s_counterGroupLock)); if (pollingIntervalInSeconds <= 0) { - _pollingIntervalInMilliseconds = 0; + DisableTimer(); } else if (_pollingIntervalInMilliseconds == 0 || pollingIntervalInSeconds * 1000 < _pollingIntervalInMilliseconds) { @@ -187,6 +187,7 @@ private void EnableTimer(float pollingIntervalInSeconds) private void DisableTimer() { + Debug.Assert(Monitor.IsEntered(s_counterGroupLock)); _pollingIntervalInMilliseconds = 0; s_counterGroupEnabledList?.Remove(this); } @@ -252,7 +253,8 @@ private void OnTimer() { _timeStampSinceCollectionStarted = now; TimeSpan delta = now - _nextPollingTimeStamp; - if (delta > TimeSpan.Zero && _pollingIntervalInMilliseconds > 0) + delta = _pollingIntervalInMilliseconds > delta.TotalMilliseconds ? TimeSpan.FromMilliseconds(_pollingIntervalInMilliseconds) : delta; + if (_pollingIntervalInMilliseconds > 0) _nextPollingTimeStamp += TimeSpan.FromMilliseconds(_pollingIntervalInMilliseconds * Math.Ceiling(delta.TotalMilliseconds / _pollingIntervalInMilliseconds)); } } -- GitLab