Skip to content

Commit 55ca5ea

Browse files
committed
fix: only swallow OSError when a per-interface endpoint fails
Best-effort bring-up should downgrade an expected socket-level failure, not a real bug. Roll back on any wrap failure, but re-raise anything that is not an OSError so a TypeError/AttributeError surfaces instead of being deduped into a one-time 'interface not added' warning.
1 parent 423f875 commit 55ca5ea

2 files changed

Lines changed: 30 additions & 4 deletions

File tree

‎src/zeroconf/_engine.py‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,13 +301,18 @@ async def _async_add_interface(
301301
try:
302302
await self._async_wrap_socket(respond_socket, is_sender=True)
303303
except Exception as exc:
304-
# Endpoint creation failed after the join/socket succeeded; roll
305-
# this interface back so it leaves no dangling group membership, then
306-
# log and skip rather than abort the whole reconcile so the other
307-
# interfaces still come up (best-effort bring-up).
304+
# Roll back the socket + group join on any failure so nothing is left
305+
# dangling.
308306
respond_socket.close()
309307
if listen_socket is not None:
310308
drop_multicast_member(listen_socket, interface)
309+
if not isinstance(exc, OSError):
310+
# Only an expected socket-level failure is best-effort; a real
311+
# bug (e.g. TypeError) must propagate rather than be downgraded
312+
# to a one-time "interface not added" warning.
313+
raise
314+
# Log and skip rather than abort the whole reconcile so the other
315+
# interfaces still come up (best-effort bring-up).
311316
self.zc.log_warning_once(f"Interface {interface!r} not added: {exc}")
312317
return False
313318
return True

‎tests/test_interface_update.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,27 @@ async def test_add_interface_rollback_without_listen_socket(aiozc_loopback: Asyn
519519
mock_drop.assert_not_called()
520520

521521

522+
@pytest.mark.asyncio
523+
async def test_add_interface_propagates_non_oserror(aiozc_loopback: AsyncZeroconf) -> None:
524+
"""A non-OSError (a real bug) propagates after rollback, not downgraded to a warning."""
525+
engine = aiozc_loopback.zeroconf.engine
526+
await aiozc_loopback.zeroconf.async_wait_for_start()
527+
528+
fake_socket = Mock()
529+
listen_socket = Mock()
530+
with (
531+
patch.object(_engine, "add_interface", return_value=fake_socket),
532+
patch.object(_engine, "drop_multicast_member") as mock_drop,
533+
patch.object(_engine.AsyncEngine, "_async_wrap_socket", new=AsyncMock(side_effect=TypeError("bug"))),
534+
pytest.raises(TypeError),
535+
):
536+
await engine._async_add_interface("127.0.0.1", listen_socket, False)
537+
538+
# Rolled back (socket closed, membership dropped) even though it propagates.
539+
fake_socket.close.assert_called_once()
540+
mock_drop.assert_called_once()
541+
542+
522543
@pytest.mark.asyncio
523544
async def test_update_interfaces_keeps_dual_use_listen_socket(aiozc_loopback: AsyncZeroconf) -> None:
524545
"""A dual-use sender (the listen socket itself) is never torn down on rescan."""

0 commit comments

Comments
 (0)