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

Tried to improve the isolation server for isolated package actions. #1402

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions Cli/TapEntry.cs
Expand Up @@ -64,7 +64,7 @@ private static bool IsColor()
return false;
}

private static void goIsolated()
private static void RunIsolationServer()
{
if (IsColor())
{
Expand Down Expand Up @@ -201,6 +201,8 @@ public static void Go()
{
if (ExecutorClient.IsExecutorMode)
{
// this is a sub process of tap.exe, meaning we are running in an isolated instance.
// This also means that the parent process tap.exe through RunIsolationServer.
goInProcess();
return;
}
Expand All @@ -218,7 +220,7 @@ public static void Go()

if ((installCommand || uninstallCommand || packageManagerCommand) && !noIsolation)
{
goIsolated();
RunIsolationServer();
}
else
{
Expand Down
2 changes: 2 additions & 0 deletions Package/PackageActions/IsolatedPackageAction.cs
Expand Up @@ -60,6 +60,8 @@ public override int Execute(CancellationToken cancellationToken)
if (!ExecutorClient.IsRunningIsolated) // are we already running isolated?
{
// Detected Executor, try to run running isolated...
// This means, we will copy the files and send a command back to the isolator server (first tap.exe)
// which will run the isolated command.
try
{
RunIsolated(target: Target, isolatedAction: this);
Expand Down
70 changes: 30 additions & 40 deletions Shared/ExecutorInterop.cs
Expand Up @@ -44,43 +44,22 @@ public ExecutorSubProcess(ProcessStartInfo start)
this.start = start;
}

void pipeConnected()
void ProcessPipe()
{

byte[] buffer = new byte[1024];

void readMessage()
while (true)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe use while (!tokenSource.Token.IsCancellationRequested) here?

{
Pipe.ReadAsync(buffer, 0, buffer.Length, tokenSource.Token)
.ContinueWith(tsk =>
{
if ((tsk.IsCanceled || tsk.IsFaulted) == false)
gotMessage(tsk.Result);
});
var read = Pipe.Read(buffer, 0, buffer.Length);
if (read == 0) break;
var str = Encoding.UTF8.GetString(buffer, 0, read);
if (MessageReceived != null)
MessageReceived(this, str);
}

void gotMessage(int cnt)
{
if (tokenSource.IsCancellationRequested)
return;
var str = Encoding.UTF8.GetString(buffer, 0, cnt);
pushMessage(str);
Array.Clear(buffer, 0, cnt);
readMessage();
}
readMessage();
}

public event EventHandler<string> MessageReceived;

void pushMessage(string msg)
{
if (MessageReceived != null)
MessageReceived(this, msg);
}

CancellationTokenSource tokenSource = new CancellationTokenSource();


public static ExecutorSubProcess Create(string name, string args, bool isolated = false)
{
var start = new ProcessStartInfo(name, args)
Expand Down Expand Up @@ -115,41 +94,51 @@ public static NamedPipeServerStream getStream(out string pipeName)

public void Start()
{
//Console.WriteLine("STARTING SUBPROCESS: tap.exe " + start.Arguments + " @ " + start.WorkingDirectory);
string pipeName;
Pipe = getStream(out pipeName);
start.Environment[EnvVarNames.TpmInteropPipeName] = pipeName;
Pipe.WaitForConnectionAsync().ContinueWith(_ => pipeConnected());

new Thread(() =>
{
try
{
Pipe.WaitForConnection();
}
catch
{
// this is probably ok. It just means that the child process is not going to communicate back.
return;
}

ProcessPipe();
}).Start();

Process = Process.Start(start);
Process.EnableRaisingEvents = true;

Task t1 = RedirectOutput(Process.StandardOutput, Console.Write);
Task t2 = RedirectOutput(Process.StandardError, Console.Error.Write);
new Thread(() => RedirectOutput(Process.StandardOutput, Console.Out.Write)).Start();
new Thread(() => RedirectOutput(Process.StandardError, Console.Error.Write)).Start();
}

async Task RedirectOutput(StreamReader reader, Action<string> callback)
void RedirectOutput(StreamReader reader, Action<string> callback)
{
char[] buffer = new char[256];
int count;

while ((count = await reader.ReadAsync(buffer, 0, buffer.Length)) > 0)
while ((count = reader.Read(buffer, 0, buffer.Length)) > 0)
{
callback(new string(buffer, 0, count));
}
}

public void Dispose()
{
tokenSource.Cancel();
if (Pipe != null)
Pipe.Dispose();
if (Process != null)
Process.Dispose();
}

public void Env(string Name, string Value)
{
start.Environment[Name] = Value;
}
}


Expand Down Expand Up @@ -219,6 +208,7 @@ internal void MessageServer(string newname)
}
var toWrite = Encoding.UTF8.GetBytes(newname);
pipe.Write(toWrite, 0, toWrite.Length);
pipe.Flush();
}
}
}