From ed1cfc601eb53fb4b3d0ee8f44f45be3a548c911 Mon Sep 17 00:00:00 2001 From: ggori Date: Sat, 4 Oct 2025 05:59:57 +0000 Subject: [PATCH] Fix Bug in AgentClient Singleton Implementation - 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 --- trainingmgr/service/agent_service.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/trainingmgr/service/agent_service.py b/trainingmgr/service/agent_service.py index c1a76e7..c1568f6 100644 --- a/trainingmgr/service/agent_service.py +++ b/trainingmgr/service/agent_service.py @@ -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.""" -- 2.16.6