Skip to content

fix: remove asyncio.run() from __del__ to prevent production crashes - #156

Closed
bleedblack1 wants to merge 1 commit into
reactome:mainfrom
bleedblack1:fix/asyncio-run-crash
Closed

fix: remove asyncio.run() from __del__ to prevent production crashes#156
bleedblack1 wants to merge 1 commit into
reactome:mainfrom
bleedblack1:fix/asyncio-run-crash

Conversation

@bleedblack1

Copy link
Copy Markdown

Fixes #154

Problem

The AgentGraph.__del__ method crashes in production:

def __del__(self) -> None:
    if self.pool:
        asyncio.run(self.close_pool())  #  RuntimeError!

Why it crashes: asyncio.run() cannot be called when an event loop is already running, which is always the case in Chainlit/uvicorn/FastAPI.

Secondary issue: LANGGRAPH_DB_URI is constructed at module import with potentially None values, creating malformed URIs.

Solution

Change Purpose
Remove del Eliminates the crash
Add aenter/aexit Proper async context manager pattern
Add close() method Explicit, idempotent cleanup
Add _build_langgraph_db_uri() Lazy URI construction with validation
Add is_initialized/is_closed State inspection properties
Add create_agent_graph() Convenience factory function

Related Issues

  • Fixes the asyncio.run() crash bug
  • Addresses malformed URI construction
  • Improves resource management patterns

@adamjohnwright

Copy link
Copy Markdown
Contributor

Thank you — you both independently found this, which is what made it credible.

Fixed on main in #173, though not with either approach as proposed:

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: asyncio.run() in __del__ causes RuntimeError crash in production

2 participants