imbajin commented on code in PR #302:
URL: 
https://github.com/apache/incubator-hugegraph-ai/pull/302#discussion_r2453913954


##########
hugegraph-llm/src/hugegraph_llm/nodes/base_node.py:
##########
@@ -0,0 +1,83 @@
+#  Licensed to the Apache Software Foundation (ASF) under one or more
+#  contributor license agreements.  See the NOTICE file distributed with
+#  this work for additional information regarding copyright ownership.
+#  The ASF licenses this file to You under the Apache License, Version 2.0
+#  (the "License"); you may not use this file except in compliance with
+#  the License.  You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+
+from typing import Any, Dict, Optional
+from PyCGraph import GNode, CStatus
+from hugegraph_llm.nodes.util import init_context
+from hugegraph_llm.state.ai_state import WkFlowInput, WkFlowState
+from hugegraph_llm.utils.log import log
+
+
+class BaseNode(GNode):
+    context: Optional[WkFlowState] = None
+    wk_input: Optional[WkFlowInput] = None
+
+    def init(self):
+        return init_context(self)
+
+    def node_init(self):
+        """
+        Node initialization method, can be overridden by subclasses.
+        Returns a CStatus object indicating whether initialization succeeded.
+        """
+        if self.wk_input is None or self.context is None:
+            return CStatus(-1, "wk_input or context not initialized")
+        if self.wk_input.data_json is not None:
+            self.context.assign_from_json(self.wk_input.data_json)
+            self.wk_input.data_json = None
+        return CStatus()
+
+    def run(self):
+        """
+        Main logic for node execution, can be overridden by subclasses.
+        Returns a CStatus object indicating whether execution succeeded.
+        """
+        sts = self.node_init()
+        if sts.isErr():
+            return sts
+        if self.context is None:

Review Comment:
   ⚠️ **Important: Context锁的异常安全性问题**
   
   ```python
   self.context.lock()
   try:
       data_json = self.context.to_json()
   finally:
       self.context.unlock()
   
   try:
       res = self.operator_schedule(data_json)
   except Exception as exc:
       # ...
       
   self.context.lock()
   try:
       if res is not None and isinstance(res, dict):
           self.context.assign_from_json(res)
       # ...
   finally:
       self.context.unlock()
   ```
   
   潜在问题:
   1. 如果 `operator_schedule` 抛出异常,第二个lock块不会执行,这是否符合预期?
   2. 多次lock/unlock增加了死锁风险
   3. 没有超时机制,lock可能永久阻塞
   
   建议:
   1. 使用 `contextlib.contextmanager` 封装lock逻辑
   2. 添加lock超时机制
   3. 考虑使用RLock如果需要可重入锁
   4. 明确文档说明异常情况下的锁释放策略



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to