Skip to content

Commit 2a9c143

Browse files
Merge pull request #173 from microsoft/psl-thread-management
feat: Agent and thread management
2 parents 9bd1828 + cfd68d4 commit 2a9c143

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

src/backend/sql_agents/helpers/comms_manager.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,3 +258,29 @@ async def async_invoke(self) -> AsyncIterable[ChatMessageContent]:
258258

259259
await asyncio.sleep(current_delay)
260260
current_delay *= self.backoff_factor
261+
262+
async def cleanup(self):
263+
"""Clean up all resources including internal threads."""
264+
try:
265+
if self.group_chat is not None:
266+
self.logger.debug("Cleaning up AgentGroupChat resources...")
267+
# Reset the group chat - this clears conversation state and deletes remote threads
268+
await self.group_chat.reset()
269+
self.logger.debug("AgentGroupChat cleanup completed successfully")
270+
271+
except Exception as e:
272+
self.logger.error("Error during cleanup: %s", str(e))
273+
274+
def __del__(self):
275+
"""Destructor to ensure cleanup if not explicitly called."""
276+
try:
277+
# Only attempt cleanup if there's an active event loop
278+
loop = asyncio.get_running_loop()
279+
if loop and not loop.is_closed():
280+
# Schedule cleanup as a task
281+
loop.create_task(self.cleanup())
282+
except RuntimeError:
283+
# No event loop running, can't clean up asynchronously
284+
self.logger.warning("No event loop available for cleanup in destructor")
285+
except Exception as e:
286+
self.logger.error("Error in destructor cleanup: %s", str(e))

0 commit comments

Comments
 (0)