Skip to content

fix(batch): stop_on_first_result should wait for the first non-None result - #1072

Merged
pchalasani merged 6 commits into
mainfrom
fix/batch-stop-first
Jul 25, 2026
Merged

pchalasani merged 6 commits into
mainfrom
fix/batch-stop-first

Conversation

@pchalasani

Copy link
Copy Markdown
Contributor

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) called
asyncio.wait(..., FIRST_COMPLETED) exactly once, then its finally block
cancelled every still-pending task — even when every completed task mapped to
None. A fast invalid result therefore suppressed a slower valid one, contrary
to the documented behavior of continuing until a non-None result is obtained.

The fix

Wait repeatedly on the remaining task set until a non-None mapped result
appears 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 -> None fast, valid slower, slow long):

  • before: [None, None, None]
  • after: [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):

  • The regression test is now deterministic: the "valid" task is released
    from inside output_map, which runs while the first completed round is being
    processed, so it provably cannot land in that first asyncio.wait round.
    Both regression tests were confirmed to FAIL against main's implementation.
  • Added test_process_batch_async_stop_on_first_all_none — an all-None batch
    must terminate (return all Nones) rather than hang.
  • Added test_process_batch_async_stop_on_first_skips_exception_result — under
    ExceptionHandling.RETURN_NONE, a fast task that raises must not suppress a
    slower valid result either.
  • Documented stop_on_first_result semantics precisely in run_batch_task_gen,
    run_batch_tasks and run_batch_agent_method: "valid" means non-None after
    output_map; processing continues past None-mapped results; with
    ExceptionHandling.RETURN_EXCEPTION the returned exception object is itself
    non-None and stops the batch (noted only on the helpers that actually accept
    handle_exceptions).
  • New user-facing note docs/notes/batch-processing.md (+ mkdocs nav entry)
    covering the batch helpers, stop_on_first_result, its interaction with
    handle_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 passed
  • pytest tests/main/test_async_handlers.py -> 3 passed, 2 skipped
  • black --check, ruff check clean; mypy langroid/agent/batch.py shows no
    new errors (only 4 pre-existing ones in a generated .pyi, identical on main)

@pchalasani
pchalasani merged commit 1bf4362 into main Jul 25, 2026
2 checks passed
@pchalasani
pchalasani deleted the fix/batch-stop-first branch July 25, 2026 20:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

stop_on_first_result cancels pending tasks after a None result

2 participants