Fix Bug in AgentClient Singleton Implementation 15/15115/2
authorggori <wjdxodlf012345@khu.ac.kr>
Sat, 4 Oct 2025 05:59:57 +0000 (05:59 +0000)
committerggori <wjdxodlf012345@khu.ac.kr>
Sat, 4 Oct 2025 06:03:20 +0000 (06:03 +0000)
- Fix "__init__" to check self._initialized and determine if the one-time initialization has occured.
- If _initialized is True, all attributes have been set.
- If _initialized is False, the instance was created but not yet fully initialized by initialized_agent()

Issue-ID: AIMLFW-273
Change-Id: Ic395f9ff9d2ec22be8f93f0affb6b04d7532c4f9
Signed-off-by: ggori <wjdxodlf012345@khu.ac.kr>
trainingmgr/service/agent_service.py

index c1a76e7..c1568f6 100644 (file)
@@ -18,7 +18,6 @@
 import os
 import requests
 import dspy
-from threading import Lock
 from trainingmgr.common.trainingmgr_config import TrainingMgrConfig
 from trainingmgr.common.exceptions_utls import TMException
 from trainingmgr.schemas.agent_schema import FeatureGroupIntent
@@ -87,19 +86,18 @@ class AgentClient:
     """Encapsulates the DSPy agent. Implements Singleton pattern if desired."""
 
     _instance = None
-    _lock = Lock()
 
     def __new__(cls, *args, **kwargs):
         """Singleton: ensure only one instance is created."""
         if cls._instance is None:
-            with cls._lock:
-                if cls._instance is None:
-                    cls._instance = super().__new__(cls, *args, **kwargs)
+            cls._instance = super().__new__(cls, *args, **kwargs)
         return cls._instance
     
     def __init__(self):
-       self._agent = None
-       self._initialized = False
+        if hasattr(self, '_initialized') and self._initialized:
+            return
+        self._initialized = False
+        self._agent = None
 
     def initialize_agent(self) -> bool:
         """Initialize the DSPy agent with tools."""