Fix the bug related to asyncio.CancelledError error
Browse files
main.py
CHANGED
@@ -79,6 +79,7 @@ async def process_request(request: RequestModel, provider: Dict):
|
|
79 |
else:
|
80 |
return await fetch_response(app.state.client, url, headers, payload)
|
81 |
|
|
|
82 |
class ModelRequestHandler:
|
83 |
def __init__(self):
|
84 |
self.last_provider_index = -1
|
@@ -154,7 +155,7 @@ class ModelRequestHandler:
|
|
154 |
try:
|
155 |
response = await process_request(request, provider)
|
156 |
return response
|
157 |
-
except (Exception, HTTPException) as e:
|
158 |
logger.error(f"Error with provider {provider['provider']}: {str(e)}")
|
159 |
if auto_retry:
|
160 |
continue
|
|
|
79 |
else:
|
80 |
return await fetch_response(app.state.client, url, headers, payload)
|
81 |
|
82 |
+
import asyncio
|
83 |
class ModelRequestHandler:
|
84 |
def __init__(self):
|
85 |
self.last_provider_index = -1
|
|
|
155 |
try:
|
156 |
response = await process_request(request, provider)
|
157 |
return response
|
158 |
+
except (Exception, HTTPException, asyncio.CancelledError, httpx.ReadError) as e:
|
159 |
logger.error(f"Error with provider {provider['provider']}: {str(e)}")
|
160 |
if auto_retry:
|
161 |
continue
|
utils.py
CHANGED
@@ -73,62 +73,7 @@ def ensure_string(item):
|
|
73 |
else:
|
74 |
return str(item)
|
75 |
|
76 |
-
|
77 |
-
# async def error_handling_wrapper(generator, status_code=200):
|
78 |
-
# async def new_generator():
|
79 |
-
# try:
|
80 |
-
# yield ensure_string(first_item)
|
81 |
-
# async for item in generator:
|
82 |
-
# yield ensure_string(item)
|
83 |
-
# except (httpx.ReadError, asyncio.CancelledError) as e:
|
84 |
-
# logger.error(f"Network error in new_generator: {e}")
|
85 |
-
# raise HTTPException(status_code=503, detail=f"Stream interrupted: {str(e)}")
|
86 |
-
# except Exception as e:
|
87 |
-
# logger.exception(f"Error in new_generator: {e}")
|
88 |
-
# raise HTTPException(status_code=status_code, detail=f"Stream error: {str(e)}")
|
89 |
-
|
90 |
-
# try:
|
91 |
-
# first_item = await generator.__anext__()
|
92 |
-
# first_item_str = first_item
|
93 |
-
# if isinstance(first_item_str, (bytes, bytearray)):
|
94 |
-
# first_item_str = first_item_str.decode("utf-8")
|
95 |
-
# if isinstance(first_item_str, str):
|
96 |
-
# if first_item_str.startswith("data: "):
|
97 |
-
# first_item_str = first_item_str[6:]
|
98 |
-
# elif first_item_str.startswith("data:"):
|
99 |
-
# first_item_str = first_item_str[5:]
|
100 |
-
# if first_item_str.startswith("[DONE]"):
|
101 |
-
# logger.error("error_handling_wrapper [DONE]!")
|
102 |
-
# raise StopAsyncIteration
|
103 |
-
# try:
|
104 |
-
# first_item_str = json.loads(first_item_str)
|
105 |
-
# except json.JSONDecodeError:
|
106 |
-
# logger.error("error_handling_wrapper JSONDecodeError!" + repr(first_item_str))
|
107 |
-
# raise StopAsyncIteration
|
108 |
-
# if isinstance(first_item_str, dict) and 'error' in first_item_str:
|
109 |
-
# raise HTTPException(status_code=status_code, detail=f"{first_item_str}"[:300])
|
110 |
-
|
111 |
-
# wrapped_generator = new_generator()
|
112 |
-
# try:
|
113 |
-
# async for item in wrapped_generator:
|
114 |
-
# yield item
|
115 |
-
# except HTTPException as http_exc:
|
116 |
-
# raise HTTPException(status_code=status_code, detail=f"Wrapper error: {str(http_exc)}")
|
117 |
-
# except (httpx.ReadError, asyncio.CancelledError) as e:
|
118 |
-
# logger.error(f"Network error during streaming: {e}")
|
119 |
-
# raise HTTPException(status_code=503, detail=f"Stream interrupted: {str(e)}")
|
120 |
-
# except Exception as e:
|
121 |
-
# logger.exception(f"Unexpected error in error_handling_wrapper: {e}")
|
122 |
-
# raise HTTPException(status_code=status_code, detail=f"Unexpected error: {str(e)}")
|
123 |
-
|
124 |
-
# except StopAsyncIteration:
|
125 |
-
# raise HTTPException(status_code=status_code, detail="data: {'error': 'No data returned'}")
|
126 |
-
# except HTTPException as http_exc:
|
127 |
-
# raise HTTPException(status_code=status_code, detail=f"Wrapper error: {str(http_exc)}")
|
128 |
-
# except Exception as e:
|
129 |
-
# logger.exception(f"Error in error_handling_wrapper: {e}")
|
130 |
-
# raise HTTPException(status_code=status_code, detail=f"Wrapper error: {str(e)}")
|
131 |
-
|
132 |
async def error_handling_wrapper(generator, status_code=200):
|
133 |
try:
|
134 |
first_item = await generator.__anext__()
|
@@ -155,8 +100,12 @@ async def error_handling_wrapper(generator, status_code=200):
|
|
155 |
# 如果不是错误,创建一个新的生成器,首先yield第一个项,然后yield剩余的项
|
156 |
async def new_generator():
|
157 |
yield ensure_string(first_item)
|
158 |
-
|
159 |
-
|
|
|
|
|
|
|
|
|
160 |
|
161 |
return new_generator()
|
162 |
|
|
|
73 |
else:
|
74 |
return str(item)
|
75 |
|
76 |
+
import asyncio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
async def error_handling_wrapper(generator, status_code=200):
|
78 |
try:
|
79 |
first_item = await generator.__anext__()
|
|
|
100 |
# 如果不是错误,创建一个新的生成器,首先yield第一个项,然后yield剩余的项
|
101 |
async def new_generator():
|
102 |
yield ensure_string(first_item)
|
103 |
+
try:
|
104 |
+
async for item in generator:
|
105 |
+
yield ensure_string(item)
|
106 |
+
except (httpx.ReadError, asyncio.CancelledError) as e:
|
107 |
+
logger.error(f"Network error in new_generator: {e}")
|
108 |
+
raise
|
109 |
|
110 |
return new_generator()
|
111 |
|