Skip to content

Commit 38eb271

Browse files
authored
Switch periodic cleanup task to call_later (#913)
- Simplifies AsyncEngine to avoid the long running task
1 parent b2a7a00 commit 38eb271

2 files changed

Lines changed: 36 additions & 16 deletions

File tree

tests/test_core.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ async def test_reaper():
7474
entries_with_cache = list(itertools.chain(*(cache.entries_with_name(name) for name in cache.names())))
7575
await asyncio.sleep(1.2)
7676
entries = list(itertools.chain(*(cache.entries_with_name(name) for name in cache.names())))
77+
assert zeroconf.cache.get(record_with_1s_ttl) is None
7778
await aiozc.async_close()
7879
assert not zeroconf.question_history.suppresses(question, now, other_known_answers)
7980
assert entries != original_entries
@@ -82,6 +83,24 @@ async def test_reaper():
8283
assert record_with_1s_ttl not in entries
8384

8485

86+
@pytest.mark.asyncio
87+
async def test_reaper_aborts_when_done():
88+
"""Ensure cache cleanup stops when zeroconf is done."""
89+
with patch.object(_core, "_CACHE_CLEANUP_INTERVAL", 10):
90+
assert _core._CACHE_CLEANUP_INTERVAL == 10
91+
aiozc = AsyncZeroconf(interfaces=['127.0.0.1'])
92+
zeroconf = aiozc.zeroconf
93+
record_with_10s_ttl = r.DNSAddress('a', const._TYPE_SOA, const._CLASS_IN, 10, b'a')
94+
record_with_1s_ttl = r.DNSAddress('a', const._TYPE_SOA, const._CLASS_IN, 1, b'b')
95+
zeroconf.cache.async_add_records([record_with_10s_ttl, record_with_1s_ttl])
96+
assert zeroconf.cache.get(record_with_10s_ttl) is not None
97+
assert zeroconf.cache.get(record_with_1s_ttl) is not None
98+
await aiozc.async_close()
99+
await asyncio.sleep(1.2)
100+
assert zeroconf.cache.get(record_with_10s_ttl) is not None
101+
assert zeroconf.cache.get(record_with_1s_ttl) is not None
102+
103+
85104
class Framework(unittest.TestCase):
86105
def test_launch_and_close(self):
87106
rv = r.Zeroconf(interfaces=r.InterfaceChoice.All)

zeroconf/_core.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def __init__(
9898
self.senders: List[asyncio.DatagramTransport] = []
9999
self._listen_socket = listen_socket
100100
self._respond_sockets = respond_sockets
101-
self._cache_cleanup_task: Optional[asyncio.Task] = None
101+
self._cleanup_timer: Optional[asyncio.TimerHandle] = None
102102
self._running_event: Optional[asyncio.Event] = None
103103

104104
def setup(self, loop: asyncio.AbstractEventLoop, loop_thread_ready: Optional[threading.Event]) -> None:
@@ -110,8 +110,10 @@ def setup(self, loop: asyncio.AbstractEventLoop, loop_thread_ready: Optional[thr
110110
async def _async_setup(self, loop_thread_ready: Optional[threading.Event]) -> None:
111111
"""Set up the instance."""
112112
assert self.loop is not None
113+
self._cleanup_timer = self.loop.call_later(
114+
millis_to_seconds(_CACHE_CLEANUP_INTERVAL), self._async_cache_cleanup
115+
)
113116
await self._async_create_endpoints()
114-
self._cache_cleanup_task = self.loop.create_task(self._async_cache_cleanup())
115117
assert self._running_event is not None
116118
self._running_event.set()
117119
if loop_thread_ready:
@@ -142,26 +144,25 @@ async def _async_create_endpoints(self) -> None:
142144
if s in sender_sockets:
143145
self.senders.append(cast(asyncio.DatagramTransport, transport))
144146

145-
async def _async_cache_cleanup(self) -> None:
147+
def _async_cache_cleanup(self) -> None:
146148
"""Periodic cache cleanup."""
147-
while not self.zc.done:
148-
now = current_time_millis()
149-
self.zc.question_history.async_expire(now)
150-
self.zc.record_manager.async_updates(
151-
now, [RecordUpdate(record, None) for record in self.zc.cache.async_expire(now)]
152-
)
153-
self.zc.record_manager.async_updates_complete()
154-
await asyncio.sleep(millis_to_seconds(_CACHE_CLEANUP_INTERVAL))
149+
now = current_time_millis()
150+
self.zc.question_history.async_expire(now)
151+
self.zc.record_manager.async_updates(
152+
now, [RecordUpdate(record, None) for record in self.zc.cache.async_expire(now)]
153+
)
154+
self.zc.record_manager.async_updates_complete()
155+
assert self.loop is not None
156+
self._cleanup_timer = self.loop.call_later(
157+
millis_to_seconds(_CACHE_CLEANUP_INTERVAL), self._async_cache_cleanup
158+
)
155159

156160
async def _async_close(self) -> None:
157161
"""Cancel and wait for the cleanup task to finish."""
158162
self._async_shutdown()
159-
if self._cache_cleanup_task:
160-
self._cache_cleanup_task.cancel()
161-
with contextlib.suppress(asyncio.CancelledError):
162-
await self._cache_cleanup_task
163-
self._cache_cleanup_task = None
164163
await asyncio.sleep(0) # flush out any call soons
164+
assert self._cleanup_timer is not None
165+
self._cleanup_timer.cancel()
165166

166167
def _async_shutdown(self) -> None:
167168
"""Shutdown transports and sockets."""

0 commit comments

Comments
 (0)