Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit bcc2940

Browse files
benaadamsstephentoub
authored andcommitted
Don't capture AsyncLocals into EventCounter timer (#26075)
* Don't capture AsyncLocals into EventCounter timer * Harden Flow suppression * Feedback
1 parent e2bfa85 commit bcc2940

File tree

1 file changed

+18
-1
lines changed
  • src/System.Diagnostics.Tracing/src/System/Diagnostics/Tracing

1 file changed

+18
-1
lines changed

src/System.Diagnostics.Tracing/src/System/Diagnostics/Tracing/EventCounter.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,24 @@ private void EnableTimer(float pollingIntervalInSeconds)
407407
_pollingIntervalInMilliseconds = (int)(pollingIntervalInSeconds * 1000);
408408
DisposeTimer();
409409
_timeStampSinceCollectionStarted = DateTime.UtcNow;
410-
_pollingTimer = new Timer(OnTimer, null, _pollingIntervalInMilliseconds, _pollingIntervalInMilliseconds);
410+
// Don't capture the current ExecutionContext and its AsyncLocals onto the timer causing them to live forever
411+
bool restoreFlow = false;
412+
try
413+
{
414+
if (!ExecutionContext.IsFlowSuppressed())
415+
{
416+
ExecutionContext.SuppressFlow();
417+
restoreFlow = true;
418+
}
419+
420+
_pollingTimer = new Timer(s => ((EventCounterGroup)s).OnTimer(null), this, _pollingIntervalInMilliseconds, _pollingIntervalInMilliseconds);
421+
}
422+
finally
423+
{
424+
// Restore the current ExecutionContext
425+
if (restoreFlow)
426+
ExecutionContext.RestoreFlow();
427+
}
411428
}
412429
// Always fire the timer event (so you see everything up to this time).
413430
OnTimer(null);

0 commit comments

Comments
 (0)