Skip to content

Commit

Permalink
Avoid infinite recursion in RuntimeMetricsWriter.FirstChanceException (
Browse files Browse the repository at this point in the history
  • Loading branch information
kevingosse authored Jun 19, 2024
1 parent 8a14986 commit 07cb5eb
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions tracer/src/Datadog.Trace/RuntimeMetrics/RuntimeMetricsWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ internal class RuntimeMetricsWriter : IDisposable
private static readonly IDatadogLogger Log = DatadogLogging.GetLoggerFor<RuntimeMetricsWriter>();
private static readonly Func<IDogStatsd, TimeSpan, bool, IRuntimeMetricsListener> InitializeListenerFunc = InitializeListener;

[ThreadStatic]
private static bool _inspectingFirstChanceException;

private static int _pssConsecutiveFailures;

private readonly Process _process;
Expand Down Expand Up @@ -257,9 +260,27 @@ private static Process GetCurrentProcess()

private void FirstChanceException(object sender, FirstChanceExceptionEventArgs e)
{
var name = e.Exception.GetType().Name;
if (_inspectingFirstChanceException)
{
// In rare occasions, inspecting an exception could throw another exception
// We need to detect this to avoid infinite recursion
return;
}

try
{
_inspectingFirstChanceException = true;

_exceptionCounts.AddOrUpdate(name, 1, (_, count) => count + 1);
var name = e.Exception.GetType().Name;
_exceptionCounts.AddOrUpdate(name, 1, (_, count) => count + 1);
}
catch
{
}
finally
{
_inspectingFirstChanceException = false;
}
}

private void GetCurrentProcessMetrics(out TimeSpan userProcessorTime, out TimeSpan systemCpuTime, out int threadCount, out long privateMemorySize)
Expand Down

0 comments on commit 07cb5eb

Please sign in to comment.