fix: handle ClientConnectionResetError gracefully in stream_response
This commit is contained in:
parent
78947ac801
commit
387726eb24
@ -13,6 +13,7 @@ import asyncio
|
|||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
|
|
||||||
from aiohttp import BasicAuth
|
from aiohttp import BasicAuth
|
||||||
|
from aiohttp.client_exceptions import ClientConnectionResetError
|
||||||
from aiohttp.web import (
|
from aiohttp.web import (
|
||||||
HTTPException,
|
HTTPException,
|
||||||
HTTPBadRequest,
|
HTTPBadRequest,
|
||||||
@ -124,6 +125,9 @@ async def stream_response(request, async_data_generator, content_type='text/html
|
|||||||
else:
|
else:
|
||||||
d = json.dumps(d, ensure_ascii=False)
|
d = json.dumps(d, ensure_ascii=False)
|
||||||
await res.write(d.encode('utf-8'))
|
await res.write(d.encode('utf-8'))
|
||||||
|
except ClientConnectionResetError:
|
||||||
|
# 客户端已断开,静默忽略,不要 re-raise
|
||||||
|
break
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
e = Exception(f'write error{e=}, {d=}')
|
e = Exception(f'write error{e=}, {d=}')
|
||||||
exception(f'{e}\n{format_exc()}')
|
exception(f'{e}\n{format_exc()}')
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user