fix: make AgentGraph destructor loop-safe to prevent connection leaks - #147
Closed
bhavyakeerthi3 wants to merge 1 commit into
Closed
fix: make AgentGraph destructor loop-safe to prevent connection leaks#147bhavyakeerthi3 wants to merge 1 commit into
bhavyakeerthi3 wants to merge 1 commit into
Conversation
bhavyakeerthi3
force-pushed
the
fix/asyncio-destructor-connection-leak
branch
from
March 14, 2026 18:22
75090ff to
b9de69b
Compare
Contributor
|
Thank you — you both independently found this, which is what made it credible. Fixed on
The change closes the pool only when no loop is running, and otherwise warns that it is still open rather than pretending otherwise. Writing the test surfaced that even the warning fails at interpreter shutdown once logging's handlers close — so this is containment, and the real fix is an explicit shutdown hook. Closing as resolved. Your reports are credited in the commit. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The
AgentGraphdestructor (__del__) currently callsasyncio.run(self.close_pool()). In an asynchronous environment (like a Chainlit or FastAPI server), if the agent instance is deleted while an event loop is already running,asyncio.run()raises aRuntimeError: This event loop is already running. Because the destructor fails mid-execution, the connection pool is never closed, leading to hanging database connections and potential pool exhaustion.Fix #146
Updated the destructor to be loop-aware. It now checks for a running event loop and schedules the cleanup task using
loop.create_task()instead of attempting to start a new nested loop.Important
loop.create_task()during__del__is best-effort. If the eventloop closes before the task executes, cleanup may still be skipped.
The robust long-term solution is explicit lifecycle management via a
shutdown()method, but this fix resolves the immediateRuntimeErrorand improves reliability during server reloads.Verification
Verified with a standalone validation script covering two scenarios:
close_poolis scheduled as a task when a loop is running.asyncio.runis used as a fallback when no loop is present (e.g., script exit).Files Affected
src/agent/graph.py