From 03f8b1d246caf7ad0f9b63faa63683e836c7dc90 Mon Sep 17 00:00:00 2001 From: Wendong-Fan Date: Fri, 7 Nov 2025 14:46:27 +0800 Subject: [PATCH] enhance: add SSE timeout handling and improve logging PR614 --- backend/app/controller/chat_controller.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/backend/app/controller/chat_controller.py b/backend/app/controller/chat_controller.py index 3f0b96ce9..9342b20d6 100644 --- a/backend/app/controller/chat_controller.py +++ b/backend/app/controller/chat_controller.py @@ -38,22 +38,22 @@ SSE_TIMEOUT_SECONDS = 10 * 60 async def timeout_stream_wrapper(stream_generator, timeout_seconds: int = SSE_TIMEOUT_SECONDS): - last_data_time = [time.time()] + """ + Wraps a stream generator with timeout handling. + + Closes the SSE connection if no data is received within the timeout period. + """ + last_data_time = time.time() generator = stream_generator.__aiter__() - should_stop = False try: - while not should_stop: - elapsed = time.time() - last_data_time[0] - remaining_timeout = max(0, timeout_seconds - elapsed) + while True: + elapsed = time.time() - last_data_time + remaining_timeout = timeout_seconds - elapsed - if elapsed >= timeout_seconds: - chat_logger.warning(f"SSE timeout: No data received for {elapsed:.1f} seconds, closing connection") - yield sse_json("error", {"message": "Connection timeout: No data received for 10 minutes"}) - break try: data = await asyncio.wait_for(generator.__anext__(), timeout=remaining_timeout) - last_data_time[0] = time.time() + last_data_time = time.time() yield data except asyncio.TimeoutError: chat_logger.warning(f"SSE timeout: No data received for {timeout_seconds} seconds, closing connection")