Skip to content

Commit eddd1e1

Browse files
authored
Merge pull request #1265 from hbldh/release/v0.20.1
v0.20.1
2 parents b6ab166 + 33fcc4a commit eddd1e1

File tree

5 files changed

+33
-5
lines changed

5 files changed

+33
-5
lines changed

CHANGELOG.rst

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ and this project adheres to `Semantic Versioning <https://semver.org/spec/v2.0.0
1010
`Unreleased`_
1111
=============
1212

13+
`0.20.1`_ (2023-03-24)
14+
======================
15+
16+
Fixed
17+
-----
18+
* Fixed possible garbage collection of running async callback from ``BleakClient.start_notify()``.
19+
* Fixed possible garbage collection of running async callback from ``BleakScanner(detection_callback=)``.
20+
* Fixed possible garbage collection of disconnect monitor in BlueZ backend. Fixed #1258.
21+
1322
`0.20.0`_ (2023-03-17)
1423
======================
1524

@@ -921,7 +930,8 @@ Fixed
921930
* Bleak created.
922931

923932

924-
.. _Unreleased: https://github.com/hbldh/bleak/compare/v0.20.0...develop
933+
.. _Unreleased: https://github.com/hbldh/bleak/compare/v0.20.1...develop
934+
.. _0.20.1: https://github.com/hbldh/bleak/compare/v0.20.0...v0.20.1
925935
.. _0.20.0: https://github.com/hbldh/bleak/compare/v0.19.5...v0.20.0
926936
.. _0.19.5: https://github.com/hbldh/bleak/compare/v0.19.4...v0.19.5
927937
.. _0.19.4: https://github.com/hbldh/bleak/compare/v0.19.3...v0.19.4

bleak/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
Iterable,
2323
List,
2424
Optional,
25+
Set,
2526
Tuple,
2627
Type,
2728
Union,
@@ -70,6 +71,10 @@
7071
_logger.setLevel(logging.DEBUG)
7172

7273

74+
# prevent tasks from being garbage collected
75+
_background_tasks: Set[asyncio.Task] = set()
76+
77+
7378
class BleakScanner:
7479
"""
7580
Interface for Bleak Bluetooth LE Scanners.
@@ -702,7 +707,9 @@ def callback(sender: BleakGATTCharacteristic, data: bytearray):
702707
if inspect.iscoroutinefunction(callback):
703708

704709
def wrapped_callback(data):
705-
asyncio.ensure_future(callback(characteristic, data))
710+
task = asyncio.create_task(callback(characteristic, data))
711+
_background_tasks.add(task)
712+
task.add_done_callback(_background_tasks.discard)
706713

707714
else:
708715
wrapped_callback = functools.partial(callback, characteristic)

bleak/backends/bluezdbus/client.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535

3636
logger = logging.getLogger(__name__)
3737

38+
# prevent tasks from being garbage collected
39+
_background_tasks: Set[asyncio.Task] = set()
40+
3841

3942
class BleakClientBlueZDBus(BaseBleakClient):
4043
"""A native Linux Bleak Client
@@ -243,13 +246,15 @@ def on_value_changed(char_path: str, value: bytes) -> None:
243246
self._is_connected = True
244247

245248
# Create a task that runs until the device is disconnected.
246-
asyncio.ensure_future(
249+
task = asyncio.create_task(
247250
self._disconnect_monitor(
248251
self._bus,
249252
self._device_path,
250253
local_disconnect_monitor_event,
251254
)
252255
)
256+
_background_tasks.add(task)
257+
task.add_done_callback(_background_tasks.discard)
253258

254259
#
255260
# We will try to use the cache if it exists and `dangerous_use_bleak_cache`

bleak/backends/scanner.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@
1111
List,
1212
NamedTuple,
1313
Optional,
14+
Set,
1415
Tuple,
1516
Type,
1617
)
1718

1819
from ..exc import BleakError
1920
from .device import BLEDevice
2021

22+
# prevent tasks from being garbage collected
23+
_background_tasks: Set[asyncio.Task] = set()
24+
2125

2226
class AdvertisementData(NamedTuple):
2327
"""
@@ -165,7 +169,9 @@ def register_detection_callback(
165169
if inspect.iscoroutinefunction(callback):
166170

167171
def detection_callback(s, d):
168-
asyncio.ensure_future(callback(s, d))
172+
task = asyncio.create_task(callback(s, d))
173+
_background_tasks.add(task)
174+
task.add_done_callback(_background_tasks.discard)
169175

170176
else:
171177
detection_callback = callback

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "bleak"
3-
version = "0.20.0"
3+
version = "0.20.1"
44
description = "Bluetooth Low Energy platform Agnostic Klient"
55
authors = ["Henrik Blidh <[email protected]>"]
66
license = "MIT"

0 commit comments

Comments
 (0)