Skip to content

Commit

Permalink
Merge pull request #921 from hbldh/release/v0.15.1
Browse files Browse the repository at this point in the history
Release/v0.15.1
  • Loading branch information
dlech authored Aug 3, 2022
2 parents 8d3ff00 + ac17549 commit 456cea8
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 31 deletions.
14 changes: 13 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ and this project adheres to `Semantic Versioning <https://semver.org/spec/v2.0.0
`Unreleased`_
=============

`0.15.1`_ (2022-08-03)
======================

Fixed
-----
* The global BlueZ manager now disconnects correctly on exception. Merged #918.
* Handle the race in the BlueZ D-Bus backend where the device disconnects during
the connection process which presented as ``Failed to cancel connection``. Merged #919.
* Ensure the BlueZ D-Bus scanner can reconnect after DBus disconnection. Merged #920.


`0.15.0`_ (2022-07-29)
======================

Expand Down Expand Up @@ -711,7 +722,8 @@ Fixed
* Bleak created.


.. _Unreleased: https://github.com/hbldh/bleak/compare/v0.15.0...develop
.. _Unreleased: https://github.com/hbldh/bleak/compare/v0.15.1...develop
.. _0.15.1: https://github.com/hbldh/bleak/compare/v0.15.0...v0.15.1
.. _0.15.0: https://github.com/hbldh/bleak/compare/v0.14.3...v0.15.0
.. _0.14.3: https://github.com/hbldh/bleak/compare/v0.14.2...v0.14.3
.. _0.14.2: https://github.com/hbldh/bleak/compare/v0.14.1...v0.14.2
Expand Down
2 changes: 1 addition & 1 deletion bleak/__version__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# -*- coding: utf-8 -*-

__version__ = "0.15.0"
__version__ = "0.15.1"
44 changes: 24 additions & 20 deletions bleak/backends/bluezdbus/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,27 +169,31 @@ def on_value_changed(char_path: str, value: bytes) -> None:
self._is_connected = True
except BaseException:
# calling Disconnect cancels any pending connect request
try:
reply = await self._bus.call(
Message(
destination=defs.BLUEZ_SERVICE,
interface=defs.DEVICE_INTERFACE,
path=self._device_path,
member="Disconnect",
)
)
if self._bus:
# If disconnected callback already fired, this will be a no-op
# since self._bus will be None and the _cleanup_all call will
# have already disconnected.
try:
assert_reply(reply)
except BleakDBusError as e:
# if the object no longer exists, then we know we
# are disconnected for sure, so don't need to log a
# warning about it
if e.dbus_error != ErrorType.UNKNOWN_OBJECT.value:
raise
except Exception as e:
logger.warning(
f"Failed to cancel connection ({self._device_path}): {e}"
)
reply = await self._bus.call(
Message(
destination=defs.BLUEZ_SERVICE,
interface=defs.DEVICE_INTERFACE,
path=self._device_path,
member="Disconnect",
)
)
try:
assert_reply(reply)
except BleakDBusError as e:
# if the object no longer exists, then we know we
# are disconnected for sure, so don't need to log a
# warning about it
if e.dbus_error != ErrorType.UNKNOWN_OBJECT.value:
raise
except Exception as e:
logger.warning(
f"Failed to cancel connection ({self._device_path}): {e}"
)

raise

Expand Down
26 changes: 17 additions & 9 deletions bleak/backends/bluezdbus/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
Iterable,
List,
NamedTuple,
Optional,
Set,
Tuple,
cast,
Expand Down Expand Up @@ -266,7 +267,7 @@ class BlueZManager:
"""

def __init__(self):
self._bus = MessageBus(bus_type=BusType.SYSTEM)
self._bus: Optional[MessageBus] = None
self._bus_lock = asyncio.Lock()

# dict of object path: dict of interface name: dict of property name: property value
Expand All @@ -284,44 +285,48 @@ async def async_init(self):
connected, no action is performed.
"""
async with self._bus_lock:
if self._bus.connected:
if self._bus and self._bus.connected:
return

await self._bus.connect()
# We need to create a new MessageBus each time as
# dbus-next will destory the underlying file descriptors
# when the previous one is closed in its finalizer.
bus = MessageBus(bus_type=BusType.SYSTEM)
await bus.connect()

try:
# Add signal listeners

self._bus.add_message_handler(self._parse_msg)
bus.add_message_handler(self._parse_msg)

rules = MatchRules(
interface=defs.OBJECT_MANAGER_INTERFACE,
member="InterfacesAdded",
arg0path="/org/bluez/",
)
reply = await add_match(self._bus, rules)
reply = await add_match(bus, rules)
assert_reply(reply)

rules = MatchRules(
interface=defs.OBJECT_MANAGER_INTERFACE,
member="InterfacesRemoved",
arg0path="/org/bluez/",
)
reply = await add_match(self._bus, rules)
reply = await add_match(bus, rules)
assert_reply(reply)

rules = MatchRules(
interface=defs.PROPERTIES_INTERFACE,
member="PropertiesChanged",
path_namespace="/org/bluez",
)
reply = await add_match(self._bus, rules)
reply = await add_match(bus, rules)
assert_reply(reply)

# get existing objects after adding signal handlers to avoid
# race condition

reply = await self._bus.call(
reply = await bus.call(
Message(
destination=defs.BLUEZ_SERVICE,
path="/",
Expand All @@ -341,9 +346,12 @@ async def async_init(self):

except BaseException:
# if setup failed, disconnect
await self._bus.disconnect()
bus.disconnect()
raise

# Everything is setup, so save the bus
self._bus = bus

async def active_scan(
self,
adapter_path: str,
Expand Down

0 comments on commit 456cea8

Please sign in to comment.