damccorm opened a new pull request, #40812: URL: https://github.com/apache/beam/pull/40812
## Summary Follow-up to #40593. `BoundedQueueExecutor.onWorkStolen()` calls `workQueue.removeIf(BoundedQueueExecutor::isStaleShell)` on `LinkedBlockingQueue` while worker threads concurrently dequeue tasks via `ThreadPoolExecutor.getTask() -> LinkedBlockingQueue.poll()`. In OpenJDK's `LinkedBlockingQueue.bulkRemove(Predicate)` (used by `removeIf`), the non-volatile `head` field (`Node<E> p = null, ancestor = head;`) and `Node.item` field (`nodes[i].item`) are read outside `fullyLock()`, racing with concurrent consumer threads writing `head = first` and `first.item = null` in `LinkedBlockingQueue.dequeue()` under `takeLock`. This triggers a ThreadSanitizer (`TSAN`) data race in `BoundedQueueExecutorTest.testConcurrentExecuteAndPollWork_processesEachWorkExactlyOnce`. Replace `workQueue.removeIf(...)` with `workQueue.iterator()` + `Iterator.remove()` (matching the idiom used by `ThreadPoolExecutor.purge()`): - `LinkedBlockingQueue.Itr` acquires `fullyLock()` for all reads/writes of `head`, `Node.next`, and `Node.item`, while still caching `ancestor` across `remove()` calls for amortized O(1) unlinking per element (O(n) total scan). - If `executor.shutdown()` races concurrently with queue removal, invoke `executor.purge()` so `ThreadPoolExecutor.tryTerminate()` is notified in case the queue became empty after `shutdown()` checked it. ## Testing - `./gradlew :runners:google-cloud-dataflow-java:worker:test --tests org.apache.beam.runners.dataflow.worker.util.BoundedQueueExecutorTest --tests org.apache.beam.runners.dataflow.worker.util.KeyGroupWorkIndexTest` -- 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]
