Skip to content

Commit

Permalink
fix: race during disconnect when unexpectedly connected (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Sep 11, 2022
1 parent f00c483 commit 2ceef9f
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/bleak_retry_connector/dbus.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import contextlib

import async_timeout
from bleak.backends.bluezdbus import defs
from bleak.backends.device import BLEDevice
from dbus_fast.aio import MessageBus
from dbus_fast.constants import BusType
from dbus_fast.message import Message

DISCONNECT_TIMEOUT = 5


async def disconnect_devices(devices: list[BLEDevice]) -> None:
"""Disconnect a list of devices."""
Expand All @@ -21,12 +24,13 @@ async def disconnect_devices(devices: list[BLEDevice]) -> None:
bus = await MessageBus(bus_type=BusType.SYSTEM, negotiate_unix_fd=True).connect()
for device in devices:
with contextlib.suppress(Exception):
await bus.send(
Message(
destination=defs.BLUEZ_SERVICE,
path=device.details["path"],
interface=defs.DEVICE_INTERFACE,
member="Disconnect",
async with async_timeout.timeout(DISCONNECT_TIMEOUT):
await bus.call(
Message(
destination=defs.BLUEZ_SERVICE,
path=device.details["path"],
interface=defs.DEVICE_INTERFACE,
member="Disconnect",
)
)
)
bus.disconnect()

0 comments on commit 2ceef9f

Please sign in to comment.