Skip to content

Fix BaseAsyncBatchLookupDoFn race losing batch results - #6027

Open
s278gupt wants to merge 2 commits into
mainfrom
fix/async-batch-lookup-race
Open

s278gupt wants to merge 2 commits into
mainfrom
fix/async-batch-lookup-race

Conversation

@s278gupt

Copy link
Copy Markdown
Collaborator

Summary

BaseAsyncBatchLookupDoFn intermittently failed bundles with:

IllegalStateException: Expected requestCount == responseCount, but <number> != <number>
    at com.spotify.scio.transforms.BaseAsyncBatchLookupDoFn.finishBundle(...)

Fixes #6026.

Root cause

The batch response callback in handleOutput queued one results entry per distinct ID of the batch, while flush() removes the batch's future from futures upon consuming an entry. If processElement or finishBundle flushed while the callback was still iterating keyedInputs (having queued the first ID's entry but not the rest):

  1. flush() consumed the first entry and removed the batch's future from tracking,
  2. finishBundle then saw an empty futures map and skipped waitForFutures,
  3. the final flush drained only the results that had landed, and
  4. the remaining per-ID results arrived after the inputCount == outputCount check — failing it.

The window is load-dependent (any batch with 2+ distinct IDs), so the failure is intermittent and the runner retries the bundle, re-executing all of its lookups.

Fix

Build the complete per-batch result list locally inside the callback and queue it as a single results entry (both success and failure paths). Consuming that entry in flush() now implies the entire batch has been emitted, so futures.remove(key) is legitimate — restoring the one-entry-per-tracked-future invariant the unbatched BaseAsyncDoFn already has. With this, finishBundle's waitForFutures (which is guaranteed to complete only after the queueing callback body finishes for all three future types) always runs before the final flush and count check.

Additionally, the failure callback now null-checks inputs.remove(id) and logs (previously an NPE risk), mirroring the success path.

Test

AsyncBatchLookupDoFnTest gains a deterministic reproducer that drives the DoFn directly: a 2-element batch whose callback is parked mid-batch (after ID "1" is produced, before ID "2" is queued), an extra element triggers flush() mid-callback, then the bundle ends. Verified it fails on main with the issue's exact exception (Expected requestCount == responseCount, but 3 != 2) and passes with this change.

Validation

  • scio-core transforms.* suites: 82 tests + 85 ScalaCheck cases pass (incl. AsyncDoFnSpec 3×100)
  • scio-grpc GrpcBatchDoFnTest passes; scio-google-cloud-platform compiles (both extend the changed class; public API unchanged)
  • javafmtCheck, scalafmtCheck, headerCheck pass

The batch response callback queued one results entry per distinct ID of
the batch, but flush() removed the batch's future from tracking upon
consuming the first entry. When processElement or finishBundle flushed at
that moment, the batch future was no longer tracked, so finishBundle
skipped waitForFutures and the remaining per-ID results landed only after
the inputCount == outputCount check, intermittently failing the bundle
with IllegalStateException ("Expected requestCount == responseCount").

Build the full batch result locally inside the callback and queue it as
a single results entry instead, so that consuming the entry in flush()
implies the entire batch has been emitted, making futures.remove safe.
Also make the failure callback null-safe when an ID is no longer pending,
mirroring the success path.

Fixes #6026

Co-Authored-By: Xirp Agent (glm-5-3) <xirp-agent@users.noreply.ghe.spotify.net>
@codecov

codecov Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 62.40%. Comparing base (0cff979) to head (1aa15f6).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6027      +/-   ##
==========================================
+ Coverage   62.33%   62.40%   +0.07%     
==========================================
  Files         326      326              
  Lines       11867    11877      +10     
  Branches      832      835       +3     
==========================================
+ Hits         7397     7412      +15     
+ Misses       4470     4465       -5     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@s278gupt
s278gupt marked this pull request as ready for review November 13, 2026 17:11

This branch has not been deployed

No deployments
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.

BaseAsyncBatchLookupDoFn: intermittent "requestCount == responseCount" failure

1 participant