Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/engine/ov_genai/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,15 @@ async def _run_generation():
generation_kwargs,
streamer
)
def _generation_exception_handler(task: asyncio.Task):
exc = task.exception()
if exc and not task.cancelled():
# Force break the below loop
streamer.text_queue.put_nowait(None)
raise exc

gen_task = asyncio.create_task(_run_generation())
gen_task.add_done_callback(_generation_exception_handler)

try:
while True:
Expand Down
3 changes: 3 additions & 0 deletions src/engine/ov_genai/streamers.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def write(self, token: Union[int, List[int]]) -> openvino_genai.StreamingStatus:

def cancel(self) -> None:
"""Signal cancellation of the streaming generation."""
# Signal completion to unblock any waiting consumer
# Write performs the same task. However, if there is nothing to be written yet, it may not be received by the consumer
self.text_queue.put_nowait(None)
self._cancelled.set()

def is_cancelled(self) -> bool:
Expand Down
7 changes: 7 additions & 0 deletions src/engine/ov_genai/vlm.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,15 @@ async def _run_generation():
generation_config=generation_kwargs,
streamer=streamer,
)
def _generation_exception_handler(task: asyncio.Task):
exc = task.exception()
if exc and not task.cancelled():
# Force break the below loop
streamer.text_queue.put_nowait(None)
raise exc

gen_task = asyncio.create_task(_run_generation())
gen_task.add_done_callback(_generation_exception_handler)

try:
while True:
Expand Down
10 changes: 10 additions & 0 deletions src/server/worker_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,16 @@ async def stream_generate(self, model_name: str, gen_config: OVGenAI_GenConfig)
stream_queue=stream_queue,
result_future=result_future,
)
async def _generation_exception_handler(future: asyncio.Future):
logger.error("Got stream cancel.")
exc = future.exception()
if exc and not future.cancelled():
# Force break the below loop
logger.error("Canceling.")
await self.infer_cancel(request_id)
stream_queue.put_nowait(None)
raise exc
result_future.add_done_callback(_generation_exception_handler)

# Register active request
async with self._lock:
Expand Down