From 64968c4c30d13048e9dc963f13faa0305d08020b Mon Sep 17 00:00:00 2001 From: Michal Pavlik Date: Tue, 31 May 2022 10:34:02 +0200 Subject: [PATCH 1/5] Added cancellation support for client. --- src/Build/BackEnd/Client/MSBuildClient.cs | 55 ++++++++++--------- src/Build/BackEnd/Node/OutOfProcServerNode.cs | 15 +---- .../BackEnd/Node/ServerNodeBuildCancel.cs | 2 + .../PublicAPI/net/PublicAPI.Unshipped.txt | 2 +- .../netstandard/PublicAPI.Unshipped.txt | 2 +- src/MSBuild/XMake.cs | 14 ++--- 6 files changed, 41 insertions(+), 49 deletions(-) diff --git a/src/Build/BackEnd/Client/MSBuildClient.cs b/src/Build/BackEnd/Client/MSBuildClient.cs index 57c19c4a361..d41ff4a24aa 100644 --- a/src/Build/BackEnd/Client/MSBuildClient.cs +++ b/src/Build/BackEnd/Client/MSBuildClient.cs @@ -50,6 +50,11 @@ public sealed class MSBuildClient /// private bool _buildFinished = false; + /// + /// Whether the build was canceled. + /// + private bool _buildCanceled = false; + /// /// Handshake between server and client. /// @@ -190,6 +195,7 @@ public MSBuildClientExitResult Execute(string commandLine, CancellationToken can { case 0: HandleCancellation(); + waitHandles[0] = CancellationToken.None.WaitHandle; break; case 1: @@ -198,8 +204,7 @@ public MSBuildClientExitResult Execute(string commandLine, CancellationToken can case 2: while (packetPump.ReceivedPacketsQueue.TryDequeue(out INodePacket? packet) && - !_buildFinished && - !cancellationToken.IsCancellationRequested) + !_buildFinished) { if (packet != null) { @@ -222,7 +227,24 @@ public MSBuildClientExitResult Execute(string commandLine, CancellationToken can return _exitResult; } - private void SendCancelCommand(NamedPipeClientStream nodeStream) => throw new NotImplementedException(); + private bool TrySendPacket(Func packetResolver) + { + INodePacket? packet = null; + try + { + packet = packetResolver(); + WritePacket(_nodeStream, packet); + CommunicationsUtilities.Trace($"Command packet of type '{packet.Type}' sent..."); + } + catch (Exception ex) + { + CommunicationsUtilities.Trace($"Failed to send command packet of type '{packet?.Type.ToString() ?? "Unknown"}' to server: {0}", ex); + _exitResult.MSBuildClientExitType = MSBuildClientExitType.ConnectionError; + return false; + } + + return true; + } /// /// Launches MSBuild server. @@ -285,23 +307,9 @@ private Process LaunchNode(string exeLocation, string msBuildServerArguments, Di return Process.Start(processStartInfo) ?? throw new InvalidOperationException("MSBuild server node failed to launch."); } - private bool TrySendBuildCommand(string commandLine) - { - try - { - ServerNodeBuildCommand buildCommand = GetServerNodeBuildCommand(commandLine); - WritePacket(_nodeStream, buildCommand); - CommunicationsUtilities.Trace("Build command sent..."); - } - catch (Exception ex) - { - CommunicationsUtilities.Trace("Failed to send build command to server: {0}", ex); - _exitResult.MSBuildClientExitType = MSBuildClientExitType.ConnectionError; - return false; - } + private bool TrySendBuildCommand(string commandLine) => TrySendPacket(() => GetServerNodeBuildCommand(commandLine)); - return true; - } + private bool TrySendCancelCommand() => TrySendPacket(() => ServerNodeBuildCancel.Instance); private ServerNodeBuildCommand GetServerNodeBuildCommand(string commandLine) { @@ -338,14 +346,11 @@ private ServerNodeHandshake GetHandshake() /// private void HandleCancellation() { - // TODO. - // Send cancellation command to server. - // SendCancelCommand(_nodeStream); + TrySendCancelCommand(); Console.WriteLine("MSBuild client cancelled."); CommunicationsUtilities.Trace("MSBuild client cancelled."); - _exitResult.MSBuildClientExitType = MSBuildClientExitType.Cancelled; - _buildFinished = true; + _buildCanceled = true; } /// @@ -395,7 +400,7 @@ private void HandleServerNodeConsoleWrite(ServerNodeConsoleWrite consoleWrite) private void HandleServerNodeBuildResult(ServerNodeBuildResult response) { CommunicationsUtilities.Trace("Build response received: exit code {0}, exit type '{1}'", response.ExitCode, response.ExitType); - _exitResult.MSBuildClientExitType = MSBuildClientExitType.Success; + _exitResult.MSBuildClientExitType = _buildCanceled ? MSBuildClientExitType.Cancelled : MSBuildClientExitType.Success; _exitResult.MSBuildAppExitTypeString = response.ExitType; _buildFinished = true; } diff --git a/src/Build/BackEnd/Node/OutOfProcServerNode.cs b/src/Build/BackEnd/Node/OutOfProcServerNode.cs index f795a3eceae..5017de2d535 100644 --- a/src/Build/BackEnd/Node/OutOfProcServerNode.cs +++ b/src/Build/BackEnd/Node/OutOfProcServerNode.cs @@ -20,8 +20,6 @@ public sealed class OutOfProcServerNode : INode, INodePacketFactory, INodePacket { private readonly Func _buildFunction; - private readonly Action _onCancel; - /// /// The endpoint used to talk to the host. /// @@ -62,14 +60,11 @@ public sealed class OutOfProcServerNode : INode, INodePacketFactory, INodePacket /// private readonly bool _debugCommunications; - private Task? _buildTask; - private string _serverBusyMutexName = default!; - public OutOfProcServerNode(Func buildFunction, Action onCancel) + public OutOfProcServerNode(Func buildFunction) { _buildFunction = buildFunction; - _onCancel = onCancel; new Dictionary(); _debugCommunications = (Environment.GetEnvironmentVariable("MSBUILDDEBUGCOMM") == "1"); @@ -279,14 +274,14 @@ private void HandlePacket(INodePacket packet) HandleServerNodeBuildCommandAsync((ServerNodeBuildCommand)packet); break; case NodePacketType.ServerNodeBuildCancel: - _onCancel(); + BuildManager.DefaultBuildManager.CancelAllSubmissions(); break; } } private void HandleServerNodeBuildCommandAsync(ServerNodeBuildCommand command) { - _buildTask = Task.Run(() => + Task.Run(() => { try { @@ -298,10 +293,6 @@ private void HandleServerNodeBuildCommandAsync(ServerNodeBuildCommand command) _shutdownReason = NodeEngineShutdownReason.Error; _shutdownEvent.Set(); } - finally - { - _buildTask = null; - } }); } diff --git a/src/Build/BackEnd/Node/ServerNodeBuildCancel.cs b/src/Build/BackEnd/Node/ServerNodeBuildCancel.cs index 349c1b8170d..813d85c78bd 100644 --- a/src/Build/BackEnd/Node/ServerNodeBuildCancel.cs +++ b/src/Build/BackEnd/Node/ServerNodeBuildCancel.cs @@ -6,6 +6,8 @@ namespace Microsoft.Build.BackEnd { internal sealed class ServerNodeBuildCancel : INodePacket { + public static ServerNodeBuildCancel Instance { get; } = new ServerNodeBuildCancel(); + public NodePacketType Type => NodePacketType.ServerNodeBuildCancel; public void Translate(ITranslator translator) diff --git a/src/Build/PublicAPI/net/PublicAPI.Unshipped.txt b/src/Build/PublicAPI/net/PublicAPI.Unshipped.txt index 349a8e57aac..2f2373e0785 100644 --- a/src/Build/PublicAPI/net/PublicAPI.Unshipped.txt +++ b/src/Build/PublicAPI/net/PublicAPI.Unshipped.txt @@ -15,5 +15,5 @@ Microsoft.Build.Execution.MSBuildClientExitType.ServerBusy = 1 -> Microsoft.Buil Microsoft.Build.Execution.MSBuildClientExitType.Success = 0 -> Microsoft.Build.Execution.MSBuildClientExitType Microsoft.Build.Execution.MSBuildClientExitType.Unexpected = 4 -> Microsoft.Build.Execution.MSBuildClientExitType Microsoft.Build.Execution.OutOfProcServerNode -Microsoft.Build.Execution.OutOfProcServerNode.OutOfProcServerNode(System.Func buildFunction, System.Action onCancel) -> void +Microsoft.Build.Execution.OutOfProcServerNode.OutOfProcServerNode(System.Func buildFunction) -> void Microsoft.Build.Execution.OutOfProcServerNode.Run(out System.Exception shutdownException) -> Microsoft.Build.Execution.NodeEngineShutdownReason diff --git a/src/Build/PublicAPI/netstandard/PublicAPI.Unshipped.txt b/src/Build/PublicAPI/netstandard/PublicAPI.Unshipped.txt index 39c901f1b5c..b7e25f06956 100644 --- a/src/Build/PublicAPI/netstandard/PublicAPI.Unshipped.txt +++ b/src/Build/PublicAPI/netstandard/PublicAPI.Unshipped.txt @@ -15,5 +15,5 @@ Microsoft.Build.Execution.MSBuildClientExitType.ServerBusy = 1 -> Microsoft.Buil Microsoft.Build.Execution.MSBuildClientExitType.Success = 0 -> Microsoft.Build.Execution.MSBuildClientExitType Microsoft.Build.Execution.MSBuildClientExitType.Unexpected = 4 -> Microsoft.Build.Execution.MSBuildClientExitType Microsoft.Build.Execution.OutOfProcServerNode -Microsoft.Build.Execution.OutOfProcServerNode.OutOfProcServerNode(System.Func buildFunction, System.Action onCancel) -> void +Microsoft.Build.Execution.OutOfProcServerNode.OutOfProcServerNode(System.Func buildFunction) -> void Microsoft.Build.Execution.OutOfProcServerNode.Run(out System.Exception shutdownException) -> Microsoft.Build.Execution.NodeEngineShutdownReason \ No newline at end of file diff --git a/src/MSBuild/XMake.cs b/src/MSBuild/XMake.cs index b7d98c179d5..a14f09fccdb 100644 --- a/src/MSBuild/XMake.cs +++ b/src/MSBuild/XMake.cs @@ -224,6 +224,8 @@ string[] args int exitCode; if (Environment.GetEnvironmentVariable(Traits.UseMSBuildServerEnvVarName) == "1") { + Console.CancelKeyPress += Console_CancelKeyPress; + // Use the client app to execute build in msbuild server. Opt-in feature. exitCode = ((s_initialized && MSBuildClientApp.Execute( #if FEATURE_GET_COMMANDLINE @@ -866,8 +868,7 @@ private static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs { if (e.SpecialKey == ConsoleSpecialKey.ControlBreak) { - e.Cancel = false; // required; the process will now be terminated rudely - return; + Environment.Exit(1); // the process will now be terminated rudely } e.Cancel = true; // do not terminate rudely @@ -2684,14 +2685,7 @@ private static void StartLocalNode(CommandLineSwitches commandLineSwitches, bool return (exitCode, exitType.ToString()); }; - Action onCancel = () => - { - Console.WriteLine(ResourceUtilities.GetResourceString("AbortingBuild")); - - BuildManager.DefaultBuildManager.CancelAllSubmissions(); - }; - - OutOfProcServerNode node = new(buildFunction, onCancel); + OutOfProcServerNode node = new(buildFunction); s_isServerNode = true; shutdownReason = node.Run(out nodeException); From 82140708bb4ef4b7e0d6c922caf5d2f2567f002d Mon Sep 17 00:00:00 2001 From: Michal Pavlik Date: Tue, 31 May 2022 10:34:02 +0200 Subject: [PATCH 2/5] Added cancellation support for client. --- src/Build/BackEnd/Client/MSBuildClient.cs | 54 ++++++++++--------- src/Build/BackEnd/Node/OutOfProcServerNode.cs | 15 ++---- .../BackEnd/Node/ServerNodeBuildCancel.cs | 2 + .../PublicAPI/net/PublicAPI.Unshipped.txt | 2 +- .../netstandard/PublicAPI.Unshipped.txt | 2 +- src/MSBuild/XMake.cs | 14 ++--- 6 files changed, 41 insertions(+), 48 deletions(-) diff --git a/src/Build/BackEnd/Client/MSBuildClient.cs b/src/Build/BackEnd/Client/MSBuildClient.cs index c8b3c4adc7f..9e9ca8c58d5 100644 --- a/src/Build/BackEnd/Client/MSBuildClient.cs +++ b/src/Build/BackEnd/Client/MSBuildClient.cs @@ -51,6 +51,11 @@ public sealed class MSBuildClient /// private bool _buildFinished = false; + /// + /// Whether the build was canceled. + /// + private bool _buildCanceled = false; + /// /// Handshake between server and client. /// @@ -196,6 +201,7 @@ public MSBuildClientExitResult Execute(string commandLine, CancellationToken can { case 0: HandleCancellation(); + waitHandles[0] = CancellationToken.None.WaitHandle; break; case 1: @@ -204,8 +210,7 @@ public MSBuildClientExitResult Execute(string commandLine, CancellationToken can case 2: while (packetPump.ReceivedPacketsQueue.TryDequeue(out INodePacket? packet) && - !_buildFinished && - !cancellationToken.IsCancellationRequested) + !_buildFinished) { if (packet != null) { @@ -228,6 +233,24 @@ public MSBuildClientExitResult Execute(string commandLine, CancellationToken can return _exitResult; } + private bool TrySendPacket(Func packetResolver) + { + INodePacket? packet = null; + try + { + packet = packetResolver(); + WritePacket(_nodeStream, packet); + CommunicationsUtilities.Trace($"Command packet of type '{packet.Type}' sent..."); + } + catch (Exception ex) + { + CommunicationsUtilities.Trace($"Failed to send command packet of type '{packet?.Type.ToString() ?? "Unknown"}' to server: {0}", ex); + _exitResult.MSBuildClientExitType = MSBuildClientExitType.ConnectionError; + return false; + } + + return true; + } private void SupportVT100() { IntPtr stdOut = NativeMethodsShared.GetStdHandle(NativeMethodsShared.STD_OUTPUT_HANDLE); @@ -301,23 +324,9 @@ private Process LaunchNode(string exeLocation, string msBuildServerArguments, Di return Process.Start(processStartInfo) ?? throw new InvalidOperationException("MSBuild server node failed to launch."); } - private bool TrySendBuildCommand(string commandLine) - { - try - { - ServerNodeBuildCommand buildCommand = GetServerNodeBuildCommand(commandLine); - WritePacket(_nodeStream, buildCommand); - CommunicationsUtilities.Trace("Build command sent..."); - } - catch (Exception ex) - { - CommunicationsUtilities.Trace("Failed to send build command to server: {0}", ex); - _exitResult.MSBuildClientExitType = MSBuildClientExitType.ConnectionError; - return false; - } + private bool TrySendBuildCommand(string commandLine) => TrySendPacket(() => GetServerNodeBuildCommand(commandLine)); - return true; - } + private bool TrySendCancelCommand() => TrySendPacket(() => ServerNodeBuildCancel.Instance); private ServerNodeBuildCommand GetServerNodeBuildCommand(string commandLine) { @@ -354,14 +363,11 @@ private ServerNodeHandshake GetHandshake() /// private void HandleCancellation() { - // TODO. - // Send cancellation command to server. - // SendCancelCommand(_nodeStream); + TrySendCancelCommand(); Console.WriteLine("MSBuild client cancelled."); CommunicationsUtilities.Trace("MSBuild client cancelled."); - _exitResult.MSBuildClientExitType = MSBuildClientExitType.Cancelled; - _buildFinished = true; + _buildCanceled = true; } /// @@ -411,7 +417,7 @@ private void HandleServerNodeConsoleWrite(ServerNodeConsoleWrite consoleWrite) private void HandleServerNodeBuildResult(ServerNodeBuildResult response) { CommunicationsUtilities.Trace("Build response received: exit code {0}, exit type '{1}'", response.ExitCode, response.ExitType); - _exitResult.MSBuildClientExitType = MSBuildClientExitType.Success; + _exitResult.MSBuildClientExitType = _buildCanceled ? MSBuildClientExitType.Cancelled : MSBuildClientExitType.Success; _exitResult.MSBuildAppExitTypeString = response.ExitType; _buildFinished = true; } diff --git a/src/Build/BackEnd/Node/OutOfProcServerNode.cs b/src/Build/BackEnd/Node/OutOfProcServerNode.cs index f795a3eceae..5017de2d535 100644 --- a/src/Build/BackEnd/Node/OutOfProcServerNode.cs +++ b/src/Build/BackEnd/Node/OutOfProcServerNode.cs @@ -20,8 +20,6 @@ public sealed class OutOfProcServerNode : INode, INodePacketFactory, INodePacket { private readonly Func _buildFunction; - private readonly Action _onCancel; - /// /// The endpoint used to talk to the host. /// @@ -62,14 +60,11 @@ public sealed class OutOfProcServerNode : INode, INodePacketFactory, INodePacket /// private readonly bool _debugCommunications; - private Task? _buildTask; - private string _serverBusyMutexName = default!; - public OutOfProcServerNode(Func buildFunction, Action onCancel) + public OutOfProcServerNode(Func buildFunction) { _buildFunction = buildFunction; - _onCancel = onCancel; new Dictionary(); _debugCommunications = (Environment.GetEnvironmentVariable("MSBUILDDEBUGCOMM") == "1"); @@ -279,14 +274,14 @@ private void HandlePacket(INodePacket packet) HandleServerNodeBuildCommandAsync((ServerNodeBuildCommand)packet); break; case NodePacketType.ServerNodeBuildCancel: - _onCancel(); + BuildManager.DefaultBuildManager.CancelAllSubmissions(); break; } } private void HandleServerNodeBuildCommandAsync(ServerNodeBuildCommand command) { - _buildTask = Task.Run(() => + Task.Run(() => { try { @@ -298,10 +293,6 @@ private void HandleServerNodeBuildCommandAsync(ServerNodeBuildCommand command) _shutdownReason = NodeEngineShutdownReason.Error; _shutdownEvent.Set(); } - finally - { - _buildTask = null; - } }); } diff --git a/src/Build/BackEnd/Node/ServerNodeBuildCancel.cs b/src/Build/BackEnd/Node/ServerNodeBuildCancel.cs index 349c1b8170d..813d85c78bd 100644 --- a/src/Build/BackEnd/Node/ServerNodeBuildCancel.cs +++ b/src/Build/BackEnd/Node/ServerNodeBuildCancel.cs @@ -6,6 +6,8 @@ namespace Microsoft.Build.BackEnd { internal sealed class ServerNodeBuildCancel : INodePacket { + public static ServerNodeBuildCancel Instance { get; } = new ServerNodeBuildCancel(); + public NodePacketType Type => NodePacketType.ServerNodeBuildCancel; public void Translate(ITranslator translator) diff --git a/src/Build/PublicAPI/net/PublicAPI.Unshipped.txt b/src/Build/PublicAPI/net/PublicAPI.Unshipped.txt index 349a8e57aac..2f2373e0785 100644 --- a/src/Build/PublicAPI/net/PublicAPI.Unshipped.txt +++ b/src/Build/PublicAPI/net/PublicAPI.Unshipped.txt @@ -15,5 +15,5 @@ Microsoft.Build.Execution.MSBuildClientExitType.ServerBusy = 1 -> Microsoft.Buil Microsoft.Build.Execution.MSBuildClientExitType.Success = 0 -> Microsoft.Build.Execution.MSBuildClientExitType Microsoft.Build.Execution.MSBuildClientExitType.Unexpected = 4 -> Microsoft.Build.Execution.MSBuildClientExitType Microsoft.Build.Execution.OutOfProcServerNode -Microsoft.Build.Execution.OutOfProcServerNode.OutOfProcServerNode(System.Func buildFunction, System.Action onCancel) -> void +Microsoft.Build.Execution.OutOfProcServerNode.OutOfProcServerNode(System.Func buildFunction) -> void Microsoft.Build.Execution.OutOfProcServerNode.Run(out System.Exception shutdownException) -> Microsoft.Build.Execution.NodeEngineShutdownReason diff --git a/src/Build/PublicAPI/netstandard/PublicAPI.Unshipped.txt b/src/Build/PublicAPI/netstandard/PublicAPI.Unshipped.txt index 39c901f1b5c..b7e25f06956 100644 --- a/src/Build/PublicAPI/netstandard/PublicAPI.Unshipped.txt +++ b/src/Build/PublicAPI/netstandard/PublicAPI.Unshipped.txt @@ -15,5 +15,5 @@ Microsoft.Build.Execution.MSBuildClientExitType.ServerBusy = 1 -> Microsoft.Buil Microsoft.Build.Execution.MSBuildClientExitType.Success = 0 -> Microsoft.Build.Execution.MSBuildClientExitType Microsoft.Build.Execution.MSBuildClientExitType.Unexpected = 4 -> Microsoft.Build.Execution.MSBuildClientExitType Microsoft.Build.Execution.OutOfProcServerNode -Microsoft.Build.Execution.OutOfProcServerNode.OutOfProcServerNode(System.Func buildFunction, System.Action onCancel) -> void +Microsoft.Build.Execution.OutOfProcServerNode.OutOfProcServerNode(System.Func buildFunction) -> void Microsoft.Build.Execution.OutOfProcServerNode.Run(out System.Exception shutdownException) -> Microsoft.Build.Execution.NodeEngineShutdownReason \ No newline at end of file diff --git a/src/MSBuild/XMake.cs b/src/MSBuild/XMake.cs index b7d98c179d5..a14f09fccdb 100644 --- a/src/MSBuild/XMake.cs +++ b/src/MSBuild/XMake.cs @@ -224,6 +224,8 @@ string[] args int exitCode; if (Environment.GetEnvironmentVariable(Traits.UseMSBuildServerEnvVarName) == "1") { + Console.CancelKeyPress += Console_CancelKeyPress; + // Use the client app to execute build in msbuild server. Opt-in feature. exitCode = ((s_initialized && MSBuildClientApp.Execute( #if FEATURE_GET_COMMANDLINE @@ -866,8 +868,7 @@ private static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs { if (e.SpecialKey == ConsoleSpecialKey.ControlBreak) { - e.Cancel = false; // required; the process will now be terminated rudely - return; + Environment.Exit(1); // the process will now be terminated rudely } e.Cancel = true; // do not terminate rudely @@ -2684,14 +2685,7 @@ private static void StartLocalNode(CommandLineSwitches commandLineSwitches, bool return (exitCode, exitType.ToString()); }; - Action onCancel = () => - { - Console.WriteLine(ResourceUtilities.GetResourceString("AbortingBuild")); - - BuildManager.DefaultBuildManager.CancelAllSubmissions(); - }; - - OutOfProcServerNode node = new(buildFunction, onCancel); + OutOfProcServerNode node = new(buildFunction); s_isServerNode = true; shutdownReason = node.Run(out nodeException); From 3175330bc4de7d9d91be8215d2b167c45311a5b7 Mon Sep 17 00:00:00 2001 From: Michal Pavlik Date: Tue, 31 May 2022 10:52:35 +0200 Subject: [PATCH 3/5] Fixing wrong merge --- src/Build/BackEnd/Client/MSBuildClient.cs | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/src/Build/BackEnd/Client/MSBuildClient.cs b/src/Build/BackEnd/Client/MSBuildClient.cs index 89bed9193bd..0d287ce9638 100644 --- a/src/Build/BackEnd/Client/MSBuildClient.cs +++ b/src/Build/BackEnd/Client/MSBuildClient.cs @@ -233,24 +233,6 @@ public MSBuildClientExitResult Execute(string commandLine, CancellationToken can return _exitResult; } - private bool TrySendPacket(Func packetResolver) - { - INodePacket? packet = null; - try - { - packet = packetResolver(); - WritePacket(_nodeStream, packet); - CommunicationsUtilities.Trace($"Command packet of type '{packet.Type}' sent..."); - } - catch (Exception ex) - { - CommunicationsUtilities.Trace($"Failed to send command packet of type '{packet?.Type.ToString() ?? "Unknown"}' to server: {0}", ex); - _exitResult.MSBuildClientExitType = MSBuildClientExitType.ConnectionError; - return false; - } - - return true; - } private void SupportVT100() { IntPtr stdOut = NativeMethodsShared.GetStdHandle(NativeMethodsShared.STD_OUTPUT_HANDLE); From 719bd2dc3190e0b49b8a3cd9c03f7ffbb193c0ed Mon Sep 17 00:00:00 2001 From: Michal Pavlik Date: Mon, 6 Jun 2022 15:29:23 +0200 Subject: [PATCH 4/5] Removed "Cancelled" exit type --- src/Build/BackEnd/Client/MSBuildClient.cs | 9 +-------- src/Build/BackEnd/Client/MSBuildClientExitType.cs | 6 +----- 2 files changed, 2 insertions(+), 13 deletions(-) diff --git a/src/Build/BackEnd/Client/MSBuildClient.cs b/src/Build/BackEnd/Client/MSBuildClient.cs index 0d287ce9638..00e0cf1dae8 100644 --- a/src/Build/BackEnd/Client/MSBuildClient.cs +++ b/src/Build/BackEnd/Client/MSBuildClient.cs @@ -8,7 +8,6 @@ using System.Globalization; using System.IO; using System.IO.Pipes; -using System.Runtime.InteropServices; using System.Threading; using Microsoft.Build.BackEnd; using Microsoft.Build.BackEnd.Client; @@ -51,11 +50,6 @@ public sealed class MSBuildClient /// private bool _buildFinished = false; - /// - /// Whether the build was canceled. - /// - private bool _buildCanceled = false; - /// /// Handshake between server and client. /// @@ -366,7 +360,6 @@ private void HandleCancellation() Console.WriteLine("MSBuild client cancelled."); CommunicationsUtilities.Trace("MSBuild client cancelled."); - _buildCanceled = true; } /// @@ -416,7 +409,7 @@ private void HandleServerNodeConsoleWrite(ServerNodeConsoleWrite consoleWrite) private void HandleServerNodeBuildResult(ServerNodeBuildResult response) { CommunicationsUtilities.Trace("Build response received: exit code {0}, exit type '{1}'", response.ExitCode, response.ExitType); - _exitResult.MSBuildClientExitType = _buildCanceled ? MSBuildClientExitType.Cancelled : MSBuildClientExitType.Success; + _exitResult.MSBuildClientExitType = MSBuildClientExitType.Success; _exitResult.MSBuildAppExitTypeString = response.ExitType; _buildFinished = true; } diff --git a/src/Build/BackEnd/Client/MSBuildClientExitType.cs b/src/Build/BackEnd/Client/MSBuildClientExitType.cs index 70bbc0113c8..c72bc0a6878 100644 --- a/src/Build/BackEnd/Client/MSBuildClientExitType.cs +++ b/src/Build/BackEnd/Client/MSBuildClientExitType.cs @@ -24,10 +24,6 @@ public enum MSBuildClientExitType /// The build stopped unexpectedly, for example, /// because a named pipe between the server and the client was unexpectedly closed. /// - Unexpected, - /// - /// The build was cancelled. - /// - Cancelled + Unexpected } } From e5cd1b6408bd83363fb5f044a28f49b8cc867c2f Mon Sep 17 00:00:00 2001 From: Michal Pavlik Date: Tue, 7 Jun 2022 10:16:16 +0200 Subject: [PATCH 5/5] Resolving comments --- src/Build/BackEnd/Client/MSBuildClient.cs | 7 ++++--- src/Build/BackEnd/Node/ServerNodeBuildCancel.cs | 4 +--- src/Build/PublicAPI/net/PublicAPI.Unshipped.txt | 1 - src/Build/PublicAPI/netstandard/PublicAPI.Unshipped.txt | 1 - 4 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/Build/BackEnd/Client/MSBuildClient.cs b/src/Build/BackEnd/Client/MSBuildClient.cs index 00e0cf1dae8..59fff38950e 100644 --- a/src/Build/BackEnd/Client/MSBuildClient.cs +++ b/src/Build/BackEnd/Client/MSBuildClient.cs @@ -195,6 +195,8 @@ public MSBuildClientExitResult Execute(string commandLine, CancellationToken can { case 0: HandleCancellation(); + // After the cancelation, we want to wait to server gracefuly finish the build. + // We have to replace the cancelation handle, because WaitAny would cause to repeatedly hit this branch of code. waitHandles[0] = CancellationToken.None.WaitHandle; break; @@ -319,7 +321,7 @@ private Process LaunchNode(string exeLocation, string msBuildServerArguments, Di private bool TrySendBuildCommand(string commandLine) => TrySendPacket(() => GetServerNodeBuildCommand(commandLine)); - private bool TrySendCancelCommand() => TrySendPacket(() => ServerNodeBuildCancel.Instance); + private bool TrySendCancelCommand() => TrySendPacket(() => new ServerNodeBuildCancel()); private ServerNodeBuildCommand GetServerNodeBuildCommand(string commandLine) { @@ -358,8 +360,7 @@ private void HandleCancellation() { TrySendCancelCommand(); - Console.WriteLine("MSBuild client cancelled."); - CommunicationsUtilities.Trace("MSBuild client cancelled."); + CommunicationsUtilities.Trace("MSBuild client sent cancelation command."); } /// diff --git a/src/Build/BackEnd/Node/ServerNodeBuildCancel.cs b/src/Build/BackEnd/Node/ServerNodeBuildCancel.cs index 813d85c78bd..fba7f613819 100644 --- a/src/Build/BackEnd/Node/ServerNodeBuildCancel.cs +++ b/src/Build/BackEnd/Node/ServerNodeBuildCancel.cs @@ -5,9 +5,7 @@ namespace Microsoft.Build.BackEnd { internal sealed class ServerNodeBuildCancel : INodePacket - { - public static ServerNodeBuildCancel Instance { get; } = new ServerNodeBuildCancel(); - + { public NodePacketType Type => NodePacketType.ServerNodeBuildCancel; public void Translate(ITranslator translator) diff --git a/src/Build/PublicAPI/net/PublicAPI.Unshipped.txt b/src/Build/PublicAPI/net/PublicAPI.Unshipped.txt index 2f2373e0785..4e534a34a00 100644 --- a/src/Build/PublicAPI/net/PublicAPI.Unshipped.txt +++ b/src/Build/PublicAPI/net/PublicAPI.Unshipped.txt @@ -8,7 +8,6 @@ Microsoft.Build.Execution.MSBuildClientExitResult.MSBuildClientExitResult() -> v Microsoft.Build.Execution.MSBuildClientExitResult.MSBuildClientExitType.get -> Microsoft.Build.Execution.MSBuildClientExitType Microsoft.Build.Execution.MSBuildClientExitResult.MSBuildClientExitType.set -> void Microsoft.Build.Execution.MSBuildClientExitType -Microsoft.Build.Execution.MSBuildClientExitType.Cancelled = 5 -> Microsoft.Build.Execution.MSBuildClientExitType Microsoft.Build.Execution.MSBuildClientExitType.ConnectionError = 2 -> Microsoft.Build.Execution.MSBuildClientExitType Microsoft.Build.Execution.MSBuildClientExitType.LaunchError = 3 -> Microsoft.Build.Execution.MSBuildClientExitType Microsoft.Build.Execution.MSBuildClientExitType.ServerBusy = 1 -> Microsoft.Build.Execution.MSBuildClientExitType diff --git a/src/Build/PublicAPI/netstandard/PublicAPI.Unshipped.txt b/src/Build/PublicAPI/netstandard/PublicAPI.Unshipped.txt index b7e25f06956..a5e2e1ca86b 100644 --- a/src/Build/PublicAPI/netstandard/PublicAPI.Unshipped.txt +++ b/src/Build/PublicAPI/netstandard/PublicAPI.Unshipped.txt @@ -8,7 +8,6 @@ Microsoft.Build.Execution.MSBuildClientExitResult.MSBuildClientExitResult() -> v Microsoft.Build.Execution.MSBuildClientExitResult.MSBuildClientExitType.get -> Microsoft.Build.Execution.MSBuildClientExitType Microsoft.Build.Execution.MSBuildClientExitResult.MSBuildClientExitType.set -> void Microsoft.Build.Execution.MSBuildClientExitType -Microsoft.Build.Execution.MSBuildClientExitType.Cancelled = 5 -> Microsoft.Build.Execution.MSBuildClientExitType Microsoft.Build.Execution.MSBuildClientExitType.ConnectionError = 2 -> Microsoft.Build.Execution.MSBuildClientExitType Microsoft.Build.Execution.MSBuildClientExitType.LaunchError = 3 -> Microsoft.Build.Execution.MSBuildClientExitType Microsoft.Build.Execution.MSBuildClientExitType.ServerBusy = 1 -> Microsoft.Build.Execution.MSBuildClientExitType