Skip to content

Commit 43464a7

Browse files
committed
ServiceBrowser must recheck for handlers to call when holding condition
- There was a short race condition window where the ServiceBrowser could add to _handlers_to_call in the Engine thread, have the condition notify_all called, but since the ServiceBrowser was not yet holding the condition it would not know to stop waiting and process the handlers to call.
1 parent 849e9bc commit 43464a7

1 file changed

Lines changed: 23 additions & 7 deletions

File tree

zeroconf/__init__.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1785,17 +1785,33 @@ def cancel(self) -> None:
17851785
super().cancel()
17861786
self.join()
17871787

1788+
def _wait_for_next_event(self) -> None:
1789+
"""Wait for the next handler or time to send queries."""
1790+
# If there are handlers to call
1791+
# we want to process them right away
1792+
if self._handlers_to_call:
1793+
return
1794+
1795+
# Wait for the type has the smallest next time
1796+
next_time = min(self._next_time.values())
1797+
now = current_time_millis()
1798+
1799+
if next_time <= now:
1800+
return
1801+
1802+
with self.zc.condition:
1803+
# We must check again while holding the condition
1804+
# in case the other thread has added to _handlers_to_call
1805+
# between when we checked above when we were not
1806+
# holding the condition
1807+
if not self._handlers_to_call:
1808+
self.zc.condition.wait(millis_to_seconds(next_time - now))
1809+
17881810
def run(self) -> None:
17891811
"""Run the browser thread."""
17901812
super().run()
17911813
while True:
1792-
if not self._handlers_to_call:
1793-
# Wait for the type has the smallest next time
1794-
next_time = min(self._next_time.values())
1795-
now = current_time_millis()
1796-
if next_time > now:
1797-
self.zc.wait(next_time - now)
1798-
1814+
self._wait_for_next_event()
17991815
if self.zc.done or self.done:
18001816
return
18011817

0 commit comments

Comments
 (0)