From e33f99251a314cd123c1447e85a2ab42e5014e94 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 18 Jul 2025 15:40:17 +0000 Subject: [PATCH] Fix: Add comprehensive telemetry cleanup to prevent agent termination hang - Fixed 7 missing telemetry cleanup calls in agent.py - Added cleanup to all return paths in chat() and achat() methods - Added cleanup to custom LLM guardrail validation paths - Added cleanup to reasoning content validation paths - Added cleanup to exception handlers in achat() method - Ensures proper termination of telemetry threads - Prevents hanging after agent task completion - Maintains full backward compatibility - Comprehensive testing shows all agents now terminate properly Co-authored-by: Mervin Praison --- .../praisonaiagents/agent/agent.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/praisonai-agents/praisonaiagents/agent/agent.py b/src/praisonai-agents/praisonaiagents/agent/agent.py index e154fdc60..649f5e970 100644 --- a/src/praisonai-agents/praisonaiagents/agent/agent.py +++ b/src/praisonai-agents/praisonaiagents/agent/agent.py @@ -1300,19 +1300,27 @@ def chat(self, prompt, temperature=0.2, tools=None, output_json=None, output_pyd # Apply guardrail validation for custom LLM response try: validated_response = self._apply_guardrail_with_retry(response_text, prompt, temperature, tools, task_name, task_description, task_id) + # Ensure proper cleanup of telemetry system to prevent hanging + self._cleanup_telemetry() return validated_response except Exception as e: logging.error(f"Agent {self.name}: Guardrail validation failed for custom LLM: {e}") # Rollback chat history on guardrail failure self.chat_history = self.chat_history[:chat_history_length] + # Ensure proper cleanup of telemetry system to prevent hanging + self._cleanup_telemetry() return None except Exception as e: # Rollback chat history if LLM call fails self.chat_history = self.chat_history[:chat_history_length] display_error(f"Error in LLM chat: {e}") + # Ensure proper cleanup of telemetry system to prevent hanging + self._cleanup_telemetry() return None except Exception as e: display_error(f"Error in LLM chat: {e}") + # Ensure proper cleanup of telemetry system to prevent hanging + self._cleanup_telemetry() return None else: # Use the new _build_messages helper method @@ -1396,11 +1404,15 @@ def chat(self, prompt, temperature=0.2, tools=None, output_json=None, output_pyd validated_reasoning = self._apply_guardrail_with_retry(response.choices[0].message.reasoning_content, original_prompt, temperature, tools, task_name, task_description, task_id) # Execute callback after validation self._execute_callback_and_display(original_prompt, validated_reasoning, time.time() - start_time, task_name, task_description, task_id) + # Ensure proper cleanup of telemetry system to prevent hanging + self._cleanup_telemetry() return validated_reasoning except Exception as e: logging.error(f"Agent {self.name}: Guardrail validation failed for reasoning content: {e}") # Rollback chat history on guardrail failure self.chat_history = self.chat_history[:chat_history_length] + # Ensure proper cleanup of telemetry system to prevent hanging + self._cleanup_telemetry() return None # Apply guardrail to regular response try: @@ -1849,6 +1861,8 @@ async def achat(self, prompt: str, temperature=0.2, tools=None, output_json=None if logging.getLogger().getEffectiveLevel() == logging.DEBUG: total_time = time.time() - start_time logging.debug(f"Agent.achat failed in {total_time:.2f} seconds: {str(e)}") + # Ensure proper cleanup of telemetry system to prevent hanging + self._cleanup_telemetry() return None async def _achat_completion(self, response, tools, reasoning_steps=False):