Skip to content

Commit c2df902

Browse files
committed
fix: don't drop a cancelled re-announce; document the apple_p2p raise
gather(return_exceptions=True) also captures BaseExceptions like CancelledError, which the isinstance(result, Exception) check skipped, so a cancelled re-announce vanished silently; re-raise a captured BaseException instead of swallowing it. Document on the public update_interfaces methods that apple_p2p on a non-Apple platform raises RuntimeError.
1 parent f0090e4 commit c2df902

3 files changed

Lines changed: 34 additions & 3 deletions

File tree

‎src/zeroconf/_core.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,8 @@ def update_interfaces(
425425
426426
While it is not expected during normal operation,
427427
this function may raise EventLoopBlocked if the underlying
428-
call to `async_update_interfaces` cannot be completed.
428+
call to `async_update_interfaces` cannot be completed. Raises
429+
RuntimeError if apple_p2p is set on a non-Apple platform.
429430
"""
430431
assert self.loop is not None
431432
# Unlike register/update, the re-announce is awaited inline (to log
@@ -458,7 +459,8 @@ async def async_update_interfaces(
458459
is best-effort: a requested interface that fails to bind, or fails to
459460
re-join after a rebuild, is logged rather than raised, and likewise a
460461
registration that fails to re-announce is logged so one failure cannot
461-
block the others.
462+
block the others. Raises RuntimeError if apple_p2p is set on a non-Apple
463+
platform (input validation, matching the constructor).
462464
"""
463465
# Resolve against the retained config but only commit it after the
464466
# engine reconcile succeeds, so a failed reconcile leaves the stored
@@ -493,6 +495,10 @@ async def async_update_interfaces(
493495
for info, result in zip(infos, results, strict=True):
494496
if isinstance(result, Exception):
495497
log.warning("Error re-announcing %s after interface update: %s", info.name, result)
498+
elif isinstance(result, BaseException):
499+
# gather(return_exceptions=True) also captures BaseExceptions
500+
# such as CancelledError; don't swallow a cancellation/interrupt.
501+
raise result
496502

497503
async def async_get_service_info(
498504
self,

‎src/zeroconf/asyncio.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ async def async_update_interfaces(
236236
interfaces that disappeared, and re-announces existing
237237
registrations on the resulting senders. ``interfaces``,
238238
``ip_version`` and ``apple_p2p`` each default to the construction-time
239-
value.
239+
value. Raises RuntimeError if apple_p2p is set on a non-Apple platform.
240240
"""
241241
await self.zeroconf.async_update_interfaces(interfaces, ip_version, apple_p2p)
242242

‎tests/test_interface_update.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,31 @@ async def broadcast(info: ServiceInfo, *args: object) -> None:
400400
assert infos[1].name not in caplog.text
401401

402402

403+
@pytest.mark.asyncio
404+
async def test_update_interfaces_reannounce_cancellation_propagates(aiozc_loopback: AsyncZeroconf) -> None:
405+
"""A cancelled re-announce propagates rather than being silently dropped as a non-Exception."""
406+
zc = aiozc_loopback.zeroconf
407+
await zc.async_wait_for_start()
408+
info = ServiceInfo(
409+
"_test._tcp.local.",
410+
"Test._test._tcp.local.",
411+
addresses=[b"\x7f\x00\x00\x01"],
412+
port=80,
413+
server="test.local.",
414+
)
415+
await aiozc_loopback.async_register_service(info)
416+
await aiozc_loopback.async_update_interfaces([])
417+
await asyncio.sleep(0)
418+
419+
with (
420+
patch.object(
421+
zc, "_async_broadcast_service", new_callable=AsyncMock, side_effect=asyncio.CancelledError
422+
),
423+
pytest.raises(asyncio.CancelledError),
424+
):
425+
await aiozc_loopback.async_update_interfaces(["127.0.0.1"])
426+
427+
403428
@pytest.mark.asyncio
404429
async def test_update_interfaces_ip_change_in_one_rescan(aiozc_loopback: AsyncZeroconf) -> None:
405430
"""An interface whose address changes is removed and re-added in a single rescan."""

0 commit comments

Comments
 (0)