Skip to content

Commit d3d439a

Browse files
authored
Allow unregistering a service multiple times (#679)
1 parent 57c94bb commit d3d439a

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

tests/services/test_registry.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,29 @@ def test_only_register_once(self):
2828
registry.remove(info)
2929
registry.add(info)
3030

31+
def test_unregister_multiple_times(self):
32+
"""Verify we can unregister a service multiple times.
33+
34+
In production unregister_service and unregister_all_services
35+
may happen at the same time during shutdown. We want to treat
36+
this as non-fatal since its expected to happen and it is unlikely
37+
that the callers know about each other.
38+
"""
39+
type_ = "_test-srvc-type._tcp.local."
40+
name = "xxxyyy"
41+
registration_name = "%s.%s" % (name, type_)
42+
43+
desc = {'path': '/~paulsm/'}
44+
info = ServiceInfo(
45+
type_, registration_name, 80, 0, 0, desc, "ash-2.local.", addresses=[socket.inet_aton("10.0.1.2")]
46+
)
47+
48+
registry = r.ServiceRegistry()
49+
registry.add(info)
50+
self.assertRaises(r.ServiceNameAlreadyRegistered, registry.add, info)
51+
registry.remove(info)
52+
registry.remove(info)
53+
3154
def test_lookups(self):
3255
type_ = "_test-srvc-type._tcp.local."
3356
name = "xxxyyy"

zeroconf/_services/registry.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ def _add(self, info: ServiceInfo) -> None:
106106
def _remove(self, infos: List[ServiceInfo]) -> None:
107107
"""Remove a services under the lock."""
108108
for info in infos:
109+
if info.key not in self._services:
110+
continue
109111
old_service_info = self._services[info.key]
110112
self.types[old_service_info.type.lower()].remove(info.key)
111113
self.servers[old_service_info.server_key].remove(info.key)

0 commit comments

Comments
 (0)