Skip to content

Commit 6e43800

Browse files
GWealecopybara-github
authored andcommitted
fix: wait for in-flight BigQuery writes
BatchProcessor.flush() checked Queue.empty(), which only reports whether an item is still waiting, so it could return after a writer dequeued an item but before that write finished. This waits on the queue's unfinished-task count with Queue.join() so flush() blocks until in-flight writes complete. Co-authored-by: George Weale <gweale@google.com> PiperOrigin-RevId: 951620765
1 parent 2dc0745 commit 6e43800

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

src/google/adk/plugins/bigquery_agent_analytics_plugin.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,10 +1484,9 @@ def __init__(
14841484
}
14851485

14861486
async def flush(self) -> None:
1487-
"""Flushes the queue by waiting for it to be empty."""
1488-
if self._queue.empty():
1489-
return
1490-
# Wait for all items in the queue to be processed
1487+
"""Flushes the queue, blocking until in-flight writes complete."""
1488+
# empty() turns true as soon as an item is dequeued, before its write
1489+
# finishes; join() waits for the unfinished-task count to reach zero.
14911490
await self._queue.join()
14921491

14931492
async def start(self) -> None:

tests/unittests/plugins/test_bigquery_agent_analytics_plugin.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8547,6 +8547,19 @@ def _stub_arrow_prep(self, bp):
85478547
fake_batch.serialize.return_value.to_pybytes.return_value = b"batch"
85488548
bp._prepare_arrow_batch = mock.MagicMock(return_value=fake_batch)
85498549

8550+
@pytest.mark.asyncio
8551+
async def test_flush_waits_for_dequeued_write(self, dummy_arrow_schema):
8552+
bp = self._make_processor(dummy_arrow_schema)
8553+
await bp.append({"event": 0})
8554+
await bp._queue.get()
8555+
8556+
flush_task = asyncio.create_task(bp.flush())
8557+
await asyncio.sleep(0)
8558+
8559+
assert not flush_task.done()
8560+
bp._queue.task_done()
8561+
await flush_task
8562+
85508563
@pytest.mark.asyncio
85518564
async def test_queue_full_drops_are_counted(self, dummy_arrow_schema):
85528565
# Writer is not started, so a size-1 queue fills after one append and the

0 commit comments

Comments
 (0)