github-actions[bot] commented on code in PR #41839:
URL: https://github.com/apache/beam/pull/41839#discussion_r4753382739


##########
sdks/python/apache_beam/ml/inference/vllm_inference.py:
##########
@@ -68,6 +68,28 @@
 _VLLM_COMPLETED_REQUESTS_METRIC = 'vllm:request_success_total'
 _VLLM_RUNNING_REQUESTS_METRIC = 'vllm:num_requests_running'
 
+# Cap in-flight requests per batch to match openai's default keep-alive pool
+# (100 connections). Submitting an unbounded BatchElements batch (default
+# max_batch_size=10000) to asyncio.gather at once triggers quadratic scanning 
in
+# httpcore.AsyncConnectionPool._assign_requests_to_connections(), stalling the
+# asyncio event loop past openai's 5s TCP connect timeout (APITimeoutError).
+_MAX_CONCURRENT_REQUESTS = 100
+
+
+async def _gather_bounded(
+    items: Sequence[Any],
+    request_fn: Callable[[Any], Any],
+    max_concurrency: Optional[int] = None) -> list[Any]:
+  limit = (
+      _MAX_CONCURRENT_REQUESTS if max_concurrency is None else max_concurrency)
+  semaphore = asyncio.Semaphore(limit)
+
+  async def _run_one(item: Any) -> Any:
+    async with semaphore:
+      return await request_fn(item)
+
+  return await asyncio.gather(*(_run_one(item) for item in items))

Review Comment:
   **🟢 Low**: `asyncio.gather` does not cancel sibling tasks when one task 
raises an exception. When a batch exceeds `_MAX_CONCURRENT_REQUESTS`, tasks 
waiting on `async with semaphore:` will remain queued when an in-flight request 
fails. As `_async_run_inference` exits the `async with getAsyncVLLMClient(...) 
as client:` context and `await client.__aexit__(...)` yields to the event loop, 
releasing semaphore slots allows waiting tasks to acquire the semaphore and 
fire new `request_fn(item)` calls against a closing/closed `AsyncOpenAI` client 
before `asyncio.run()` tears down the loop.
   
   Consider explicitly cancelling remaining tasks on failure in 
`_gather_bounded`:
   ```python
     tasks = [asyncio.create_task(_run_one(item)) for item in items]
     try:
       return await asyncio.gather(*tasks)
     except BaseException:
       for task in tasks:
         task.cancel()
       await asyncio.gather(*tasks, return_exceptions=True)
       raise
   ```



-- 
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