Repository navigation
fix(batch): stop_on_first_result should wait for the first non-None result - #1072
Merged
Merged
Conversation
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.
Supersedes #1069 by @KXHXK, whose original commit and fix are preserved here
(with authorship). Fixes #1068.
The bug
_process_batch_async(..., stop_on_first_result=True)calledasyncio.wait(..., FIRST_COMPLETED)exactly once, then itsfinallyblockcancelled every still-pending task — even when every completed task mapped to
None. A fast invalid result therefore suppressed a slower valid one, contraryto the documented behavior of continuing until a non-
Noneresult is obtained.The fix
Wait repeatedly on the remaining task set until a non-
Nonemapped resultappears or no tasks remain. Results keep their original input positions, and
only the tasks still pending at that point are cancelled. Exception handling
and cleanup stay on the same paths.
Reproducer from #1068 (
invalid->Nonefast,validslower,slowlong):[None, None, None][None, 'Processed valid', None]Hardening added on top of the original PR
Following an adversarial review (asyncio-correctness, contract/caller-impact,
test-quality, and a contract-free cold-diff lens):
from inside
output_map, which runs while the first completed round is beingprocessed, so it provably cannot land in that first
asyncio.waitround.Both regression tests were confirmed to FAIL against
main's implementation.test_process_batch_async_stop_on_first_all_none— an all-Nonebatchmust terminate (return all
Nones) rather than hang.test_process_batch_async_stop_on_first_skips_exception_result— underExceptionHandling.RETURN_NONE, a fast task that raises must not suppress aslower valid result either.
stop_on_first_resultsemantics precisely inrun_batch_task_gen,run_batch_tasksandrun_batch_agent_method: "valid" means non-Noneafteroutput_map; processing continues pastNone-mapped results; withExceptionHandling.RETURN_EXCEPTIONthe returned exception object is itselfnon-
Noneand stops the batch (noted only on the helpers that actually accepthandle_exceptions).docs/notes/batch-processing.md(+ mkdocs nav entry)covering the batch helpers,
stop_on_first_result, its interaction withhandle_exceptions,batch_size/sequential, and the behavior change.No production-logic change beyond the original PR's loop fix.
Verification
pytest tests/main/test_batch.py -k process_batch_async-> 11 passedpytest tests/main/test_async_handlers.py-> 3 passed, 2 skippedblack --check,ruff checkclean;mypy langroid/agent/batch.pyshows nonew errors (only 4 pre-existing ones in a generated
.pyi, identical on main)