@@ -21,6 +21,8 @@ public LLMException(string message, int errorCode) : base(message)
21
21
ErrorCode = errorCode ;
22
22
}
23
23
}
24
+
25
+ public class DestroyException : Exception { }
24
26
/// \endcond
25
27
26
28
[ DefaultExecutionOrder ( - 1 ) ]
@@ -83,6 +85,7 @@ public class LLM : MonoBehaviour
83
85
List < StreamWrapper > streamWrappers = new List < StreamWrapper > ( ) ;
84
86
public LLMManager llmManager = new LLMManager ( ) ;
85
87
List < float > loraWeights = new List < float > ( ) ;
88
+ private readonly object startLock = new object ( ) ;
86
89
87
90
/// \endcond
88
91
@@ -114,6 +117,7 @@ public async void Awake()
114
117
return ;
115
118
}
116
119
await Task . Run ( ( ) => StartLLMServer ( arguments ) ) ;
120
+ if ( ! started ) return ;
117
121
if ( dontDestroyOnLoad ) DontDestroyOnLoad ( transform . root . gameObject ) ;
118
122
if ( basePrompt != "" ) await SetBasePrompt ( basePrompt ) ;
119
123
}
@@ -322,7 +326,7 @@ private void StartLLMServer(string arguments)
322
326
try
323
327
{
324
328
InitLib ( arch ) ;
325
- InitServer ( arguments ) ;
329
+ InitService ( arguments ) ;
326
330
LLMUnitySetup . Log ( $ "Using architecture: { arch } ") ;
327
331
break ;
328
332
}
@@ -331,6 +335,10 @@ private void StartLLMServer(string arguments)
331
335
error = e . Message ;
332
336
Destroy ( ) ;
333
337
}
338
+ catch ( DestroyException )
339
+ {
340
+ break ;
341
+ }
334
342
catch ( Exception e )
335
343
{
336
344
error = $ "{ e . GetType ( ) } : { e . Message } ";
@@ -343,7 +351,7 @@ private void StartLLMServer(string arguments)
343
351
failed = true ;
344
352
return ;
345
353
}
346
- StartService ( ) ;
354
+ CallIfNotDestroyed ( ( ) => StartService ( ) ) ;
347
355
LLMUnitySetup . Log ( "LLM service created" ) ;
348
356
}
349
357
@@ -353,13 +361,22 @@ private void InitLib(string arch)
353
361
CheckLLMStatus ( false ) ;
354
362
}
355
363
356
- private void InitServer ( string arguments )
364
+ void CallIfNotDestroyed ( EmptyCallback fn )
357
365
{
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 ) ) ;
363
380
}
364
381
365
382
private void StartService ( )
@@ -624,28 +641,31 @@ public void CancelRequest(int id_slot)
624
641
/// </summary>
625
642
public void Destroy ( )
626
643
{
627
- try
644
+ lock ( startLock )
628
645
{
629
- if ( llmlib != null )
646
+ try
630
647
{
631
- if ( LLMObject != IntPtr . Zero )
648
+ if ( llmlib != null )
632
649
{
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 ;
639
661
}
640
- llmlib . Destroy ( ) ;
662
+ started = false ;
663
+ failed = false ;
664
+ }
665
+ catch ( Exception e )
666
+ {
667
+ LLMUnitySetup . LogError ( e . Message ) ;
641
668
}
642
- started = false ;
643
- failed = false ;
644
- llmlib = null ;
645
- }
646
- catch ( Exception e )
647
- {
648
- LLMUnitySetup . LogError ( e . Message ) ;
649
669
}
650
670
}
651
671
0 commit comments