Skip to content

fix(core): cancel pending tasks in abatch_as_completed on early exit or exception#35420

Open
GAUTAM V DATLA (gautamvarmadatla) wants to merge 12 commits intolangchain-ai:masterfrom
gautamvarmadatla:fix/abatch-as-completed-task-leak
Open

fix(core): cancel pending tasks in abatch_as_completed on early exit or exception#35420
GAUTAM V DATLA (gautamvarmadatla) wants to merge 12 commits intolangchain-ai:masterfrom
gautamvarmadatla:fix/abatch-as-completed-task-leak

Conversation

@gautamvarmadatla
Copy link
Copy Markdown

Fixes: #35419

abatch_as_completed now creates explicit asyncio.Task objects and ensures they are cleaned up on exit. The iteration is wrapped in a try/finally so any unfinished tasks are cancelled and all tasks are awaited with gather(..., return_exceptions=True). Waiting on all tasks also prevents "Task exception was never retrieved" warnings if the caller stops iterating while multiple tasks finish at the same time. I also added two unit tests to cover the exception and early-break paths.

@github-actions github-actions bot added external core `langchain-core` package issues & PRs fix For PRs that implement a fix labels Feb 24, 2026
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Feb 24, 2026

Merging this PR will improve performance by 25.71%

⚠️ 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.

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 1 improved benchmark
✅ 12 untouched benchmarks
⏩ 27 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
WallTime test_async_callbacks_in_sync 19.3 ms 15.4 ms +25.71%

Comparing gautamvarmadatla:fix/abatch-as-completed-task-leak (befb4f5) with master (b3dff4a)

Open in CodSpeed

Footnotes

  1. 27 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.

@gautamvarmadatla

This comment was marked as outdated.

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 size: S 50-199 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

abatch_as_completed does not cancel pending tasks on exception or early exit

2 participants