Skip to content

Commit

Permalink
Switch timeouts to use async_timeout (#447)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Jul 31, 2023
1 parent 20833bc commit 021ec25
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 12 deletions.
4 changes: 2 additions & 2 deletions pyhap/accessory.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
"""Module for the Accessory classes."""
import itertools
import logging

from uuid import UUID

from pyhap import SUPPORT_QR_CODE, util
from pyhap.const import (
CATEGORY_BRIDGE,
CATEGORY_OTHER,
HAP_PROTOCOL_VERSION,
HAP_REPR_AID,
HAP_REPR_IID,
HAP_PROTOCOL_VERSION,
HAP_REPR_SERVICES,
HAP_REPR_VALUE,
STANDALONE_AID,
Expand Down
14 changes: 7 additions & 7 deletions pyhap/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@

import asyncio
import functools
import os
import ipaddress
import logging
import os
import struct
from uuid import UUID

from pyhap import RESOURCE_DIR
import async_timeout

from pyhap import RESOURCE_DIR, tlv
from pyhap.accessory import Accessory
from pyhap.const import CATEGORY_CAMERA
from pyhap.util import to_base64_str, byte_bool
from pyhap import tlv

from pyhap.util import byte_bool, to_base64_str

SETUP_TYPES = {
'SESSION_ID': b'\x01',
Expand Down Expand Up @@ -859,8 +859,8 @@ async def stop_stream(self, session_info):
logger.info('[%s] Stopping stream.', session_id)
try:
ffmpeg_process.terminate()
_, stderr = await asyncio.wait_for(
ffmpeg_process.communicate(), timeout=2.0)
async with async_timeout.timeout(2.0):
_, stderr = await ffmpeg_process.communicate()
logger.debug('Stream command stderr: %s', stderr)
except asyncio.TimeoutError:
logger.error(
Expand Down
9 changes: 8 additions & 1 deletion pyhap/hap_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from urllib.parse import ParseResult, parse_qs, urlparse
import uuid

import async_timeout
from chacha20poly1305_reuseable import ChaCha20Poly1305Reusable as ChaCha20Poly1305
from cryptography.exceptions import InvalidSignature, InvalidTag
from cryptography.hazmat.primitives import serialization
Expand Down Expand Up @@ -93,6 +94,12 @@ class UnprivilegedRequestException(Exception):
pass


async def _run_with_timeout(coro, timeout: float) -> bytes:
"""Run a coroutine with a timeout."""
async with async_timeout.timeout(timeout):
return await coro


class HAPServerHandler:
"""Manages HAP connection state and handles incoming HTTP requests."""

Expand Down Expand Up @@ -820,7 +827,7 @@ def handle_resource(self) -> None:
'does not define a "get_snapshot" or "async_get_snapshot" method'
)

task = asyncio.ensure_future(asyncio.wait_for(coro, RESPONSE_TIMEOUT))
task = asyncio.ensure_future(_run_with_timeout(coro, RESPONSE_TIMEOUT))
self.send_response(HTTPStatus.OK)
self.send_header("Content-Type", "image/jpeg")
assert self.response is not None # nosec
Expand Down
1 change: 1 addition & 0 deletions pyhap/hsrp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Server Side SRP implementation

import os

from .util import long_to_bytes


Expand Down
4 changes: 3 additions & 1 deletion pyhap/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import socket
from uuid import UUID

import async_timeout
import orjson

from .const import BASE_UUID
Expand Down Expand Up @@ -135,7 +136,8 @@ async def event_wait(event, timeout):
:rtype: bool
"""
try:
await asyncio.wait_for(event.wait(), timeout)
async with async_timeout.timeout(timeout):
await event.wait()
except asyncio.TimeoutError:
pass
return event.is_set()
Expand Down
9 changes: 8 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@
README = f.read()


REQUIRES = ["cryptography", "chacha20poly1305-reuseable", "orjson>=3.7.2", "zeroconf>=0.36.2", "h11"]
REQUIRES = [
"async_timeout",
"cryptography",
"chacha20poly1305-reuseable",
"orjson>=3.7.2",
"zeroconf>=0.36.2",
"h11",
]


setup(
Expand Down

0 comments on commit 021ec25

Please sign in to comment.