Skip to content

Commit 37ecec1

Browse files
committed
fix(core): only swallow CancelledError from setup, not all setup errors
Copilot review on #1706: ``gather(return_exceptions=True)`` was too broad — it would have hidden a genuine setup failure (e.g. a socket bind error in ``_async_create_endpoints``) during close. Switch back to ``try/await/except CancelledError`` and gate the swallow on ``self._setup_task.cancelled()``; that's True only when the task itself was cancelled (the close-before-start case we want to absorb), so outer-task cancellation and non-cancel setup exceptions both propagate. Also update the ``test_service_browser_listeners_no_update_service`` docstring to describe what the test now actually asserts — that a listener which ignores update events records only add/remove callbacks — since the listener now provides an explicit no-op ``update_service``.
1 parent 75d7056 commit 37ecec1

2 files changed

Lines changed: 12 additions & 6 deletions

File tree

‎src/zeroconf/_engine.py‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,17 @@ def _async_schedule_next_cache_cleanup(self) -> None:
146146
async def _async_close(self) -> None:
147147
"""Cancel and wait for the cleanup task to finish."""
148148
assert self._setup_task is not None
149-
# ``gather(return_exceptions=True)`` lets a setup task that was
150-
# cancelled by a prior ``_async_shutdown`` (close-before-start)
151-
# finish without re-raising, while still propagating cancellation
152-
# of the calling task.
153-
await asyncio.gather(self._setup_task, return_exceptions=True)
149+
# Setup may have been cancelled by a prior ``_async_shutdown``
150+
# (close-before-start); swallow that case only. ``setup_task.cancelled()``
151+
# is True precisely when the task itself was cancelled, so a
152+
# ``CancelledError`` raised by an outer-task cancellation falls
153+
# through to the caller, and any non-cancel setup exception is
154+
# re-raised by ``await``.
155+
try:
156+
await self._setup_task
157+
except asyncio.CancelledError:
158+
if not self._setup_task.cancelled():
159+
raise
154160
self._async_shutdown()
155161
await asyncio.sleep(0) # flush out any call soons
156162
if self._cleanup_timer is not None:

‎tests/services/test_browser.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1033,7 +1033,7 @@ def update_service(self, zc, type_, name) -> None: # type: ignore[no-untyped-de
10331033

10341034

10351035
def test_service_browser_listeners_no_update_service():
1036-
"""Test that the ServiceBrowser ServiceListener that does not implement update_service."""
1036+
"""A listener that ignores update events records only add/remove callbacks."""
10371037

10381038
# instantiate a zeroconf instance
10391039
zc = Zeroconf(interfaces=["127.0.0.1"])

0 commit comments

Comments
 (0)