damccorm opened a new pull request, #41633:
URL: https://github.com/apache/beam/pull/41633

   Note: this PR was auto-generated by tooling. Before moving it out of draft, 
the author has done a full review of the PR, specifically evaluating it on the 
following criteria:
   
   - The PR is an appropriate fix for the issue (e.g. we're not changing a test 
to avoid a bug)
   - The PR is a simple fix to the issue (e.g. we're not adding infra when we 
could just modify a test)
   - The PR is not covering for other problems (e.g. if we're bumping timeouts, 
it is not because we're covering for a performance problem)
     
   Full details of the methodology of generation and review can be found here - 
https://docs.google.com/document/d/14lj7ECVov4T4EdwmIzNlWrRGE3lLDMOwX5xUMGzaLoc/edit?usp=sharing
   
   If you have concerns with the PR or with the underlying methodology used to 
generate this, please comment below - they will be addressed by the author not 
the tooling :)
   
   ## Summary
   
   Fixes a concurrency bug in `SqsIO.WriteBatches.BatchHandler.finishBundle()` 
where cancelling the periodic `expirationCheck` task did not wait for an 
in-flight `batches.submitExpired(false)` execution on the single-threaded 
scheduler to finish before invoking `batches.submitAll()`.
   
   ## Production Impact
   
   This affects production Java pipelines using `SqsIO.writeBatches()` with 
strict batch timeouts (`withBatchTimeout(..., true)`) on any runner (including 
Dataflow, Direct, Flink, and Spark). When `finishBundle()` ran while the 
periodic expiration thread was mid-way through `batches.submitExpired(false)`, 
`finishBundle()` did not wait for the in-flight check to complete and 
immediately called `batches.submitAll()`, which could:
   - Fail the bundle with `IllegalStateException` at 
`checkState(batches.submitAll())` when encountering a batch still locked by the 
scheduler thread.
   - Race `Dynamic.batches.clear()` against the scheduler thread iterating the 
non-thread-safe `HashMap`.
   - Proceed to `handler.waitForCompletion()` before a batch locked by the 
scheduler thread acquired its write permit.
   
   Waiting on a barrier task submitted to the single-threaded `scheduler` after 
`expirationCheck.cancel(false)` guarantees a happens-before boundary after any 
in-flight expiration check finishes before `batches.submitAll()` executes.
   
   ## Root Cause
   
   In `SqsIO.java`, `BatchHandler.finishBundle()` called:
   
   ```java
   expirationCheck.cancel(false);
   while (true) {
     try {
       expirationCheck.get(EXPIRATION_CHECK_TIMEOUT_SECS, TimeUnit.SECONDS);
     } catch (TimeoutException e) {
       LOG.warn("Waiting for timeout check to complete");
     } catch (CancellationException e) {
       break; // scheduled checks completed after cancellation
     }
   }
   checkState(batches.submitAll());
   ```
   
   In `java.util.concurrent.ScheduledThreadPoolExecutor`, `cancel(false)` 
transitions the `ScheduledFuture` to `CANCELLED` immediately even when a 
periodic execution is actively running; a subsequent `expirationCheck.get()` 
throws `CancellationException` immediately without waiting for the active run 
to return. In 
`SqsIOWriteBatchesTest.testWriteBatchesToDynamicWithStrictTimeoutAtHighVolume`, 
`batches.submitAll()` then observed a batch locked by the still-running 
`submitExpired(false)` call and failed `checkState(batches.submitAll())` with 
`IllegalStateException` at `SqsIO.java:676`.
   
   ## Fix
   
   - After `expirationCheck.cancel(false)`, submit a no-op barrier task 
`scheduler.submit(() -> {})` to the single-threaded `ScheduledExecutorService` 
and wait for `barrier.get(EXPIRATION_CHECK_TIMEOUT_SECS, TimeUnit.SECONDS)` to 
complete before proceeding to `batches.submitAll()`.
   - Keep `expirationCheck.get()` (ignoring `CancellationException`) so any 
prior uncaught `ExecutionException` from the periodic task is still propagated.
   - Add deterministic unit test 
`SqsIOWriteBatchesTest.testFinishBundleWaitsForInFlightExpirationCheck` 
verifying that `finishBundle()` waits for an in-flight expiration flush before 
submitting remaining batches.
   
   ## Verification
   
   - Ran `./gradlew :sdks:java:io:amazon-web-services2:spotlessCheck 
:sdks:java:io:amazon-web-services2:checkstyleMain 
:sdks:java:io:amazon-web-services2:checkstyleTest 
:sdks:java:io:amazon-web-services2:spotbugsMain 
:sdks:java:io:amazon-web-services2:test --tests 
org.apache.beam.sdk.io.aws2.sqs.SqsIOWriteBatchesTest` (13/13 passed, all 
checks green).
   
   ### Recent Occurrences
   
   1 failing `master` workflow run in the past 3 weeks:
   - `PreCommit Java Amazon-Web-Services2 IO Direct`:
     - https://github.com/apache/beam/actions/runs/45437578663 (2026-11-24)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to