Skip to content

Commit

Permalink
Fix memory leak (#135)
Browse files Browse the repository at this point in the history
* Fix memory leak

* Update changelog

* Fix tests
  • Loading branch information
Shutgun authored Jul 13, 2023
1 parent b266c65 commit fcbf61b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
5 changes: 4 additions & 1 deletion devolo_plc_api/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __init__(
self.plcnet: PlcNetApi | None = None

self._background_tasks: set[asyncio.Task] = set()
self._browser: AsyncServiceBrowser | None
self._browser: AsyncServiceBrowser | None = None
self._connected = False
self._info: dict[str, ZeroconfServiceInfo] = {PLCNETAPI: ZeroconfServiceInfo(), DEVICEAPI: ZeroconfServiceInfo()}
self._logger = logging.getLogger(f"{self.__class__.__module__}.{self.__class__.__name__}")
Expand Down Expand Up @@ -207,6 +207,9 @@ async def _get_zeroconf_info(self) -> None:
self._logger.debug("Browsing for %s", service_types)
addr = None if self._multicast else self.ip
question_type = DNSQuestionType.QM if self._multicast else DNSQuestionType.QU
if self._browser:
await self._browser.async_cancel()
self._browser = None
self._browser = AsyncServiceBrowser(
zeroconf=self._zeroconf.zeroconf,
type_=service_types,
Expand Down
6 changes: 6 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [v1.3.2] - 2023/07/13

### Fixed

- Frequently connecting to an offline device lead to a memory leak

## [v1.3.1] - 2023/05/12

### Fixed
Expand Down
4 changes: 3 additions & 1 deletion tests/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ async def test_sync_connect_multicast(self, test_data: TestData):
assert get_zeroconf_info.call_count == 2

@pytest.mark.asyncio()
@pytest.mark.parametrize("device_type", [DeviceType.PLC])
@pytest.mark.usefixtures("service_browser")
async def test_async_connect_not_found(self, mock_device: Device, sleep: AsyncMock):
"""Test that an exception is raised if both APIs are not available."""
with patch("devolo_plc_api.device.AsyncServiceBrowser"), pytest.raises(DeviceNotFound):
with pytest.raises(DeviceNotFound):
await mock_device.async_connect()
assert not mock_device._connected
assert sleep.call_count == Device.MDNS_TIMEOUT
Expand Down
2 changes: 1 addition & 1 deletion tests/test_plcnetapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from . import DeviceType


class TestDeviceApi:
class TestPlcApi:
"""Test devolo_plc_api.plcnet_api.plcnetapi.PlcNetApi class."""

@pytest.mark.asyncio()
Expand Down

0 comments on commit fcbf61b

Please sign in to comment.