Implement closed_stream notification and tests

This commit is contained in:
bomanaps
2025-08-15 16:02:58 +01:00
parent 90f143cd88
commit 5c78a41552
2 changed files with 192 additions and 8 deletions

View File

@ -330,8 +330,18 @@ class Swarm(Service, INetworkService):
# Close all listeners
if hasattr(self, "listeners"):
for listener in self.listeners.values():
for maddr_str, listener in self.listeners.items():
await listener.close()
# Notify about listener closure
try:
from multiaddr import Multiaddr
multiaddr = Multiaddr(maddr_str)
await self.notify_listen_close(multiaddr)
except Exception as e:
logger.warning(
f"Failed to notify listen_close for {maddr_str}: {e}"
)
self.listeners.clear()
# Close the transport if it exists and has a close method
@ -420,7 +430,9 @@ class Swarm(Service, INetworkService):
nursery.start_soon(notifee.closed_stream, self, stream)
async def notify_listen_close(self, multiaddr: Multiaddr) -> None:
raise NotImplementedError
async with trio.open_nursery() as nursery:
for notifee in self.notifees:
nursery.start_soon(notifee.listen_close, self, multiaddr)
# Generic notifier used by NetStream._notify_closed
async def notify_all(self, notifier: Callable[[INotifee], Awaitable[None]]) -> None: