-
Notifications
You must be signed in to change notification settings - Fork 22.4k
RuntimeError in _AsyncHttpxClientWrapper.__del__ when event loop is closed #35327
Copy link
Copy link
Open
Copy link
Labels
anthropic`langchain-anthropic` package issues & PRs`langchain-anthropic` package issues & PRsbugRelated to a bug, vulnerability, unexpected error with an existing featureRelated to a bug, vulnerability, unexpected error with an existing featureexternallangchain`langchain` package issues & PRs`langchain` package issues & PRsopenai`langchain-openai` package issues & PRs`langchain-openai` package issues & PRstrusted-contributor>= 5 merged PRs in the `langchain-ai/langchain` repo>= 5 merged PRs in the `langchain-ai/langchain` repo
Metadata
Metadata
Assignees
Labels
anthropic`langchain-anthropic` package issues & PRs`langchain-anthropic` package issues & PRsbugRelated to a bug, vulnerability, unexpected error with an existing featureRelated to a bug, vulnerability, unexpected error with an existing featureexternallangchain`langchain` package issues & PRs`langchain` package issues & PRsopenai`langchain-openai` package issues & PRs`langchain-openai` package issues & PRstrusted-contributor>= 5 merged PRs in the `langchain-ai/langchain` repo>= 5 merged PRs in the `langchain-ai/langchain` repo
Type
Fields
Give feedbackNo fields configured for Bug.
Checked other resources
Package (Required)
Related Issues / PRs
No response
Reproduction Steps / Example Code (Python)
Error Message and Stack Trace (if applicable)
Description
Description:
I found a resource cleanup issue in langchain-openai and langchain-anthropic.
The
_AsyncHttpxClientWrapper.__del__method attempts to schedule an async cleanup task usingasyncio.get_running_loop().However, if the object is garbage collected during interpreter shutdown or in a context where the event loop is already closed,
get_running_loop()raises a RuntimeError.The current code catches this in a broad
except Exception:block and suppresses it, meaningself.aclose()is never called, potentially leading to unclosed resource warnings fromhttpxProposed Fix: Safely check for the running loop using try/except RuntimeError and loop.is_running() before attempting to schedule the cleanup task.
System Info
System Information
Package Information
Other Dependencies