-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix: Resolve agent termination issue by properly cleaning up telemetry system #990
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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.") |
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -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 | ||||||||||||||
|
Comment on lines
+19
to
+21
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The use of
Suggested change
|
||||||||||||||
|
|
||||||||||||||
| 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) | ||||||||||||||
|
Comment on lines
+48
to
+49
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The cleanup method correctly resolves the agent termination issue for single-run scenarios. However, it introduces a potential issue in applications that execute multiple agents sequentially within the same process.
get_telemetry()returns a global singleton instance of the telemetry collector. This method callsshutdown()on that singleton instance. Once shut down, the telemetry instance cannot be reused, and subsequent calls to tracking methods will likely be ignored. Therefore, if another agent is started in the same process, its activities will not be recorded by telemetry.A complete solution would likely involve modifying
get_telemetry()to re-initialize the telemetry system if the existing instance has been shut down. Since that's outside the changed files in this PR, this is a known limitation of the current fix. For many use cases where a script runs a single agent and exits, this fix is perfectly fine.