3232
3333from ._cache import DNSCache
3434from ._dns import DNSQuestion , DNSQuestionType
35- from ._exceptions import NonUniqueNameException
35+ from ._exceptions import NonUniqueNameException , NotRunningException
3636from ._handlers import (
3737 MulticastOutgoingQueue ,
3838 QueryHandler ,
8080 _MDNS_PORT ,
8181 _ONE_SECOND ,
8282 _REGISTER_TIME ,
83+ _STARTUP_TIMEOUT ,
8384 _TYPE_PTR ,
8485 _UNREGISTER_TIME ,
8586)
@@ -118,15 +119,15 @@ def __init__(
118119 self .protocols : List [AsyncListener ] = []
119120 self .readers : List [asyncio .DatagramTransport ] = []
120121 self .senders : List [asyncio .DatagramTransport ] = []
122+ self .running_event : Optional [asyncio .Event ] = None
121123 self ._listen_socket = listen_socket
122124 self ._respond_sockets = respond_sockets
123125 self ._cleanup_timer : Optional [asyncio .TimerHandle ] = None
124- self ._running_event : Optional [asyncio .Event ] = None
125126
126127 def setup (self , loop : asyncio .AbstractEventLoop , loop_thread_ready : Optional [threading .Event ]) -> None :
127128 """Set up the instance."""
128129 self .loop = loop
129- self ._running_event = asyncio .Event ()
130+ self .running_event = asyncio .Event ()
130131 self .loop .create_task (self ._async_setup (loop_thread_ready ))
131132
132133 async def _async_setup (self , loop_thread_ready : Optional [threading .Event ]) -> None :
@@ -136,16 +137,11 @@ async def _async_setup(self, loop_thread_ready: Optional[threading.Event]) -> No
136137 millis_to_seconds (_CACHE_CLEANUP_INTERVAL ), self ._async_cache_cleanup
137138 )
138139 await self ._async_create_endpoints ()
139- assert self ._running_event is not None
140- self ._running_event .set ()
140+ assert self .running_event is not None
141+ self .running_event .set ()
141142 if loop_thread_ready :
142143 loop_thread_ready .set ()
143144
144- async def async_wait_for_start (self ) -> None :
145- """Wait for start up."""
146- assert self ._running_event is not None
147- await self ._running_event .wait ()
148-
149145 async def _async_create_endpoints (self ) -> None :
150146 """Create endpoints to send and receive."""
151147 assert self .loop is not None
@@ -495,8 +491,17 @@ def _run_loop() -> None:
495491 loop_thread_ready .wait ()
496492
497493 async def async_wait_for_start (self ) -> None :
498- """Wait for start up."""
499- await self .engine .async_wait_for_start ()
494+ """Wait for start up for actions that require a running Zeroconf instance.
495+
496+ Throws NotRunningException if the instance is not running or could
497+ not be started.
498+ """
499+ if self .done : # If the instance was shutdown from under us, raise immediately
500+ raise NotRunningException
501+ assert self .engine .running_event is not None
502+ await wait_event_or_timeout (self .engine .running_event , timeout = _STARTUP_TIMEOUT )
503+ if not self .engine .running_event .is_set () or self .done :
504+ raise NotRunningException
500505
501506 @property
502507 def listeners (self ) -> List [RecordUpdateListener ]:
0 commit comments