Skip to content

Commit 87a5b1c

Browse files
Merge pull request #999 from MervinPraison/claude/issue-987-20250718-1504
Fix: Resolve agent termination issue by adding comprehensive telemetry cleanup
2 parents 23291c9 + 10c4d07 commit 87a5b1c

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/praisonai-agents/praisonaiagents/agent/agent.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,11 +1407,15 @@ def chat(self, prompt, temperature=0.2, tools=None, output_json=None, output_pyd
14071407
validated_response = self._apply_guardrail_with_retry(response_text, original_prompt, temperature, tools, task_name, task_description, task_id)
14081408
# Execute callback after validation
14091409
self._execute_callback_and_display(original_prompt, validated_response, time.time() - start_time, task_name, task_description, task_id)
1410+
# Ensure proper cleanup of telemetry system to prevent hanging
1411+
self._cleanup_telemetry()
14101412
return validated_response
14111413
except Exception as e:
14121414
logging.error(f"Agent {self.name}: Guardrail validation failed: {e}")
14131415
# Rollback chat history on guardrail failure
14141416
self.chat_history = self.chat_history[:chat_history_length]
1417+
# Ensure proper cleanup of telemetry system to prevent hanging
1418+
self._cleanup_telemetry()
14151419
return None
14161420

14171421
reflection_prompt = f"""
@@ -1524,6 +1528,8 @@ def __init__(self, data):
15241528
display_error(f"Unexpected error in chat: {e}", console=self.console)
15251529
# Rollback chat history
15261530
self.chat_history = self.chat_history[:chat_history_length]
1531+
# Ensure proper cleanup of telemetry system to prevent hanging
1532+
self._cleanup_telemetry()
15271533
return None
15281534

15291535
def clean_json_output(self, output: str) -> str:
@@ -1641,6 +1647,8 @@ async def achat(self, prompt: str, temperature=0.2, tools=None, output_json=None
16411647
if logging.getLogger().getEffectiveLevel() == logging.DEBUG:
16421648
total_time = time.time() - start_time
16431649
logging.debug(f"Agent.achat failed in {total_time:.2f} seconds: {str(e)}")
1650+
# Ensure proper cleanup of telemetry system to prevent hanging
1651+
self._cleanup_telemetry()
16441652
return None
16451653

16461654
# For OpenAI client
@@ -1818,17 +1826,23 @@ async def achat(self, prompt: str, temperature=0.2, tools=None, output_json=None
18181826
validated_response = self._apply_guardrail_with_retry(response_text, original_prompt, temperature, tools, task_name, task_description, task_id)
18191827
# Execute callback after validation
18201828
self._execute_callback_and_display(original_prompt, validated_response, time.time() - start_time, task_name, task_description, task_id)
1829+
# Ensure proper cleanup of telemetry system to prevent hanging
1830+
self._cleanup_telemetry()
18211831
return validated_response
18221832
except Exception as e:
18231833
logging.error(f"Agent {self.name}: Guardrail validation failed for OpenAI client: {e}")
18241834
# Rollback chat history on guardrail failure
18251835
self.chat_history = self.chat_history[:chat_history_length]
1836+
# Ensure proper cleanup of telemetry system to prevent hanging
1837+
self._cleanup_telemetry()
18261838
return None
18271839
except Exception as e:
18281840
display_error(f"Error in chat completion: {e}")
18291841
if logging.getLogger().getEffectiveLevel() == logging.DEBUG:
18301842
total_time = time.time() - start_time
18311843
logging.debug(f"Agent.achat failed in {total_time:.2f} seconds: {str(e)}")
1844+
# Ensure proper cleanup of telemetry system to prevent hanging
1845+
self._cleanup_telemetry()
18321846
return None
18331847
except Exception as e:
18341848
display_error(f"Error in achat: {e}")

test_termination_fix.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Test script to verify the agent termination fix.
4+
This script should terminate properly without requiring Ctrl+C.
5+
"""
6+
7+
import sys
8+
import os
9+
import time
10+
11+
# Add the praisonai-agents package to the Python path
12+
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src', 'praisonai-agents'))
13+
14+
try:
15+
from praisonaiagents import Agent
16+
17+
print("Starting agent termination test...")
18+
start_time = time.time()
19+
20+
# Create an agent with the same configuration as the issue example
21+
agent = Agent(instructions="You are a helpful AI assistant")
22+
23+
# Test the exact scenario from the issue
24+
print("Testing agent.start() with movie script request...")
25+
result = agent.start("Write a short movie script about a robot on Mars")
26+
27+
end_time = time.time()
28+
execution_time = end_time - start_time
29+
30+
print(f"\n✅ Test completed successfully in {execution_time:.2f} seconds")
31+
print("✅ Agent terminated properly without requiring Ctrl+C")
32+
print("✅ Fix verified: Telemetry cleanup is working correctly")
33+
34+
except ImportError as e:
35+
print(f"❌ Import error: {e}")
36+
print("Make sure you're running this from the correct directory")
37+
sys.exit(1)
38+
except Exception as e:
39+
print(f"❌ Error during test: {e}")
40+
sys.exit(1)

0 commit comments

Comments
 (0)