[xbuild] Fix ProcessWrapper to WaitForExit(..) before accessing ExitCode
authorAnkit Jain <ankit.jain@xamarin.com>
Wed, 11 May 2016 20:54:46 +0000 (16:54 -0400)
committerAnkit Jain <ankit.jain@xamarin.com>
Wed, 11 May 2016 21:43:23 +0000 (17:43 -0400)
commitd47f479ba48c33b7e0ea7ec8e7a1c66d4674a9a4
tree43a61ccb5274346d85132e09c78fc6e115866b38
parent3c2fd4f371846cec6c8e8e780bbff66614185c26
[xbuild] Fix ProcessWrapper to WaitForExit(..) before accessing ExitCode

Process.Exited event can get invoked before the process has really
exited. So, accessing process.ExitCode before the real exit can throw:

Error executing task Exec: System.InvalidOperationException: Process must exit before requested information can be determined.
  at System.Diagnostics.Process.EnsureState (System.Diagnostics.Process+State state) [0x000b9] in /Users/ankit/dev/mono/mcs/class/referencesource/System/services/monitoring/system/diagnosticts/Process.cs:1439
  at System.Diagnostics.Process.get_ExitCode () [0x00000] in /Users/ankit/dev/mono/mcs/class/referencesource/System/services/monitoring/system/diagnosticts/Process.cs:219
  at (wrapper remoting-invoke-with-check) System.Diagnostics.Process:get_ExitCode ()
  at Microsoft.Build.Utilities.ToolTask.ExecuteTool (System.String pathToTool, System.String responseFileCommands, System.String commandLineCommands) [0x00101] in /Users/ankit/dev/mono/mcs/class/Microsoft.Build.Utilities/Microsoft.Build.Utilities/ToolTask.cs:185
  at Microsoft.Build.Tasks.Exec.ExecuteTool (System.String pathToTool, System.String responseFileCommands, System.String commandLineCommands) [0x00026] in /Users/ankit/dev/mono/mcs/class/Microsoft.Build.Tasks/Microsoft.Build.Tasks/Exec.cs:83
  at Microsoft.Build.Utilities.ToolTask.Execute () [0x0001c] in /Users/ankit/dev/mono/mcs/class/Microsoft.Build.Utilities/Microsoft.Build.Utilities/ToolTask.cs:128
  at Microsoft.Build.BuildEngine.TaskEngine.Execute () [0x00000] in /Users/ankit/dev/mono/mcs/class/Microsoft.Build.Engine/Microsoft.Build.BuildEngine/TaskEngine.cs:134
  at Microsoft.Build.BuildEngine.BuildTask.Execute () [0x0008f] in /Users/ankit/dev/mono/mcs/class/Microsoft.Build.Engine/Microsoft.Build.BuildEngine/BuildTask.cs:101

ProcessWrapper.WaitForOutput depends on the
`endEventExit`(ManualResetEvent) to be set, which is done in the event
handler for Process.Exited . So, effectively, WaitForOutput can return
before the process has really exited and ToolTask ends up throwing an
exception when it accesses the ExitCode.

Fix: Add a WaitForExit in WaitForOutput, to be sure!
mcs/class/Microsoft.Build.Utilities/Microsoft.Build.Utilities/ProcessWrapper.cs