Skip to content

Commit 6a1346a

Browse files
committed
fix crash when stopping scene before LLM creation
1 parent ff1f300 commit 6a1346a

File tree

2 files changed

+45
-26
lines changed

2 files changed

+45
-26
lines changed

Runtime/LLM.cs

+45-25
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public LLMException(string message, int errorCode) : base(message)
2121
ErrorCode = errorCode;
2222
}
2323
}
24+
25+
public class DestroyException : Exception {}
2426
/// \endcond
2527

2628
[DefaultExecutionOrder(-1)]
@@ -83,6 +85,7 @@ public class LLM : MonoBehaviour
8385
List<StreamWrapper> streamWrappers = new List<StreamWrapper>();
8486
public LLMManager llmManager = new LLMManager();
8587
List<float> loraWeights = new List<float>();
88+
private readonly object startLock = new object();
8689

8790
/// \endcond
8891

@@ -114,6 +117,7 @@ public async void Awake()
114117
return;
115118
}
116119
await Task.Run(() => StartLLMServer(arguments));
120+
if (!started) return;
117121
if (dontDestroyOnLoad) DontDestroyOnLoad(transform.root.gameObject);
118122
if (basePrompt != "") await SetBasePrompt(basePrompt);
119123
}
@@ -322,7 +326,7 @@ private void StartLLMServer(string arguments)
322326
try
323327
{
324328
InitLib(arch);
325-
InitServer(arguments);
329+
InitService(arguments);
326330
LLMUnitySetup.Log($"Using architecture: {arch}");
327331
break;
328332
}
@@ -331,6 +335,10 @@ private void StartLLMServer(string arguments)
331335
error = e.Message;
332336
Destroy();
333337
}
338+
catch (DestroyException)
339+
{
340+
break;
341+
}
334342
catch (Exception e)
335343
{
336344
error = $"{e.GetType()}: {e.Message}";
@@ -343,7 +351,7 @@ private void StartLLMServer(string arguments)
343351
failed = true;
344352
return;
345353
}
346-
StartService();
354+
CallIfNotDestroyed(() => StartService());
347355
LLMUnitySetup.Log("LLM service created");
348356
}
349357

@@ -353,13 +361,22 @@ private void InitLib(string arch)
353361
CheckLLMStatus(false);
354362
}
355363

356-
private void InitServer(string arguments)
364+
void CallIfNotDestroyed(EmptyCallback fn)
357365
{
358-
if (debug) SetupLogging();
359-
LLMObject = llmlib.LLM_Construct(arguments);
360-
if (remote) llmlib.LLM_StartServer(LLMObject);
361-
llmlib.LLM_SetTemplate(LLMObject, chatTemplate);
362-
CheckLLMStatus(false);
366+
lock (startLock)
367+
{
368+
if (llmlib == null) throw new DestroyException();
369+
fn();
370+
}
371+
}
372+
373+
private void InitService(string arguments)
374+
{
375+
if (debug) CallIfNotDestroyed(() => SetupLogging());
376+
CallIfNotDestroyed(() => {LLMObject = llmlib.LLM_Construct(arguments);});
377+
if (remote) CallIfNotDestroyed(() => llmlib.LLM_StartServer(LLMObject));
378+
CallIfNotDestroyed(() => llmlib.LLM_SetTemplate(LLMObject, chatTemplate));
379+
CallIfNotDestroyed(() => CheckLLMStatus(false));
363380
}
364381

365382
private void StartService()
@@ -624,28 +641,31 @@ public void CancelRequest(int id_slot)
624641
/// </summary>
625642
public void Destroy()
626643
{
627-
try
644+
lock (startLock)
628645
{
629-
if (llmlib != null)
646+
try
630647
{
631-
if (LLMObject != IntPtr.Zero)
648+
if (llmlib != null)
632649
{
633-
llmlib.LLM_Stop(LLMObject);
634-
if (remote) llmlib.LLM_StopServer(LLMObject);
635-
StopLogging();
636-
llmThread?.Join();
637-
llmlib.LLM_Delete(LLMObject);
638-
LLMObject = IntPtr.Zero;
650+
if (LLMObject != IntPtr.Zero)
651+
{
652+
llmlib.LLM_Stop(LLMObject);
653+
if (remote) llmlib.LLM_StopServer(LLMObject);
654+
StopLogging();
655+
llmThread?.Join();
656+
llmlib.LLM_Delete(LLMObject);
657+
LLMObject = IntPtr.Zero;
658+
}
659+
llmlib.Destroy();
660+
llmlib = null;
639661
}
640-
llmlib.Destroy();
662+
started = false;
663+
failed = false;
664+
}
665+
catch (Exception e)
666+
{
667+
LLMUnitySetup.LogError(e.Message);
641668
}
642-
started = false;
643-
failed = false;
644-
llmlib = null;
645-
}
646-
catch (Exception e)
647-
{
648-
LLMUnitySetup.LogError(e.Message);
649669
}
650670
}
651671

Runtime/LLMLib.cs

-1
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,6 @@ static LLMLib()
281281

282282
public LLMLib(string arch)
283283
{
284-
LLMUnitySetup.Log(GetArchitecturePath(arch));
285284
libraryHandle = LibraryLoader.LoadLibrary(GetArchitecturePath(arch));
286285
if (libraryHandle == IntPtr.Zero)
287286
{

0 commit comments

Comments
 (0)