Skip to content

Commit

Permalink
await separately in StartServer
Browse files Browse the repository at this point in the history
  • Loading branch information
amakropoulos committed Mar 7, 2024
1 parent a7b6aa7 commit 7e9c931
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions Runtime/LLM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,21 +301,30 @@ private async Task StartLLMServer()

string GPUArgument = numGPULayers <= 0 ? "" : $" -ngl {numGPULayers}";
LLMUnitySetup.makeExecutable(server);
await RunAndWait(server, arguments + GPUArgument);

RunServerCommand(server, arguments + GPUArgument);
if (asynchronousStartup) await WaitOneASync(serverBlock, TimeSpan.FromSeconds(60));
else serverBlock.WaitOne(60000);

if (process.HasExited && mmapCrash)
{
Debug.Log("Mmap error, fallback to no mmap use");
serverBlock.Reset();
arguments += " --no-mmap";
await RunAndWait(server, arguments + GPUArgument);

RunServerCommand(server, arguments + GPUArgument);
if (asynchronousStartup) await WaitOneASync(serverBlock, TimeSpan.FromSeconds(60));
else serverBlock.WaitOne(60000);
}

if (process.HasExited && numGPULayers > 0)
{
Debug.Log("GPU failed, fallback to CPU");
serverBlock.Reset();
await RunAndWait(server, arguments);

RunServerCommand(server, arguments);
if (asynchronousStartup) await WaitOneASync(serverBlock, TimeSpan.FromSeconds(60));
else serverBlock.WaitOne(60000);
}

if (process.HasExited) throw new Exception("Server could not be started!");
Expand All @@ -342,13 +351,6 @@ public void OnDestroy()
StopProcess();
}

private async Task RunAndWait(string exe, string args, int seconds = 60)
{
RunServerCommand(exe, args);
if (asynchronousStartup) await WaitOneASync(serverBlock, TimeSpan.FromSeconds(seconds));
else serverBlock.WaitOne(seconds * 1000);
}

/// Wrapper from https://stackoverflow.com/a/18766131
private static Task WaitOneASync(WaitHandle handle, TimeSpan timeout)
{
Expand Down

0 comments on commit 7e9c931

Please sign in to comment.