Skip to content

RuntimeError in _AsyncHttpxClientWrapper.__del__ when event loop is closed #35327

@Shivangisharma4

Description

Checked other resources

  • This is a bug, not a usage question.
  • I added a clear and descriptive title that summarizes this issue.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).
  • This is not related to the langchain-community package.
  • I posted a self-contained, minimal, reproducible example. A maintainer can copy it and run it AS IS.

Package (Required)

  • langchain
  • langchain-openai
  • langchain-anthropic
  • langchain-classic
  • langchain-core
  • langchain-model-profiles
  • langchain-tests
  • langchain-text-splitters
  • langchain-chroma
  • langchain-deepseek
  • langchain-exa
  • langchain-fireworks
  • langchain-groq
  • langchain-huggingface
  • langchain-mistralai
  • langchain-nomic
  • langchain-ollama
  • langchain-openrouter
  • langchain-perplexity
  • langchain-qdrant
  • langchain-xai
  • Other / not sure / general

Related Issues / PRs

No response

Reproduction Steps / Example Code (Python)

import asyncio
import gc
from langchain_openai.chat_models._client_utils import _AsyncHttpxClientWrapper

async def main():
    client = _AsyncHttpxClientWrapper(base_url="http://test", timeout=10)
    # used...
    pass

if __name__ == "__main__":
    asyncio.run(main()) 
    # Logic: loop is closed here
    gc.collect() # Trigger __del__ -> RuntimeError: no running event loop

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 using asyncio.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, meaning self.aclose() is never called, potentially leading to unclosed resource warnings from httpx

Proposed 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

OS: Darwin
OS Version: Darwin Kernel Version 25.2.0: Tue Nov 18 21:08:48 PST 2025; root:xnu-12377.61.12~1/RELEASE_ARM64_T8132
Python Version: 3.13.0rc3 (v3.13.0rc3:fae84c70fbd, Oct 1 2024, 00:10:10) [Clang 15.0.0 (clang-1500.3.9.4)]

Package Information

langchain_core: 1.2.13
langsmith: 0.7.4
langgraph_sdk: 0.3.6

Other Dependencies

httpx: 0.28.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    anthropic`langchain-anthropic` package issues & PRsbugRelated to a bug, vulnerability, unexpected error with an existing featureexternallangchain`langchain` package issues & PRsopenai`langchain-openai` package issues & PRstrusted-contributor>= 5 merged PRs in the `langchain-ai/langchain` repo

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions