@@ -146,11 +146,14 @@ def generate_service_query(
146146 for record in zc .cache .get_all_by_details (type_ , _TYPE_PTR , _CLASS_IN )
147147 if not record .is_stale (now )
148148 )
149- if multicast and zc .question_history .suppresses (question , now , cast (Set [DNSRecord ], known_answers )):
149+ if not qu_question and zc .question_history .suppresses (
150+ question , now , cast (Set [DNSRecord ], known_answers )
151+ ):
150152 log .debug ("Asking %s was suppressed by the question history" , question )
151153 continue
152154 questions_with_known_answers [question ] = known_answers
153- zc .question_history .add_question_at_time (question , now , cast (Set [DNSRecord ], known_answers ))
155+ if not qu_question :
156+ zc .question_history .add_question_at_time (question , now , cast (Set [DNSRecord ], known_answers ))
154157
155158 return _group_ptr_queries_with_known_answers (now , multicast , questions_with_known_answers )
156159
@@ -379,7 +382,7 @@ def _async_cancel(self) -> None:
379382 self .done = True
380383 self .zc .async_remove_listener (self )
381384
382- def generate_ready_queries (self ) -> List [DNSOutgoing ]:
385+ def _generate_ready_queries (self , first_request : bool ) -> List [DNSOutgoing ]:
383386 """Generate the service browser query for any type that is due."""
384387 now = current_time_millis ()
385388 if self ._millis_to_wait (current_time_millis ()):
@@ -395,7 +398,13 @@ def generate_ready_queries(self) -> List[DNSOutgoing]:
395398 self ._next_time [type_ ] = now + self ._delay [type_ ]
396399 self ._delay [type_ ] = min (_BROWSER_BACKOFF_LIMIT * 1000 , self ._delay [type_ ] * 2 )
397400
398- return generate_service_query (self .zc , now , ready_types , self .multicast , self .question_type )
401+ # If they did not specify and this is the first request, ask QU questions
402+ # https://datatracker.ietf.org/doc/html/rfc6762#section-5.4 since we are
403+ # just starting up and we know our cache is likely empty. This ensures
404+ # the next outgoing will be sent with the known answers list.
405+ question_type = DNSQuestionType .QU if not self .question_type and first_request else self .question_type
406+
407+ return generate_service_query (self .zc , now , ready_types , self .multicast , question_type )
399408
400409 def _millis_to_wait (self , now : float ) -> Optional [float ]:
401410 """Returns the number of milliseconds to wait for the next event."""
@@ -406,14 +415,17 @@ def _millis_to_wait(self, now: float) -> Optional[float]:
406415 async def async_browser_task (self ) -> None :
407416 """Run the browser task."""
408417 await self .zc .async_wait_for_start ()
418+ first_request = True
409419 while True :
410420 timeout = self ._millis_to_wait (current_time_millis ())
411421 if timeout :
412422 await self .zc .async_wait (timeout )
413423
414- outs = self .generate_ready_queries ()
415- for out in outs :
416- self .zc .async_send (out , addr = self .addr , port = self .port )
424+ outs = self ._generate_ready_queries (first_request )
425+ if outs :
426+ first_request = False
427+ for out in outs :
428+ self .zc .async_send (out , addr = self .addr , port = self .port )
417429
418430 async def _async_cancel_browser (self ) -> None :
419431 """Cancel the browser."""
0 commit comments