|
43 | 43 | from ._services.info import ServiceInfo, instance_name_from_service_info |
44 | 44 | from ._services.registry import ServiceRegistry |
45 | 45 | from ._updates import RecordUpdate, RecordUpdateListener |
46 | | -from ._utils.asyncio import await_awaitable, get_running_loop, shutdown_loop, wait_event_or_timeout |
| 46 | +from ._utils.asyncio import ( |
| 47 | + await_awaitable, |
| 48 | + get_running_loop, |
| 49 | + run_coro_with_timeout, |
| 50 | + shutdown_loop, |
| 51 | + wait_event_or_timeout, |
| 52 | +) |
47 | 53 | from ._utils.name import service_type_name |
48 | 54 | from ._utils.net import ( |
49 | 55 | IPVersion, |
|
62 | 68 | _FLAGS_AA, |
63 | 69 | _FLAGS_QR_QUERY, |
64 | 70 | _FLAGS_QR_RESPONSE, |
65 | | - _LOADED_SYSTEM_TIMEOUT, |
66 | 71 | _MAX_MSG_ABSOLUTE, |
67 | 72 | _MDNS_ADDR, |
68 | 73 | _MDNS_ADDR6, |
|
73 | 78 | ) |
74 | 79 |
|
75 | 80 | _TC_DELAY_RANDOM_INTERVAL = (400, 500) |
76 | | -_CLOSE_TIMEOUT = 3 |
| 81 | +_CLOSE_TIMEOUT = 3000 # ms |
77 | 82 | _REGISTER_BROADCASTS = 3 |
78 | 83 |
|
79 | 84 |
|
@@ -174,9 +179,7 @@ def close(self) -> None: |
174 | 179 | return |
175 | 180 | if not self.loop.is_running(): |
176 | 181 | return |
177 | | - asyncio.run_coroutine_threadsafe(self._async_close(), self.loop).result( |
178 | | - _CLOSE_TIMEOUT + _LOADED_SYSTEM_TIMEOUT |
179 | | - ) |
| 182 | + run_coro_with_timeout(self._async_close(), self.loop, _CLOSE_TIMEOUT) |
180 | 183 |
|
181 | 184 |
|
182 | 185 | class AsyncListener(asyncio.Protocol, QuietLogger): |
@@ -486,12 +489,13 @@ def register_service( |
486 | 489 | can register the same service on the network for resilience |
487 | 490 | (if you want this behavior set `cooperating_responders` to `True`).""" |
488 | 491 | assert self.loop is not None |
489 | | - asyncio.run_coroutine_threadsafe( |
| 492 | + run_coro_with_timeout( |
490 | 493 | await_awaitable( |
491 | 494 | self.async_register_service(info, ttl, allow_name_change, cooperating_responders) |
492 | 495 | ), |
493 | 496 | self.loop, |
494 | | - ).result(millis_to_seconds(_REGISTER_TIME * _REGISTER_BROADCASTS) + _LOADED_SYSTEM_TIMEOUT) |
| 497 | + _REGISTER_TIME * _REGISTER_BROADCASTS, |
| 498 | + ) |
495 | 499 |
|
496 | 500 | async def async_register_service( |
497 | 501 | self, |
@@ -522,8 +526,8 @@ def update_service(self, info: ServiceInfo) -> None: |
522 | 526 | Zeroconf will then respond to requests for information for that |
523 | 527 | service.""" |
524 | 528 | assert self.loop is not None |
525 | | - asyncio.run_coroutine_threadsafe(await_awaitable(self.async_update_service(info)), self.loop).result( |
526 | | - millis_to_seconds(_REGISTER_TIME * _REGISTER_BROADCASTS) + _LOADED_SYSTEM_TIMEOUT |
| 529 | + run_coro_with_timeout( |
| 530 | + await_awaitable(self.async_update_service(info)), self.loop, _REGISTER_TIME * _REGISTER_BROADCASTS |
527 | 531 | ) |
528 | 532 |
|
529 | 533 | async def async_update_service(self, info: ServiceInfo) -> Awaitable: |
@@ -577,9 +581,9 @@ def _add_broadcast_answer( # pylint: disable=no-self-use |
577 | 581 | def unregister_service(self, info: ServiceInfo) -> None: |
578 | 582 | """Unregister a service.""" |
579 | 583 | assert self.loop is not None |
580 | | - asyncio.run_coroutine_threadsafe( |
581 | | - await_awaitable(self.async_unregister_service(info)), self.loop |
582 | | - ).result(millis_to_seconds(_UNREGISTER_TIME * _REGISTER_BROADCASTS) + _LOADED_SYSTEM_TIMEOUT) |
| 584 | + run_coro_with_timeout( |
| 585 | + self.async_unregister_service(info), self.loop, _UNREGISTER_TIME * _REGISTER_BROADCASTS |
| 586 | + ) |
583 | 587 |
|
584 | 588 | async def async_unregister_service(self, info: ServiceInfo) -> Awaitable: |
585 | 589 | """Unregister a service.""" |
|
0 commit comments