Skip to content

fix crash when stopping scene before LLM creation #214

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

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
70 changes: 45 additions & 25 deletions Runtime/LLM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public LLMException(string message, int errorCode) : base(message)
ErrorCode = errorCode;
}
}

public class DestroyException : Exception {}
/// \endcond

[DefaultExecutionOrder(-1)]
Expand Down Expand Up @@ -83,6 +85,7 @@ public class LLM : MonoBehaviour
List<StreamWrapper> streamWrappers = new List<StreamWrapper>();
public LLMManager llmManager = new LLMManager();
List<float> loraWeights = new List<float>();
private readonly object startLock = new object();

/// \endcond

Expand Down Expand Up @@ -114,6 +117,7 @@ public async void Awake()
return;
}
await Task.Run(() => StartLLMServer(arguments));
if (!started) return;
if (dontDestroyOnLoad) DontDestroyOnLoad(transform.root.gameObject);
if (basePrompt != "") await SetBasePrompt(basePrompt);
}
Expand Down Expand Up @@ -322,7 +326,7 @@ private void StartLLMServer(string arguments)
try
{
InitLib(arch);
InitServer(arguments);
InitService(arguments);
LLMUnitySetup.Log($"Using architecture: {arch}");
break;
}
Expand All @@ -331,6 +335,10 @@ private void StartLLMServer(string arguments)
error = e.Message;
Destroy();
}
catch (DestroyException)
{
break;
}
catch (Exception e)
{
error = $"{e.GetType()}: {e.Message}";
Expand All @@ -343,7 +351,7 @@ private void StartLLMServer(string arguments)
failed = true;
return;
}
StartService();
CallIfNotDestroyed(() => StartService());
LLMUnitySetup.Log("LLM service created");
}

Expand All @@ -353,13 +361,22 @@ private void InitLib(string arch)
CheckLLMStatus(false);
}

private void InitServer(string arguments)
void CallIfNotDestroyed(EmptyCallback fn)
{
if (debug) SetupLogging();
LLMObject = llmlib.LLM_Construct(arguments);
if (remote) llmlib.LLM_StartServer(LLMObject);
llmlib.LLM_SetTemplate(LLMObject, chatTemplate);
CheckLLMStatus(false);
lock (startLock)
{
if (llmlib == null) throw new DestroyException();
fn();
}
}

private void InitService(string arguments)
{
if (debug) CallIfNotDestroyed(() => SetupLogging());
CallIfNotDestroyed(() => {LLMObject = llmlib.LLM_Construct(arguments);});
if (remote) CallIfNotDestroyed(() => llmlib.LLM_StartServer(LLMObject));
CallIfNotDestroyed(() => llmlib.LLM_SetTemplate(LLMObject, chatTemplate));
CallIfNotDestroyed(() => CheckLLMStatus(false));
}

private void StartService()
Expand Down Expand Up @@ -624,28 +641,31 @@ public void CancelRequest(int id_slot)
/// </summary>
public void Destroy()
{
try
lock (startLock)
{
if (llmlib != null)
try
{
if (LLMObject != IntPtr.Zero)
if (llmlib != null)
{
llmlib.LLM_Stop(LLMObject);
if (remote) llmlib.LLM_StopServer(LLMObject);
StopLogging();
llmThread?.Join();
llmlib.LLM_Delete(LLMObject);
LLMObject = IntPtr.Zero;
if (LLMObject != IntPtr.Zero)
{
llmlib.LLM_Stop(LLMObject);
if (remote) llmlib.LLM_StopServer(LLMObject);
StopLogging();
llmThread?.Join();
llmlib.LLM_Delete(LLMObject);
LLMObject = IntPtr.Zero;
}
llmlib.Destroy();
llmlib = null;
}
llmlib.Destroy();
started = false;
failed = false;
}
catch (Exception e)
{
LLMUnitySetup.LogError(e.Message);
}
started = false;
failed = false;
llmlib = null;
}
catch (Exception e)
{
LLMUnitySetup.LogError(e.Message);
}
}

Expand Down
1 change: 0 additions & 1 deletion Runtime/LLMLib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,6 @@ static LLMLib()

public LLMLib(string arch)
{
LLMUnitySetup.Log(GetArchitecturePath(arch));
libraryHandle = LibraryLoader.LoadLibrary(GetArchitecturePath(arch));
if (libraryHandle == IntPtr.Zero)
{
Expand Down
Loading