Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When sharing the terminal with child nodes, wait for the children to terminate before exiting ourselves. #6053

Merged
merged 19 commits into from
Feb 25, 2021
Merged
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,23 @@ protected void ShutdownConnectedNodes(List<NodeContext> contextsToShutDown, bool
// Send the build completion message to the nodes, causing them to shutdown or reset.
_processesToIgnore.Clear();

// We wait for child nodes to exit to avoid them changing the terminal
// after this process terminates.
bool waitForExit = !enableReuse &&
!Console.IsInputRedirected &&
tmds marked this conversation as resolved.
Show resolved Hide resolved
Traits.Instance.EscapeHatches.EnsureStdOutForChildNodesIsPrimaryStdout;
Forgind marked this conversation as resolved.
Show resolved Hide resolved

foreach (NodeContext nodeContext in contextsToShutDown)
{
nodeContext?.SendData(new NodeBuildComplete(enableReuse));
if (nodeContext is null)
{
continue;
}
nodeContext.SendData(new NodeBuildComplete(enableReuse));
if (waitForExit)
tmds marked this conversation as resolved.
Show resolved Hide resolved
{
nodeContext.WaitForExit();
tmds marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

Expand Down Expand Up @@ -792,8 +806,9 @@ public void SendData(INodePacket packet)
/// <summary>
/// Closes the node's context, disconnecting it from the node.
/// </summary>
public void Close()
private void Close()
{
_processId = -1;
_clientToServerStream.Dispose();
if (!object.ReferenceEquals(_clientToServerStream, _serverToClientStream))
{
Expand All @@ -802,6 +817,30 @@ public void Close()
_terminateDelegate(_nodeId);
}

/// <summary>
/// Waits for the child node process to exit.
/// </summary>
public void WaitForExit()
{
int processId = _processId;
if (processId != -1)
{
Process childProcess;
try
{
childProcess = Process.GetProcessById(processId);
}
catch (System.ArgumentException)
{
// The process has terminated already.
return;
}
// Wait for the process to terminate.
CommunicationsUtilities.Trace("Waiting for node with pid = {0} to terminate", processId);
childProcess.WaitForExit();
tmds marked this conversation as resolved.
Show resolved Hide resolved
}
}

#if FEATURE_APM
/// <summary>
/// Completes the asynchronous packet write to the node.
Expand Down