@@ -28,6 +28,9 @@ internal class RuntimeMetricsWriter : IDisposable
2828 private static readonly IDatadogLogger Log = DatadogLogging . GetLoggerFor < RuntimeMetricsWriter > ( ) ;
2929 private static readonly Func < IDogStatsd , TimeSpan , bool , IRuntimeMetricsListener > InitializeListenerFunc = InitializeListener ;
3030
31+ [ ThreadStatic ]
32+ private static bool _inspectingFirstChanceException ;
33+
3134 private static int _pssConsecutiveFailures ;
3235
3336 private readonly Process _process ;
@@ -257,9 +260,27 @@ private static Process GetCurrentProcess()
257260
258261 private void FirstChanceException ( object sender , FirstChanceExceptionEventArgs e )
259262 {
260- var name = e . Exception . GetType ( ) . Name ;
263+ if ( _inspectingFirstChanceException )
264+ {
265+ // In rare occasions, inspecting an exception could throw another exception
266+ // We need to detect this to avoid infinite recursion
267+ return ;
268+ }
269+
270+ try
271+ {
272+ _inspectingFirstChanceException = true ;
261273
262- _exceptionCounts . AddOrUpdate ( name , 1 , ( _ , count ) => count + 1 ) ;
274+ var name = e . Exception . GetType ( ) . Name ;
275+ _exceptionCounts . AddOrUpdate ( name , 1 , ( _ , count ) => count + 1 ) ;
276+ }
277+ catch
278+ {
279+ }
280+ finally
281+ {
282+ _inspectingFirstChanceException = false ;
283+ }
263284 }
264285
265286 private void GetCurrentProcessMetrics ( out TimeSpan userProcessorTime , out TimeSpan systemCpuTime , out int threadCount , out long privateMemorySize )
0 commit comments