BirkhoffLee commited on
Commit
38f243d
·
unverified ·
1 Parent(s): 757f620

fix: 修复了GET请求 SSE 泄漏的错误

Browse files
Files changed (1) hide show
  1. src/gateway.py +7 -3
src/gateway.py CHANGED
@@ -620,13 +620,17 @@ async def api_jobs_stream(
620
  async def event_generator() -> Any:
621
  try:
622
  while True:
623
- # 支持客户端主动断开
624
  if await request.is_disconnected():
625
  break
626
- payload = await queue.get()
 
 
 
 
 
627
  yield f"data: {json.dumps(payload)}\n\n"
628
  except asyncio.CancelledError:
629
- logger.info("Job SSE connection cancelled for user=%s", username)
630
  raise
631
  finally:
632
  _unsubscribe_user_jobs(username, queue)
 
620
  async def event_generator() -> Any:
621
  try:
622
  while True:
 
623
  if await request.is_disconnected():
624
  break
625
+ try:
626
+ payload = await asyncio.wait_for(queue.get(), timeout=15)
627
+ except asyncio.TimeoutError:
628
+ # SSE 心跳:保持连接活跃,同时让 is_disconnected() 有机会检测断开
629
+ yield ": heartbeat\n\n"
630
+ continue
631
  yield f"data: {json.dumps(payload)}\n\n"
632
  except asyncio.CancelledError:
633
+ logger.info("SSE connection cancelled for user=%s", username)
634
  raise
635
  finally:
636
  _unsubscribe_user_jobs(username, queue)