Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions src/Build/BackEnd/Node/OutOfProcServerNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.Build.BackEnd;
using Microsoft.Build.Shared;
using Microsoft.Build.Internal;
using System.Threading.Tasks;

namespace Microsoft.Build.Execution
{
Expand Down Expand Up @@ -59,6 +60,8 @@ public sealed class OutOfProcServerNode : INode, INodePacketFactory, INodePacket
/// </summary>
private readonly bool _debugCommunications;

private Task? _buildTask;

private string _serverBusyMutexName = default!;

public OutOfProcServerNode(Func<string, (int exitCode, string exitType)> buildFunction)
Expand All @@ -74,6 +77,7 @@ public OutOfProcServerNode(Func<string, (int exitCode, string exitType)> buildFu

(this as INodePacketFactory).RegisterPacketHandler(NodePacketType.ServerNodeBuildCommand, ServerNodeBuildCommand.FactoryForDeserialization, this);
(this as INodePacketFactory).RegisterPacketHandler(NodePacketType.NodeBuildComplete, NodeBuildComplete.FactoryForDeserialization, this);
(this as INodePacketFactory).RegisterPacketHandler(NodePacketType.ServerNodeBuildCancel, ServerNodeBuildCancel.FactoryForDeserialization, this);
}

#region INode Members
Expand Down Expand Up @@ -105,7 +109,7 @@ public NodeEngineShutdownReason Run(out Exception? shutdownException)
_nodeEndpoint.Listen(this);

var waitHandles = new WaitHandle[] { _shutdownEvent, _packetReceivedEvent };

// Get the current directory before doing any work. We need this so we can restore the directory when the node shutsdown.
while (true)
{
Expand Down Expand Up @@ -269,11 +273,40 @@ private void HandlePacket(INodePacket packet)
switch (packet.Type)
{
case NodePacketType.ServerNodeBuildCommand:
HandleServerNodeBuildCommand((ServerNodeBuildCommand)packet);
HandleServerNodeBuildCommandAsync((ServerNodeBuildCommand)packet);
break;
case NodePacketType.ServerNodeBuildCancel:
HandleServerNodeBuildCancel((ServerNodeBuildCancel)packet);
break;
}
}

private void HandleServerNodeBuildCancel(ServerNodeBuildCancel command)
{
BuildManager.DefaultBuildManager.CancelAllSubmissions();

@AR-May AR-May May 20, 2022

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder, why can't we reuse here the cancellation from the MSBuildApp.Execute() function, by throwing the cancellation event manually? Also, how the things about the server after the current cancellation implementation works? Does server send the ServerNodeBuildResult package with the relevant information? A unit test would be helpful here, as we would see the intended output after the cancellation as well as check that the build was finalized.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We discussed with Roman what we can reuse from the cancellation code in XMake (Console_CancelKeyPress). It contains some handling for interactive console, but server node works in non-interactive console.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I planned to add test when Nathan's PR with server node tests is merged. We can merge his PR first and I will add test for cancelation.

}

private void HandleServerNodeBuildCommandAsync(ServerNodeBuildCommand command)
{
_buildTask = Task.Run(() =>
{
try
{
HandleServerNodeBuildCommand(command);
}
catch(Exception e)
{
_shutdownException = e;
_shutdownReason = NodeEngineShutdownReason.Error;
_shutdownEvent.Set();
}
finally
{
_buildTask = null;
}
});
}

private void HandleServerNodeBuildCommand(ServerNodeBuildCommand command)
{
CommunicationsUtilities.Trace("Building with MSBuild server with command line {0}", command.CommandLine);
Expand All @@ -285,6 +318,8 @@ private void HandleServerNodeBuildCommand(ServerNodeBuildCommand command)
_shutdownException = new InvalidOperationException("Client requested build while server is busy processing previous client build request.");
_shutdownReason = NodeEngineShutdownReason.Error;
_shutdownEvent.Set();

return;
}

// set build process context
Expand Down
20 changes: 20 additions & 0 deletions src/Build/BackEnd/Node/ServerNodeBuildCancel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
//

namespace Microsoft.Build.BackEnd
{
internal sealed class ServerNodeBuildCancel : INodePacket
{
public NodePacketType Type => NodePacketType.ServerNodeBuildCancel;

public void Translate(ITranslator translator)
{
}

internal static INodePacket FactoryForDeserialization(ITranslator translator)
{
return new ServerNodeBuildCancel();
}
}
}
1 change: 1 addition & 0 deletions src/Build/Microsoft.Build.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@
<Compile Include="FileSystem\*.cs" />
<Compile Include="BackEnd\Node\ConsoleOutput.cs" />
<Compile Include="BackEnd\Node\ServerNamedMutex.cs" />
<Compile Include="BackEnd\Node\ServerNodeBuildCancel.cs" />
<Compile Include="BackEnd\Node\ServerNodeBuildCommand.cs" />
<Compile Include="BackEnd\Node\ServerNodeConsoleWrite.cs" />
<Compile Include="BackEnd\Node\ServerNodeBuildResult.cs" />
Expand Down
6 changes: 6 additions & 0 deletions src/Shared/INodePacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ internal enum NodePacketType : byte
/// Keep this enum value constant intact as this is part of contract with dotnet CLI
/// </summary>
ServerNodeConsoleWrite = 0xF2,

/// <summary>
/// Command to cancel ongoing build.
/// Keep this enum value constant intact as this is part of contract with dotnet CLI
/// </summary>
ServerNodeBuildCancel = 0xF3,
}
#endregion

Expand Down