diff --git a/simple_test.py b/simple_test.py new file mode 100644 index 000000000..522252325 --- /dev/null +++ b/simple_test.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python3 +""" +Simple test to verify the termination fix +""" +import sys +import os + +# Add the src directory to the path so we can import praisonaiagents +sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src', 'praisonai-agents')) + +print("Testing agent termination fix...") + +try: + from praisonaiagents import Agent + + # Create agent with minimal setup + agent = Agent(instructions="You are a helpful AI assistant") + + # Run the same test as in the issue + print("Running agent.start() ...") + response = agent.start("Write a movie script about a robot on Mars") + + print("Agent completed successfully!") + print(f"Response type: {type(response)}") + print(f"Response length: {len(str(response)) if response else 'None'}") + + # If we get here, the fix worked + print("SUCCESS: Program should terminate properly!") + +except Exception as e: + print(f"ERROR: Exception occurred: {e}") + import traceback + traceback.print_exc() + sys.exit(1) + +print("Test completed - program should exit now.") \ No newline at end of file diff --git a/src/praisonai-agents/praisonaiagents/agent/agent.py b/src/praisonai-agents/praisonaiagents/agent/agent.py index e9b7b1534..4b1420628 100644 --- a/src/praisonai-agents/praisonaiagents/agent/agent.py +++ b/src/praisonai-agents/praisonaiagents/agent/agent.py @@ -1929,19 +1929,44 @@ async def _achat_completion(self, response, tools, reasoning_steps=False): async def astart(self, prompt: str, **kwargs): """Async version of start method""" - return await self.achat(prompt, **kwargs) + try: + result = await self.achat(prompt, **kwargs) + return result + finally: + # Ensure proper cleanup of telemetry system to prevent hanging + self._cleanup_telemetry() def run(self): """Alias for start() method""" return self.start() + def _cleanup_telemetry(self): + """Clean up telemetry system to ensure proper program termination.""" + try: + # Import here to avoid circular imports + from ..telemetry import get_telemetry + + # Get the global telemetry instance and shut it down + telemetry = get_telemetry() + if telemetry and hasattr(telemetry, 'shutdown'): + telemetry.shutdown() + except Exception as e: + # Log error but don't fail the execution + logging.debug(f"Error cleaning up telemetry: {e}") + def start(self, prompt: str, **kwargs): """Start the agent with a prompt. This is a convenience method that wraps chat().""" - # Check if streaming is enabled and user wants streaming chunks - if self.stream and kwargs.get('stream', True): - return self._start_stream(prompt, **kwargs) - else: - return self.chat(prompt, **kwargs) + try: + # Check if streaming is enabled and user wants streaming chunks + if self.stream and kwargs.get('stream', True): + result = self._start_stream(prompt, **kwargs) + return result + else: + result = self.chat(prompt, **kwargs) + return result + finally: + # Ensure proper cleanup of telemetry system to prevent hanging + self._cleanup_telemetry() def _start_stream(self, prompt: str, **kwargs): """Generator method that yields streaming chunks from the agent.""" diff --git a/test_fix.py b/test_fix.py new file mode 100644 index 000000000..b05223aa7 --- /dev/null +++ b/test_fix.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python3 +""" +Test script to verify the termination fix works +""" +import sys +import os +import signal +import time +from threading import Timer + +# Add the src directory to the path so we can import praisonaiagents +sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src', 'praisonai-agents')) + +# Set up timeout mechanism +def timeout_handler(signum, frame): + print("ERROR: Test timed out - program is still hanging!") + sys.exit(1) + +# Set up signal handler for timeout +signal.signal(signal.SIGALRM, timeout_handler) +signal.alarm(30) # 30 second timeout + +try: + # Import here to avoid issues with path setup + from praisonaiagents import Agent + + print("Testing agent termination fix...") + + # Create agent with minimal setup + agent = Agent(instructions="You are a helpful AI assistant") + + # Run the same test as in the issue + print("Running agent.start() ...") + response = agent.start("Write a short hello world message") + + print(f"Agent completed successfully!") + print(f"Response (truncated): {str(response)[:100]}...") + + # If we get here, the fix worked + print("SUCCESS: Program terminated properly without hanging!") + +except Exception as e: + print(f"ERROR: Exception occurred: {e}") + import traceback + traceback.print_exc() + sys.exit(1) +finally: + # Cancel the alarm + signal.alarm(0) \ No newline at end of file diff --git a/test_termination_issue.py b/test_termination_issue.py new file mode 100644 index 000000000..13e609a49 --- /dev/null +++ b/test_termination_issue.py @@ -0,0 +1,30 @@ +""" +Test script to reproduce the termination issue +""" +import sys +import os + +# Add the src directory to the path so we can import praisonaiagents +sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src', 'praisonai-agents')) + +from praisonaiagents import Agent + +def test_termination(): + """Test that the agent terminates properly after execution""" + print("Testing agent termination...") + + agent = Agent(instructions="You are a helpful AI assistant") + response = agent.start("Write a short hello world message") + + print(f"Agent response: {response}") + print("Agent execution completed. Testing if program terminates...") + + # If this completes without hanging, the fix works + return True + +if __name__ == "__main__": + result = test_termination() + if result: + print("SUCCESS: Program terminated properly") + else: + print("FAILURE: Program did not terminate") \ No newline at end of file