Skip to content

Commit

Permalink
fix: add back the bleak overall safety timeout (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Aug 2, 2022
1 parent 3692d51 commit f3f8ded
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
11 changes: 10 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Sphinx = {version = "^5.0", optional = true}
sphinx-rtd-theme = {version = "^1.0", optional = true}
myst-parser = {version = "^0.18", optional = true}
bleak = ">=0.14.3"
async-timeout = ">=4.0.1"

[tool.poetry.extras]
docs = [
Expand Down
10 changes: 9 additions & 1 deletion src/bleak_retry_connector/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from collections.abc import Callable
from typing import Any

import async_timeout
from bleak import BleakClient, BleakError
from bleak.backends.device import BLEDevice

Expand All @@ -27,6 +28,12 @@
MAX_CONNECT_ATTEMPTS = 5
BLEAK_TIMEOUT = 10

# Bleak may not always timeout
# since the dbus connection can stall
# so we have an additional timeout to
# be sure we do not block forever
BLEAK_SAFETY_TIMEOUT = 12

# These errors are transient with dbus, and we should retry
TRANSIENT_ERRORS = {"le-connection-abort-by-local", "br-connection-canceled"}

Expand Down Expand Up @@ -74,7 +81,8 @@ def _raise_if_needed(name: str, exc: Exception) -> None:
attempt += 1
_LOGGER.debug("%s: Connecting (attempt: %s)", name, attempt)
try:
await client.connect(timeout=BLEAK_TIMEOUT)
async with async_timeout.timeout(BLEAK_SAFETY_TIMEOUT):
await client.connect(timeout=BLEAK_TIMEOUT)
except asyncio.TimeoutError as exc:
timeouts += 1
_LOGGER.debug(
Expand Down
21 changes: 20 additions & 1 deletion tests/test_init.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import asyncio
from unittest.mock import MagicMock
from unittest.mock import MagicMock, patch

import pytest
from bleak import BleakClient, BleakError

import bleak_retry_connector
from bleak_retry_connector import (
MAX_TRANSIENT_ERRORS,
BleakConnectionError,
Expand Down Expand Up @@ -132,3 +133,21 @@ async def disconnect(self, *args, **kwargs):

with pytest.raises(BleakConnectionError):
await establish_connection(FakeBleakClient, MagicMock(), "test")


@pytest.mark.asyncio
async def test_bleak_connect_overruns_timeout():
class FakeBleakClient(BleakClient):
def __init__(self, *args, **kwargs):
pass

async def connect(self, *args, **kwargs):
await asyncio.sleep(40)

async def disconnect(self, *args, **kwargs):
pass

with patch.object(bleak_retry_connector, "BLEAK_SAFETY_TIMEOUT", 0), pytest.raises(
BleakNotFoundError
):
await establish_connection(FakeBleakClient, MagicMock(), "test")

0 comments on commit f3f8ded

Please sign in to comment.