mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 18:27:09 +00:00
Handle aiohttp task cancellation better (#6862)
This commit is contained in:
parent
5bb201c7fc
commit
8c97bccaaa
@ -7,6 +7,7 @@ https://home-assistant.io/components/camera/
|
|||||||
"""
|
"""
|
||||||
import asyncio
|
import asyncio
|
||||||
import collections
|
import collections
|
||||||
|
from contextlib import suppress
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
import hashlib
|
import hashlib
|
||||||
@ -167,7 +168,7 @@ class Camera(Entity):
|
|||||||
if not img_bytes:
|
if not img_bytes:
|
||||||
break
|
break
|
||||||
|
|
||||||
if img_bytes is not None and img_bytes != last_image:
|
if img_bytes and img_bytes != last_image:
|
||||||
write(img_bytes)
|
write(img_bytes)
|
||||||
|
|
||||||
# Chrome seems to always ignore first picture,
|
# Chrome seems to always ignore first picture,
|
||||||
@ -180,8 +181,8 @@ class Camera(Entity):
|
|||||||
|
|
||||||
yield from asyncio.sleep(.5)
|
yield from asyncio.sleep(.5)
|
||||||
|
|
||||||
except (asyncio.CancelledError, ConnectionResetError):
|
except asyncio.CancelledError:
|
||||||
_LOGGER.debug("Close stream by frontend.")
|
_LOGGER.debug("Stream closed by frontend.")
|
||||||
response = None
|
response = None
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
@ -263,16 +264,14 @@ class CameraImageView(CameraView):
|
|||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def handle(self, request, camera):
|
def handle(self, request, camera):
|
||||||
"""Serve camera image."""
|
"""Serve camera image."""
|
||||||
try:
|
with suppress(asyncio.CancelledError, asyncio.TimeoutError):
|
||||||
image = yield from camera.async_camera_image()
|
with async_timeout.timeout(10, loop=request.app['hass'].loop):
|
||||||
|
image = yield from camera.async_camera_image()
|
||||||
|
|
||||||
if image is None:
|
if image:
|
||||||
return web.Response(status=500)
|
return web.Response(body=image)
|
||||||
|
|
||||||
return web.Response(body=image)
|
return web.Response(status=500)
|
||||||
|
|
||||||
except asyncio.CancelledError:
|
|
||||||
_LOGGER.debug("Close stream by frontend.")
|
|
||||||
|
|
||||||
|
|
||||||
class CameraMjpegStream(CameraView):
|
class CameraMjpegStream(CameraView):
|
||||||
|
@ -78,10 +78,16 @@ def async_aiohttp_proxy_web(hass, request, web_coro, buffer_size=102400,
|
|||||||
with async_timeout.timeout(timeout, loop=hass.loop):
|
with async_timeout.timeout(timeout, loop=hass.loop):
|
||||||
req = yield from web_coro
|
req = yield from web_coro
|
||||||
|
|
||||||
|
except asyncio.CancelledError:
|
||||||
|
# The user cancelled the request
|
||||||
|
return
|
||||||
|
|
||||||
except asyncio.TimeoutError as err:
|
except asyncio.TimeoutError as err:
|
||||||
|
# Timeout trying to start the web request
|
||||||
raise HTTPGatewayTimeout() from err
|
raise HTTPGatewayTimeout() from err
|
||||||
|
|
||||||
except aiohttp.ClientError as err:
|
except aiohttp.ClientError as err:
|
||||||
|
# Something went wrong with the connection
|
||||||
raise HTTPBadGateway() from err
|
raise HTTPBadGateway() from err
|
||||||
|
|
||||||
yield from async_aiohttp_proxy_stream(hass, request, req.content,
|
yield from async_aiohttp_proxy_stream(hass, request, req.content,
|
||||||
@ -108,9 +114,12 @@ def async_aiohttp_proxy_stream(hass, request, stream, content_type,
|
|||||||
response.write(data)
|
response.write(data)
|
||||||
|
|
||||||
except (asyncio.TimeoutError, aiohttp.ClientError):
|
except (asyncio.TimeoutError, aiohttp.ClientError):
|
||||||
pass
|
# Something went wrong fetching data, close connection gracefully
|
||||||
|
yield from response.write_eof()
|
||||||
|
|
||||||
yield from response.write_eof()
|
except asyncio.CancelledError:
|
||||||
|
# The user closed the connection
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
|
Loading…
x
Reference in New Issue
Block a user