From: David Wrighton Date: Thu, 18 Feb 2021 03:44:04 +0000 (-0800) Subject: Address a race condition identified in CI (#2013) X-Git-Tag: submit/tizen/20210909.063632~17^2~123 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=6540da451edb04542e758af91fe3591aca6e4ea9;p=platform%2Fcore%2Fdotnet%2Fdiagnostics.git Address a race condition identified in CI (#2013) --- diff --git a/src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs b/src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs index 0400bec2b..8c959a359 100644 --- a/src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs +++ b/src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs @@ -163,6 +163,28 @@ namespace Microsoft.Diagnostics.Tools.Trace { process = Process.GetProcessById(processId); } + string processMainModuleFileName = ""; + + // Reading the process MainModule filename can fail if the target process closes + // or isn't fully setup. Retry a few times to attempt to address the issue + for (int attempts = 0; true; attempts++) + { + try + { + processMainModuleFileName = process.MainModule.FileName; + break; + } + catch + { + if (attempts > 10) + { + Console.Error.WriteLine("Unable to examine process."); + return ErrorCodes.SessionCreationError; + } + Thread.Sleep(200); + } + } + var shouldExit = new ManualResetEvent(false); var shouldStopAfterDuration = duration != default(TimeSpan); var rundownRequested = false; @@ -207,7 +229,7 @@ namespace Microsoft.Diagnostics.Tools.Trace using (var fs = new FileStream(output.FullName, FileMode.Create, FileAccess.Write)) { - Console.Out.WriteLine($"Process : {process.MainModule.FileName}"); + Console.Out.WriteLine($"Process : {processMainModuleFileName}"); Console.Out.WriteLine($"Output File : {fs.Name}"); if (shouldStopAfterDuration) Console.Out.WriteLine($"Trace Duration : {duration.ToString(@"dd\:hh\:mm\:ss")}");