@@ -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