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
Open
Conversation
Merging this PR will improve performance by 25.71%
|
| 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)
Footnotes
-
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. ↩
…varmadatla/langchain into fix/abatch-as-completed-task-leak
This comment was marked as outdated.
This comment was marked as outdated.
This was referenced Feb 25, 2026
29 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes: #35419
abatch_as_completednow creates explicitasyncio.Taskobjects and ensures they are cleaned up on exit. The iteration is wrapped in atry/finallyso any unfinished tasks are cancelled and all tasks are awaited withgather(..., 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.