Reload module after run session.initialize()?

Describe the bug

My variables are implemented using the singleton pattern. However, before and after using session.initialize(), the modules are reloaded, because I observe that the instances are re-initialized; they are using new IDs.

To Reproduce
Steps to reproduce the behavior:

  1. Define global variable: (singleton pattern)
def initialize_config():
    global error_handler, error_registry, file
    # global error_handler, error_registry, file, session_memory
    # create an instance of handling errors (vietnamese)
    error_handler = ErrorHandler(language='vi')
    # create an instance of error registry (vietnamese)
    error_registry = ErrorRegistry(language='vi')
  1. Implement session
    def __init__(self):
        # initialize session and client objects
        self.session: Optional[ClientSession] = None
        self.read_transport = None
        self.write_transport = None
        self.exit_stack = AsyncExitStack()
        logger.info("MCPClient initialized successfully")
async def connect_to_server(self, server_script_path: str):
        ....................
        server_params = StdioServerParameters(
            command=sys.executable,
            args=[server_script_path],
            env=os.environ.copy()
        )
        print(f"{os.getpid()}")
        stdio_transport = await self.exit_stack.enter_async_context(stdio_client(server_params))
        self.read_transport, self.write_transport = stdio_transport
        print(f"{os.getpid()}")
        self.session = await self.exit_stack.enter_async_context(
            ClientSession(self.read_transport, self.write_transport)
        )        
        print(f"{os.getpid()}")
        await self.session.initialize()
        print(f"{os.getpid()}")
        HERE
        ......................

I checked the PIDs (Process IDs) at various stages, and they had the same value.

Expected behavior

I hope that after the session is initialized, the variable values will not be re-initialized.

Log detail

...............
2025-06-05 10:06:39.059 | INFO     | src.llm_idp.error_handling.errors:__init__:50 - Initialized ErrorHandler with language: vi
2025-06-05 10:06:39.059 | DEBUG    | src.llm_idp.error_handling.errors:get_instance:136 - Calling ErrorHandler get_instance() - current instance: <src.llm_idp.error_handling.errors.ErrorHandler object at 0x7ff653bb5910>
2025-06-05 10:06:39.059 | INFO     | src.llm_idp.error_handling.error_registry:__init__:51 - Initialized ErrorRegistry with language: vi
2025-06-05 10:06:39.059 | DEBUG    | src.llm_idp.error_handling.error_registry:get_instance:100 - Calling ErrorRegistry get_instance() - current instance: <src.llm_idp.error_handling.error_registry.ErrorRegistry object at 0x7ff653d2c810>
2025-06-05 10:06:39.251 | DEBUG    | src.llm_idp.error_handling.errors:get_instance:136 - Calling ErrorHandler get_instance() - current instance: <src.llm_idp.error_handling.errors.ErrorHandler object at 0x7ff653bb5910>
2025-06-05 10:06:39.251 | DEBUG    | src.llm_idp.error_handling.error_registry:get_instance:100 - Calling ErrorRegistry get_instance() - current instance: <src.llm_idp.error_handling.error_registry.ErrorRegistry object at 0x7ff653d2c810>
2025-06-05 10:06:39.305 | DEBUG    | src.llm_idp.error_handling.errors:get_instance:136 - Calling ErrorHandler get_instance() - current instance: <src.llm_idp.error_handling.errors.ErrorHandler object at 0x7ff653bb5910>
2025-06-05 10:06:39.305 | DEBUG    | src.llm_idp.error_handling.error_registry:get_instance:100 - Calling ErrorRegistry get_instance() - current instance: <src.llm_idp.error_handling.error_registry.ErrorRegistry object at 0x7ff653d2c810>
2025-06-05 10:06:44.669 | DEBUG    | src.llm_idp.error_handling.errors:get_instance:136 - Calling ErrorHandler get_instance() - current instance: <src.llm_idp.error_handling.errors.ErrorHandler object at 0x7ff653bb5910>
2025-06-05 10:06:44.669 | DEBUG    | src.llm_idp.error_handling.error_registry:get_instance:100 - Calling ErrorRegistry get_instance() - current instance: <src.llm_idp.error_handling.error_registry.ErrorRegistry object at 0x7ff653d2c810>
2025-06-05 10:06:44.670 | DEBUG    | src.llm_idp.error_handling.errors:get_instance:136 - Calling ErrorHandler get_instance() - current instance: <src.llm_idp.error_handling.errors.ErrorHandler object at 0x7ff653bb5910>
2025-06-05 10:06:44.670 | DEBUG    | src.llm_idp.error_handling.error_registry:get_instance:100 - Calling ErrorRegistry get_instance() - current instance: <src.llm_idp.error_handling.error_registry.ErrorRegistry object at 0x7ff653d2c810>
2025-06-05 10:06:44.670 | DEBUG    | src.llm_idp.error_handling.errors:get_instance:136 - Calling ErrorHandler get_instance() - current instance: <src.llm_idp.error_handling.errors.ErrorHandler object at 0x7ff653bb5910>
2025-06-05 10:06:44.670 | DEBUG    | src.llm_idp.error_handling.error_registry:get_instance:100 - Calling ErrorRegistry get_instance() - current instance: <src.llm_idp.error_handling.error_registry.ErrorRegistry object at 0x7ff653d2c810>
2025-06-05 10:06:46.952 | DEBUG    | src.llm_idp.error_handling.errors:get_instance:136 - Calling ErrorHandler get_instance() - current instance: <src.llm_idp.error_handling.errors.ErrorHandler object at 0x7ff653bb5910>
2025-06-05 10:06:46.952 | DEBUG    | src.llm_idp.error_handling.error_registry:get_instance:100 - Calling ErrorRegistry get_instance() - current instance: <src.llm_idp.error_handling.error_registry.ErrorRegistry object at 0x7ff653d2c810>
2025-06-05 10:06:46.976 | DEBUG    | src.llm_idp.error_handling.errors:get_instance:136 - Calling ErrorHandler get_instance() - current instance: <src.llm_idp.error_handling.errors.ErrorHandler object at 0x7ff653bb5910>
2025-06-05 10:06:46.976 | DEBUG    | src.llm_idp.error_handling.error_registry:get_instance:100 - Calling ErrorRegistry get_instance() - current instance: <src.llm_idp.error_handling.error_registry.ErrorRegistry object at 0x7ff653d2c810>
2025-06-05 10:06:46.976 | DEBUG    | src.llm_idp.error_handling.errors:get_instance:136 - Calling ErrorHandler get_instance() - current instance: <src.llm_idp.error_handling.errors.ErrorHandler object at 0x7ff653bb5910>
2025-06-05 10:06:46.976 | DEBUG    | src.llm_idp.error_handling.error_registry:get_instance:100 - Calling ErrorRegistry get_instance() - current instance: <src.llm_idp.error_handling.error_registry.ErrorRegistry object at 0x7ff653d2c810>
2025-06-05 10:06:46.977 | INFO     | src.llm_idp.multi_agent.expert_graph.rag_graph_definition:_build_graph:28 - [START] compile expert graph
2025-06-05 10:06:46.980 | INFO     | src.llm_idp.multi_agent.expert_graph.rag_graph_definition:_build_graph:39 - [END] compile expert graph
2025-06-05 10:06:46.980 | DEBUG    | src.llm_idp.error_handling.errors:get_instance:136 - Calling ErrorHandler get_instance() - current instance: <src.llm_idp.error_handling.errors.ErrorHandler object at 0x7ff653bb5910>
2025-06-05 10:06:46.980 | DEBUG    | src.llm_idp.error_handling.error_registry:get_instance:100 - Calling ErrorRegistry get_instance() - current instance: <src.llm_idp.error_handling.error_registry.ErrorRegistry object at 0x7ff653d2c810>
2025-06-05 10:06:47.029 | DEBUG    | src.llm_idp.error_handling.errors:get_instance:136 - Calling ErrorHandler get_instance() - current instance: <src.llm_idp.error_handling.errors.ErrorHandler object at 0x7ff653bb5910>
2025-06-05 10:06:47.029 | DEBUG    | src.llm_idp.error_handling.error_registry:get_instance:100 - Calling ErrorRegistry get_instance() - current instance: <src.llm_idp.error_handling.error_registry.ErrorRegistry object at 0x7ff653d2c810>
2025-06-05 10:06:47.580 | DEBUG    | src.llm_idp.error_handling.errors:get_instance:136 - Calling ErrorHandler get_instance() - current instance: <src.llm_idp.error_handling.errors.ErrorHandler object at 0x7ff653bb5910>
2025-06-05 10:06:47.580 | DEBUG    | src.llm_idp.error_handling.error_registry:get_instance:100 - Calling ErrorRegistry get_instance() - current instance: <src.llm_idp.error_handling.error_registry.ErrorRegistry object at 0x7ff653d2c810>
2025-06-05 10:06:47.580 | DEBUG    | src.llm_idp.error_handling.errors:get_instance:136 - Calling ErrorHandler get_instance() - current instance: <src.llm_idp.error_handling.errors.ErrorHandler object at 0x7ff653bb5910>
2025-06-05 10:06:47.580 | DEBUG    | src.llm_idp.error_handling.error_registry:get_instance:100 - Calling ErrorRegistry get_instance() - current instance: <src.llm_idp.error_handling.error_registry.ErrorRegistry object at 0x7ff653d2c810>
2025-06-05 10:06:47.580 | DEBUG    | src.llm_idp.error_handling.errors:get_instance:136 - Calling ErrorHandler get_instance() - current instance: <src.llm_idp.error_handling.errors.ErrorHandler object at 0x7ff653bb5910>
2025-06-05 10:06:47.580 | DEBUG    | src.llm_idp.error_handling.error_registry:get_instance:100 - Calling ErrorRegistry get_instance() - current instance: <src.llm_idp.error_handling.error_registry.ErrorRegistry object at 0x7ff653d2c810>
2025-06-05 10:06:47.580 | DEBUG    | src.llm_idp.error_handling.errors:get_instance:136 - Calling ErrorHandler get_instance() - current instance: <src.llm_idp.error_handling.errors.ErrorHandler object at 0x7ff653bb5910>
2025-06-05 10:06:47.580 | DEBUG    | src.llm_idp.error_handling.error_registry:get_instance:100 - Calling ErrorRegistry get_instance() - current instance: <src.llm_idp.error_handling.error_registry.ErrorRegistry object at 0x7ff653d2c810>
2025-06-05 10:06:47.580 | INFO     | src.llm_idp.multi_agent.supervisor_agent.supervisor_graph_definition:_build_graph:30 - [START] compile supervisor graph
2025-06-05 10:06:47.586 | INFO     | src.llm_idp.multi_agent.supervisor_agent.supervisor_graph_definition:_build_graph:41 - [END] compile supervisor graph
2025-06-05 10:06:47.587 | DEBUG    | src.llm_idp.error_handling.errors:get_instance:136 - Calling ErrorHandler get_instance() - current instance: <src.llm_idp.error_handling.errors.ErrorHandler object at 0x7ff653bb5910>
2025-06-05 10:06:47.587 | DEBUG    | src.llm_idp.error_handling.errors:get_instance:136 - Calling ErrorHandler get_instance() - current instance: <src.llm_idp.error_handling.errors.ErrorHandler object at 0x7ff653bb5910>
2025-06-05 10:06:47.588 | DEBUG    | src.llm_idp.error_handling.errors:get_instance:136 - Calling ErrorHandler get_instance() - current instance: <src.llm_idp.error_handling.errors.ErrorHandler object at 0x7ff653bb5910>
URI http://localhost:19530
INFO:     Started server process [52459]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:9005 (Press CTRL+C to quit)
2025-06-05 10:07:11.939 | INFO     | src.llm_idp.api.routers.chat:create_item_graph:152 - [Start] Chat
2025-06-05 10:07:11.939 | INFO     | src.llm_idp.api.routers.chat:create_item_graph:154 - data input: {'bot_name': 'idp', 'session': 'session_123', 'user': 'user_123', 'message': "Tạo 1 action 'Build jenkins' và thực hiện chạy action đó trên hệ thống 'ClickOps'"}
2025-06-05 10:07:13.146 | INFO     | src.llm_idp.multi_agent.shared.utils:stream_graph_updates:18 - start conversation
2025-06-05 10:07:13.148 | INFO     | src.llm_idp.multi_agent.shared.utils:stream_graph_updates:29 - Assistant: ()...
2025-06-05 10:07:13.148 | INFO     | src.llm_idp.multi_agent.shared.utils:stream_graph_updates:30 - ----
2025-06-05 10:07:13.148 | INFO     | src.llm_idp.multi_agent.shared.utils:stream_graph_updates:29 - Assistant: {'messages': [HumanMessage(content="Tạo 1 action '...
2025-06-05 10:07:13.148 | INFO     | src.llm_idp.multi_agent.shared.utils:stream_graph_updates:30 - ----
2025-06-05 10:07:13.149 | INFO     | src.llm_idp.multi_agent.supervisor_agent.supervisor_node:supervisor_node:37 - route to supervisor agent
2025-06-05 10:07:13.149 | INFO     | src.llm_idp.multi_agent.supervisor_agent.supervisor_node:supervisor_node:38 - state information: state key: dict_keys(['messages']),
 state["messages"]: [HumanMessage(content="Tạo 1 action 'Build jenkins' và thực hiện chạy action đó trên hệ thống 'ClickOps'", additional_kwargs={}, response_metadata={}, id='a0fa599e-7c4b-4cc8-bf36-1a239826ccab')]...
2025-06-05 10:07:13.807 | INFO     | src.llm_idp.multi_agent.shared.measure_time:wrapper:22 - Node supervisor_node run in: 0.6580 second
2025-06-05 10:07:13.808 | INFO     | src.llm_idp.multi_agent.shared.utils:stream_graph_updates:29 - Assistant: ()...
2025-06-05 10:07:13.808 | INFO     | src.llm_idp.multi_agent.shared.utils:stream_graph_updates:30 - ----
2025-06-05 10:07:13.808 | INFO     | src.llm_idp.multi_agent.shared.utils:stream_graph_updates:29 - Assistant: {'messages': [HumanMessage(content="Tạo 1 action '...
2025-06-05 10:07:13.808 | INFO     | src.llm_idp.multi_agent.shared.utils:stream_graph_updates:30 - ----
2025-06-05 10:07:13.808 | INFO     | src.llm_idp.multi_agent.execution_graph.execution_node:execution_node:36 - route to execution agent
2025-06-05 10:07:13.808 | INFO     | src.llm_idp.multi_agent.execution_graph.execution_node:execution_node:37 - state information: state key: dict_keys(['messages', 'next']),
 state["messages"]: [HumanMessage(content="Tạo 1 action 'Build jenkins' và thực hiện chạy action đó trên hệ thống 'ClickOps'", additional_kwargs={}, response_metadata={}, id='a0fa599e-7c4b-4cc8-bf36-1a239826ccab')]...
 before: 52459
2025-06-05 10:07:13.856 | INFO     | src.llm_idp.multi_agent.execution_graph.mcp_clients.jenkins_client:__init__:59 - MCPClient initialized successfully
2025-06-05 10:07:13.856 | INFO     | src.llm_idp.multi_agent.execution_graph.mcp_clients.jenkins_client:connect_to_server:73 - init stdio server parameters
2025-06-05 10:07:13.856 | INFO     | src.llm_idp.multi_agent.execution_graph.mcp_clients.jenkins_client:connect_to_server:81 - Attempting to connect to MCP server: /media/manhdt4/sda1/llm-idp/src/llm_idp/multi_agent/execution_graph/mcp_servers/jenkins_server.py...
2025-06-05 10:07:13.886 | INFO     | src.llm_idp.multi_agent.execution_graph.mcp_clients.jenkins_client:connect_to_server:89 - Stdio transport established.
 after: 52459
52459
 after: 52459
2025-06-05 10:07:15.527 | INFO     | src.llm_idp.error_handling.errors:__init__:50 - Initialized ErrorHandler with language: vi
2025-06-05 10:07:15.527 | DEBUG    | src.llm_idp.error_handling.errors:get_instance:136 - Calling ErrorHandler get_instance() - current instance: <src.llm_idp.error_handling.errors.ErrorHandler object at 0x7f6dbf8f9bd0>
2025-06-05 10:07:15.527 | INFO     | src.llm_idp.error_handling.error_registry:__init__:51 - Initialized ErrorRegistry with language: vi
2025-06-05 10:07:15.527 | DEBUG    | src.llm_idp.error_handling.error_registry:get_instance:100 - Calling ErrorRegistry get_instance() - current instance: <src.llm_idp.error_handling.error_registry.ErrorRegistry object at 0x7f6dbf1be750>
2025-06-05 10:07:15.852 | DEBUG    | src.llm_idp.error_handling.errors:get_instance:136 - Calling ErrorHandler get_instance() - current instance: <src.llm_idp.error_handling.errors.ErrorHandler object at 0x7f6dbf8f9bd0>
2025-06-05 10:07:15.852 | DEBUG    | src.llm_idp.error_handling.error_registry:get_instance:100 - Calling ErrorRegistry get_instance() - current instance: <src.llm_idp.error_handling.error_registry.ErrorRegistry object at 0x7f6dbf1be750>
2025-06-05 10:07:15.922 | DEBUG    | src.llm_idp.error_handling.errors:get_instance:136 - Calling ErrorHandler get_instance() - current instance: <src.llm_idp.error_handling.errors.ErrorHandler object at 0x7f6dbf8f9bd0>
2025-06-05 10:07:15.922 | DEBUG    | src.llm_idp.error_handling.error_registry:get_instance:100 - Calling ErrorRegistry get_instance() - current instance: <src.llm_idp.error_handling.error_registry.ErrorRegistry object at 0x7f6dbf1be750>
2025-06-05 10:07:18.330 | DEBUG    | src.llm_idp.error_handling.errors:get_instance:136 - Calling ErrorHandler get_instance() - current instance: <src.llm_idp.error_handling.errors.ErrorHandler object at 0x7f6dbf8f9bd0>
2025-06-05 10:07:18.330 | DEBUG    | src.llm_idp.error_handling.error_registry:get_instance:100 - Calling ErrorRegistry get_instance() - current instance: <src.llm_idp.error_handling.error_registry.ErrorRegistry object at 0x7f6dbf1be750>
2025-06-05 10:07:18.330 | DEBUG    | src.llm_idp.error_handling.errors:get_instance:136 - Calling ErrorHandler get_instance() - current instance: <src.llm_idp.error_handling.errors.ErrorHandler object at 0x7f6dbf8f9bd0>
...............

Desktop (please complete the following information):

  • OS: ubuntu 20.04
  • Lib:
    langchain 0.3.15
    langchain-community 0.3.5
    langchain-core 0.3.45
    langchain-experimental 0.3.4
    langchain-mcp-adapters 0.1.1
    langchain-ollama 0.2.3
    langchain-openai 0.3.9
    langchain-text-splitters 0.3.5
    langdetect 1.0.9
    langgraph 0.3.14
    langgraph-checkpoint 2.0.21
    langgraph-prebuilt 0.1.3
    langgraph-sdk 0.1.57
    langsmith 0.1.147