From ec1226d04702a8f80d3e098d08dbb649aa45c7a5 Mon Sep 17 00:00:00 2001
From: Yi Tseng <a86487817@gmail.com>
Date: Fri, 3 Jun 2016 16:07:28 +0800
Subject: [PATCH] Fix RuntimeError of lldp_packet_in_handler

Items should not be removed during iteration

Error message
```
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/ryu/base/app_manager.py", line 290, in _event_loop
    handler(ev)
  File "/usr/local/lib/python3.4/dist-packages/ryu/topology/switches.py", line 821, in lldp_packet_in_handler
    for host in self.hosts.values():
RuntimeError: dictionary changed size during iteration
```

Signed-off-by: Yi Tseng <a86487817@gmail.com>
---
 ryu/topology/switches.py | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/ryu/topology/switches.py b/ryu/topology/switches.py
index 971d30b..e1081ae 100644
--- a/ryu/topology/switches.py
+++ b/ryu/topology/switches.py
@@ -817,10 +817,14 @@ class Switches(app_manager.RyuApp):
         if link not in self.links:
             self.send_event_to_observers(event.EventLinkAdd(link))
 
-            # remove hosts from edge port
+            # remove hosts if it's not attached to edge port
+            host_to_del = []
             for host in self.hosts.values():
                 if not self._is_edge_port(host.port):
-                    del self.hosts[host.mac]
+                    host_to_del.append(host.mac)
+
+            for host_mac in host_to_del:
+                del self.hosts[host_mac]
 
         if not self.links.update_link(src, dst):
             # reverse link is not detected yet.
-- 
2.7.4 (Apple Git-66)

