Skip to content

Conversation

@MirrorDNA-Reflection-Protocol
Copy link
Contributor

PR Title: fix(core): prevent async task garbage collection (RUF006)

Description

This PR addresses a cryptic issue (flagged by Ruff rule RUF006) where asyncio tasks created via loop.create_task could be garbage collected mid-execution because no strong reference was maintained.

In libs/core/langchain_core/language_models/llms.py, the retry decorator's _before_sleep hook creates a fire-and-forget task for logging/callbacks. If the garbage collector runs before this task completes, the task may be destroyed, leading to silent failures.

Changes

  • Introduced a module-level set _background_tasks to hold strong references to running tasks.
  • Updated _before_sleep to add new tasks to this set.
  • Added a done_callback to remove the task from the set upon completion, preventing memory leaks.

Verification

  • Verified logic with a standalone script to ensure tasks are added/removed from the set correctly.
  • This is a standard pattern recommended in the Python asyncio documentation.

Checklist

  • I have read the contributing guidelines.
  • I have run tests locally (logic verification).

@github-actions github-actions bot added core `langchain-core` package issues & PRs fix For PRs that implement a fix labels Dec 7, 2025
@codspeed-hq
Copy link

codspeed-hq bot commented Dec 7, 2025

CodSpeed Performance Report

Merging #34238 will not alter performance

Comparing MirrorDNA-Reflection-Protocol:fix/core-async-gc-ruf006 (07a1f42) with master (9875ffb)

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

Summary

✅ 13 untouched
⏩ 21 skipped1

Footnotes

  1. 21 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a potential async task garbage collection issue (flagged by Ruff rule RUF006) in the retry decorator's callback handling. The fix introduces a module-level set to maintain strong references to fire-and-forget tasks, preventing them from being prematurely garbage collected during execution.

Key Changes:

  • Introduced _background_tasks set to hold strong references to running async tasks
  • Modified task creation in _before_sleep to register tasks with the tracking set
  • Added cleanup callback using add_done_callback to prevent memory leaks


logger = logging.getLogger(__name__)

_background_tasks: set[asyncio.Task] = set()
Copy link

Copilot AI Dec 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type annotation for _background_tasks is incomplete. Since asyncio.Task is a generic type, it should include a type parameter. Consider using set[asyncio.Task[Any]] to properly type-hint the set of tasks with their return types.

Copilot generated this review using guidance from repository custom instructions.
@mdrxy mdrxy merged commit ba6c259 into langchain-ai:master Dec 8, 2025
88 checks passed
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 fix For PRs that implement a fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants