diff --git a/src/zeroconf/_core.py b/src/zeroconf/_core.py index 5e3a7f46..71e2c2f4 100644 --- a/src/zeroconf/_core.py +++ b/src/zeroconf/_core.py @@ -24,6 +24,7 @@ import asyncio import logging +import random import sys import threading from collections.abc import Awaitable @@ -544,6 +545,11 @@ async def async_check_service( instance_name = instance_name_from_service_info(info, strict=strict) if cooperating_responders: return + + # Wait a random amount of time up avoid collisions and avoid + # a thundering herd when multiple services are started on the network + await self.async_wait(random.randint(150, 250)) # noqa: S311 + next_instance_number = 2 next_time = now = current_time_millis() i = 0 diff --git a/src/zeroconf/const.py b/src/zeroconf/const.py index c3a62875..1db39a46 100644 --- a/src/zeroconf/const.py +++ b/src/zeroconf/const.py @@ -28,7 +28,7 @@ # Some timing constants _UNREGISTER_TIME = 125 # ms -_CHECK_TIME = 175 # ms +_CHECK_TIME = 500 # ms _REGISTER_TIME = 225 # ms _LISTENER_TIME = 200 # ms _BROWSER_TIME = 10000 # ms diff --git a/tests/test_asyncio.py b/tests/test_asyncio.py index b6e124aa..fe24b148 100644 --- a/tests/test_asyncio.py +++ b/tests/test_asyncio.py @@ -1081,7 +1081,8 @@ def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT, v6_flow_scope=()): # The rest of the startup questions should have # known answers for answer_list in answers[1:-2]: - assert len(answer_list) == 1 + # Allow 0 or 1 answers due to random delays and timing + assert len(answer_list) <= 1 # Once the TTL is reached, the last question should have no known answers assert len(answers[-1]) == 0