From 0705551542f0dc5981f2d23dcf18152a168d4e2e Mon Sep 17 00:00:00 2001 From: Pavel Moravec Date: Sat, 11 Apr 2015 12:53:56 +0000 Subject: [PATCH] Remove ServiceBrowser as a listener from Zeroconf when the service finishes Not removing the service causes that it cannot be collected by garbage collector when cancelled (other objects transitively as well). --- zeroconf.py | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/zeroconf.py b/zeroconf.py index 99883f660..63979c719 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -1040,27 +1040,30 @@ def cancel(self): self.zc.notify_all() def run(self): - while True: - now = current_time_millis() - if len(self._handlers_to_call) == 0 and self.next_time > now: - self.zc.wait(self.next_time - now) - if _GLOBAL_DONE or self.done: - return - now = current_time_millis() + try: + while True: + now = current_time_millis() + if len(self._handlers_to_call) == 0 and self.next_time > now: + self.zc.wait(self.next_time - now) + if _GLOBAL_DONE or self.done: + return + now = current_time_millis() - if self.next_time <= now: - out = DNSOutgoing(_FLAGS_QR_QUERY) - out.add_question(DNSQuestion(self.type, _TYPE_PTR, _CLASS_IN)) - for record in self.services.values(): - if not record.is_expired(now): - out.add_answer_at_time(record, now) - self.zc.send(out) - self.next_time = now + self.delay - self.delay = min(20 * 1000, self.delay * 2) - - if len(self._handlers_to_call) > 0: - handler = self._handlers_to_call.pop(0) - handler(self.zc) + if self.next_time <= now: + out = DNSOutgoing(_FLAGS_QR_QUERY) + out.add_question(DNSQuestion(self.type, _TYPE_PTR, _CLASS_IN)) + for record in self.services.values(): + if not record.is_expired(now): + out.add_answer_at_time(record, now) + self.zc.send(out) + self.next_time = now + self.delay + self.delay = min(20 * 1000, self.delay * 2) + + if len(self._handlers_to_call) > 0: + handler = self._handlers_to_call.pop(0) + handler(self.zc) + finally: + self.zc.remove_listener(self) class ServiceInfo(object):