From 5dd45aea0c9c2967d465f53fe707ed536c32ce5b Mon Sep 17 00:00:00 2001 From: Isaku Yamahata Date: Fri, 22 Nov 2013 16:45:57 +0900 Subject: [PATCH] base/app_manager: RyuApp initialization race at startup There is a race between RyuApp instantiation and starting its thread. Each RyuApp spawns an event-loop thread which handles events and may generate events when a RyuApp instance is created. Currently on startup, necessary RyuApps are created, and event-loop thread is created at the same time. Then event-piping (which events are delivered to which RyuApp) is done. This causes missing events if RyuApp which was create early generates events before finishing event-piping. To address it, split RyuApp startup into three phases from two phase. - create RyuApp instances - event piping - then, start event-loop threads. Signed-off-by: Isaku Yamahata Signed-off-by: YAMAMOTO Takashi Signed-off-by: FUJITA Tomonori --- ryu/app/gre_tunnel.py | 10 ++++++++++ ryu/base/app_manager.py | 8 ++++++++ 2 files changed, 18 insertions(+) diff --git a/ryu/app/gre_tunnel.py b/ryu/app/gre_tunnel.py index 10d1f30a..3e9fbe72 100644 --- a/ryu/app/gre_tunnel.py +++ b/ryu/app/gre_tunnel.py @@ -400,6 +400,16 @@ class GRETunnel(app_manager.RyuApp): [dpset.EventDP, PortSet.EventTunnelKeyDel, PortSet.EventVMPort, PortSet.EventTunnelPort, ofp_event.EventOFPPacketIn]) + def start(self): + super(GRETunnel, self).start() + self.port_set.start() + + def stop(self): + app_mgr = app_manager.get_instance() + app_mgr.uninstantiate(self.port_set) + self.port_set = None + super(GRETunnel, self).stop() + # TODO: track active vm/tunnel ports @handler.set_ev_handler(dpset.EventDP) diff --git a/ryu/base/app_manager.py b/ryu/base/app_manager.py index a5f1c4cf..21e129b5 100644 --- a/ryu/base/app_manager.py +++ b/ryu/base/app_manager.py @@ -64,6 +64,11 @@ class RyuApp(object): self.events = hub.Queue(128) self.replies = hub.Queue() self.logger = logging.getLogger(self.name) + + def start(self): + """ + Hook that is called after startup initialization is done. + """ self.threads.append(hub.spawn(self._event_loop)) def register_handler(self, ev_cls, handler): @@ -240,6 +245,9 @@ class AppManager(object): for ev_cls in i.event_handlers.keys(): LOG.debug(" CONSUMES %s" % (ev_cls.__name__,)) + for app in self.applications.values(): + app.start() + def close(self): def close_all(close_dict): for app in close_dict.values():