From c84bea202fc9a6acd9b1326d45f4f31a402dab5b Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Mon, 15 Jun 2026 13:05:42 +0200 Subject: [PATCH] refac --- cptr/routers/gateway.py | 2 +- cptr/utils/chat_task.py | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/cptr/routers/gateway.py b/cptr/routers/gateway.py index 9d5fb72..b05958a 100644 --- a/cptr/routers/gateway.py +++ b/cptr/routers/gateway.py @@ -527,7 +527,7 @@ async def _proxy_to_llm( max_tokens=200, ) except Exception as e: - logger.warning("[gateway] Utility task LLM call failed: %s", e) + logger.warning("[gateway] Utility task LLM call failed: %r", e) result = "" completion_id = f"chatcmpl-{uuid.uuid4().hex[:24]}" diff --git a/cptr/utils/chat_task.py b/cptr/utils/chat_task.py index 37c470f..c691737 100644 --- a/cptr/utils/chat_task.py +++ b/cptr/utils/chat_task.py @@ -945,7 +945,12 @@ async def run_chat_task( async def emit(**data): """Stream an output delta to the user.""" - await emit_to_user(user_id, {"chat_id": chat_id, "message_id": message_id, **data}) + try: + await emit_to_user(user_id, {"chat_id": chat_id, "message_id": message_id, **data}) + except Exception: + # Socket failure must not prevent the queue push below, + # otherwise the gateway SSE stream hangs forever. + logger.debug("[task %s] emit_to_user failed", message_id[:8], exc_info=True) # Push to gateway queue if present if output_queue is not None: if "delta" in data: @@ -1491,6 +1496,16 @@ async def run_chat_task( _task_state.pop(message_id, None) await emit(done=True, error=error_msg) finally: + # Guarantee the gateway SSE stream terminates. If emit() + # already pushed a done/error event the sentinel is harmless + # (_stream checks for None separately). Without this, a + # crash in emit_to_user or an unexpected exit path leaves + # the SSE generator hanging for up to 5 minutes. + if output_queue is not None: + try: + await output_queue.put(None) + except Exception: + pass _tasks.pop(message_id, None) _task_state.pop(message_id, None) _task_chat.pop(message_id, None)