Skip to content

Commit df3042b

Browse files
Fix: Add telemetry cleanup to execute() and aexecute() methods to prevent hanging
- Add try-finally blocks to execute() and aexecute() methods - Ensures telemetry cleanup happens for all public entry points - Prevents programs from hanging due to PostHog background threads - Maintains backward compatibility - Addresses critical gaps identified in PR reviews Co-authored-by: Mervin Praison <MervinPraison@users.noreply.github.com>
1 parent 23291c9 commit df3042b

File tree

1 file changed

+26
-18
lines changed
  • src/praisonai-agents/praisonaiagents/agent

1 file changed

+26
-18
lines changed

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

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2147,27 +2147,35 @@ def _openai_stream(self, prompt, temperature=0.2, tools=None, output_json=None,
21472147

21482148
def execute(self, task, context=None):
21492149
"""Execute a task synchronously - backward compatibility method"""
2150-
if hasattr(task, 'description'):
2151-
prompt = task.description
2152-
elif isinstance(task, str):
2153-
prompt = task
2154-
else:
2155-
prompt = str(task)
2156-
return self.chat(prompt)
2150+
try:
2151+
if hasattr(task, 'description'):
2152+
prompt = task.description
2153+
elif isinstance(task, str):
2154+
prompt = task
2155+
else:
2156+
prompt = str(task)
2157+
return self.chat(prompt)
2158+
finally:
2159+
# Ensure proper cleanup of telemetry system to prevent hanging
2160+
self._cleanup_telemetry()
21572161

21582162
async def aexecute(self, task, context=None):
21592163
"""Execute a task asynchronously - backward compatibility method"""
2160-
if hasattr(task, 'description'):
2161-
prompt = task.description
2162-
elif isinstance(task, str):
2163-
prompt = task
2164-
else:
2165-
prompt = str(task)
2166-
# Extract task info if available
2167-
task_name = getattr(task, 'name', None)
2168-
task_description = getattr(task, 'description', None)
2169-
task_id = getattr(task, 'id', None)
2170-
return await self.achat(prompt, task_name=task_name, task_description=task_description, task_id=task_id)
2164+
try:
2165+
if hasattr(task, 'description'):
2166+
prompt = task.description
2167+
elif isinstance(task, str):
2168+
prompt = task
2169+
else:
2170+
prompt = str(task)
2171+
# Extract task info if available
2172+
task_name = getattr(task, 'name', None)
2173+
task_description = getattr(task, 'description', None)
2174+
task_id = getattr(task, 'id', None)
2175+
return await self.achat(prompt, task_name=task_name, task_description=task_description, task_id=task_id)
2176+
finally:
2177+
# Ensure proper cleanup of telemetry system to prevent hanging
2178+
self._cleanup_telemetry()
21712179

21722180
async def execute_tool_async(self, function_name: str, arguments: Dict[str, Any]) -> Any:
21732181
"""Async version of execute_tool"""

0 commit comments

Comments
 (0)