@@ -1040,27 +1040,30 @@ def cancel(self):
10401040 self .zc .notify_all ()
10411041
10421042 def run (self ):
1043- while True :
1044- now = current_time_millis ()
1045- if len (self ._handlers_to_call ) == 0 and self .next_time > now :
1046- self .zc .wait (self .next_time - now )
1047- if _GLOBAL_DONE or self .done :
1048- return
1049- now = current_time_millis ()
1043+ try :
1044+ while True :
1045+ now = current_time_millis ()
1046+ if len (self ._handlers_to_call ) == 0 and self .next_time > now :
1047+ self .zc .wait (self .next_time - now )
1048+ if _GLOBAL_DONE or self .done :
1049+ return
1050+ now = current_time_millis ()
10501051
1051- if self .next_time <= now :
1052- out = DNSOutgoing (_FLAGS_QR_QUERY )
1053- out .add_question (DNSQuestion (self .type , _TYPE_PTR , _CLASS_IN ))
1054- for record in self .services .values ():
1055- if not record .is_expired (now ):
1056- out .add_answer_at_time (record , now )
1057- self .zc .send (out )
1058- self .next_time = now + self .delay
1059- self .delay = min (20 * 1000 , self .delay * 2 )
1060-
1061- if len (self ._handlers_to_call ) > 0 :
1062- handler = self ._handlers_to_call .pop (0 )
1063- handler (self .zc )
1052+ if self .next_time <= now :
1053+ out = DNSOutgoing (_FLAGS_QR_QUERY )
1054+ out .add_question (DNSQuestion (self .type , _TYPE_PTR , _CLASS_IN ))
1055+ for record in self .services .values ():
1056+ if not record .is_expired (now ):
1057+ out .add_answer_at_time (record , now )
1058+ self .zc .send (out )
1059+ self .next_time = now + self .delay
1060+ self .delay = min (20 * 1000 , self .delay * 2 )
1061+
1062+ if len (self ._handlers_to_call ) > 0 :
1063+ handler = self ._handlers_to_call .pop (0 )
1064+ handler (self .zc )
1065+ finally :
1066+ self .zc .remove_listener (self )
10641067
10651068
10661069class ServiceInfo (object ):
0 commit comments