Skip to content

Fix missing await on zeroconf broadcast tasks - #580

Open
Naka-Masa19 wants to merge 3 commits into
ikalchev:devfrom
Naka-Masa19:fix-zeroconf-await
Open

Naka-Masa19 wants to merge 3 commits into
ikalchev:devfrom
Naka-Masa19:fix-zeroconf-await

Conversation

@Naka-Masa19

@Naka-Masa19 Naka-Masa19 commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Two related issues when stopping the accessory driver:

  1. Every shutdown logs:

    ERROR:asyncio:Task was destroyed but it is pending!
    task: <Task pending name='Task-7' coro=<Zeroconf._async_broadcast_service() done, defined at /home/USER/.local/lib/python3.15/site-packages/zeroconf/_core.py:735> wait_for=<Future pending cb=[Task.task_wakeup()]>>
    
  2. After the server stops, HomeKit clients sometimes do not notice the disconnect reliably. Recovery can be unstable.

Cause

AsyncZeroconf.async_register_service, async_unregister_service, and async_update_service return an awaitable for the mDNS broadcast task (_async_broadcast_service). HAP-python only awaited the outer call and did not await that returned task.

With python -X dev, shutdown shows (paths and IPs anonymized)::

INFO:pyhap.accessory_driver:Stopping accessory My LightBulb on address ['192.168.1.x'], port 51826.
INFO:pyhap.accessory_driver:Closed the event loop
ERROR:asyncio:Task was destroyed but it is pending!
source_traceback: Object created at (most recent call last):
  File "/home/USER/test.py", line 38, in <module>
    driver.start()
  File "/home/USER/.local/lib/python3.15/site-packages/pyhap/accessory_driver.py", line 373, in start
    self.loop.run_forever()
  File "/usr/lib64/python3.15/asyncio/base_events.py", line 682, in run_forever
    self._run_once()
  File "/usr/lib64/python3.15/asyncio/base_events.py", line 2065, in _run_once
    handle._run()
  File "/usr/lib64/python3.15/asyncio/events.py", line 94, in _run
    self._context.run(self._callback, *self._args)
  File "/home/USER/.local/lib/python3.15/site-packages/pyhap/accessory_driver.py", line 466, in async_stop
    await self.advertiser.async_unregister_service(self.mdns_service_info)
  File "/home/USER/.local/lib/python3.15/site-packages/zeroconf/asyncio.py", line 226, in async_unregister_service
    return await self.zeroconf.async_unregister_service(info)
  File "/home/USER/.local/lib/python3.15/site-packages/zeroconf/_core.py", line 411, in async_unregister_service
    return asyncio.ensure_future(
  File "/usr/lib64/python3.15/asyncio/tasks.py", line 755, in ensure_future
    return loop.create_task(coro_or_future)
task: <Task pending name='Task-7' coro=<Zeroconf._async_broadcast_service() done, defined at /home/USER/.local/lib/python3.15/site-packages/zeroconf/_core.py:735> wait_for=<Future pending cb=[Task.task_wakeup()] created at /usr/lib64/python3.15/asyncio/base_events.py:463> created at /usr/lib64/python3.15/asyncio/tasks.py:755>

python-zeroconf’s own examples await both levels (the call and the returned awaitable).

Fix

Double-await the broadcast task for register, unregister, and update:

await (await self.advertiser.async_register_service(...))
await (await self.advertiser.async_unregister_service(...))
await (await self.advertiser.async_update_service(...))

API reference

Result

  • Shutdown pending-task error is gone.
  • In 10 consecutive server restart tests, clients always observed disconnect and reconnected successfully after restart.

Notes

AI was used to help draft this PR description; the code change and testing were done by me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant