Terminate dotnet-trace when the runtime closes the eventpipe
authorAndrew Au <andrewau@microsoft.com>
Mon, 6 May 2019 18:01:28 +0000 (11:01 -0700)
committerAndrew Au <cshung@gmail.com>
Mon, 6 May 2019 19:09:12 +0000 (12:09 -0700)
src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs

index 4361d5b643678220ec2e5ecacb769456ff10973c..6d42a03ddbfa83a914c2ff32a7b3eebd81f6ee7a 100644 (file)
@@ -86,6 +86,7 @@ namespace Microsoft.Diagnostics.Tools.Trace
                     providers: providerCollection);
 
                 var shouldExit = new ManualResetEvent(false);
+                var terminated = false;
 
                 ulong sessionId = 0;
                 using (Stream stream = EventPipeClient.CollectTracing(processId, configuration, out sessionId))
@@ -118,6 +119,8 @@ namespace Microsoft.Diagnostics.Tools.Trace
 
                                 Debug.WriteLine($"PACKET: {Convert.ToBase64String(buffer, 0, nBytesRead)} (bytes {nBytesRead})");
                             }
+                            terminated = true;
+                            shouldExit.Set();
                         }
                     });
                     collectingTask.Start();
@@ -132,7 +135,10 @@ namespace Microsoft.Diagnostics.Tools.Trace
                         while (!Console.KeyAvailable && !shouldExit.WaitOne(250)) { }
                     } while (!shouldExit.WaitOne(0) && Console.ReadKey(true).Key != ConsoleKey.Enter);
 
-                    EventPipeClient.StopTracing(processId, sessionId);
+                    if (!terminated)
+                    {
+                        EventPipeClient.StopTracing(processId, sessionId);
+                    }
                     collectingTask.Wait();
                 }