Skip to content

Comments

fix(core): prevent memory leak from lru_cache on bound methods#35296

Open
yaowubarbara wants to merge 1 commit intolangchain-ai:masterfrom
yaowubarbara:fix/lru-cache-memory-leak-30667
Open

fix(core): prevent memory leak from lru_cache on bound methods#35296
yaowubarbara wants to merge 1 commit intolangchain-ai:masterfrom
yaowubarbara:fix/lru-cache-memory-leak-30667

Conversation

@yaowubarbara
Copy link
Contributor

Summary

  • @lru_cache on get_function_nonlocals holds strong references to bound method arguments, preventing the associated objects from being garbage collected
  • Split the function into a public entry point that skips the cache for bound methods (detected via inspect.ismethod) and a cached path for regular functions/lambdas
  • Regular functions still benefit from lru_cache performance, bound methods just compute directly (they never had cache hits anyway since each obj.method creates a new method object)

Fixes #30667

Test plan

  • Added test_bound_method_no_memory_leak — verifies get_function_nonlocals(obj.call) does not prevent obj from being garbage collected
  • Added test_bound_method_in_runnable_sequence_no_leak — verifies the full (thing.call | RunnablePassthrough()).invoke({}) scenario does not leak
  • Existing test_nonlocals still passes (regular functions and lambdas still cached correctly)

Skip lru_cache for bound methods in get_function_nonlocals to avoid
holding strong references that prevent garbage collection.
@github-actions github-actions bot added external core `langchain-core` package issues & PRs fix For PRs that implement a fix labels Feb 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core `langchain-core` package issues & PRs external fix For PRs that implement a fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

core: leak when using object member in a RunnableSequence

1 participant