Make dotnet-counters exit gracefully on runtime shutdown
authorSung Yoon Whang <suwhang@microsoft.com>
Tue, 7 May 2019 21:58:42 +0000 (14:58 -0700)
committerAndrew Au <cshung@gmail.com>
Wed, 8 May 2019 00:26:20 +0000 (17:26 -0700)
src/Tools/dotnet-counters/CounterMonitor.cs

index d0c88ecae7a3352574a21bb8d29f012d556651da..ffc35eaa403ea75ef3f59cece5e0ef3008a39e2e 100644 (file)
@@ -151,6 +151,8 @@ namespace Microsoft.Diagnostics.Tools.Counters
                 providerString = sb.ToString();
             }
 
+            var terminated = false;
+
             Task monitorTask = new Task(() => {
                 var configuration = new SessionConfiguration(
                     circularBufferSizeMB: 1000,
@@ -162,6 +164,7 @@ namespace Microsoft.Diagnostics.Tools.Counters
                 writer.InitializeDisplay();
                 source.Dynamic.All += Dynamic_All;
                 source.Process();
+                terminated = true; // This indicates that the runtime is done. We shoudn't try to talk to it anymore.
             });
 
             Task commandTask = new Task(() =>
@@ -189,12 +192,15 @@ namespace Microsoft.Diagnostics.Tools.Counters
             commandTask.Start();
             await commandTask;
 
-            try
+            if (!terminated)
             {
-                EventPipeClient.StopTracing(_processId, _sessionId);    
+                try
+                {
+                    EventPipeClient.StopTracing(_processId, _sessionId);    
+                }
+                catch (System.IO.EndOfStreamException) {} // If the app we're monitoring exits abrubtly, this may throw in which case we just swallow the exception and exit gracefully.    
             }
-            catch (System.IO.EndOfStreamException) {} // If the app we're monitoring exits abrubtly, this may throw in which case we just swallow the exception and exit gracefully.
-
+            
             return 0;
         }
     }