Shutdown fix: wait for the simulator to stop running before dereferencing elements.
authorClaudio-Daniel Freire <claudio-daniel.freire@inria.fr>
Wed, 15 Jun 2011 09:47:41 +0000 (11:47 +0200)
committerClaudio-Daniel Freire <claudio-daniel.freire@inria.fr>
Wed, 15 Jun 2011 09:47:41 +0000 (11:47 +0200)
NS3 isn't thread-safe, so destroying elements (which could happen when dereferenced)
while the simulator is running could be disastruous

src/nepi/testbeds/ns3/execute.py

index 5f9bb72..1fdd415 100644 (file)
@@ -129,14 +129,24 @@ class TestbedController(testbed_impl.TestbedController):
             if isinstance(element, self.LOCAL_TYPES):
                 # graceful shutdown of locally-implemented objects
                 element.Cleanup()
-        self._elements.clear()
         if self.ns3:
             self.ns3.Simulator.Stop()
+            
+            # Wait for it to stop, with a 30s timeout
+            for i in xrange(300):
+                if self.ns3.Simulator.isFinished():
+                    break
+                time.sleep(0.1)
             #self._stop_simulation("0s")
-        # TODO!!!! SHOULD WAIT UNTIL THE THREAD FINISHES
-        #   if self._simulator_thread:
-        #       self._simulator_thread.join()
+        
+        self._elements.clear()
+        
+        if self.ns3:
+            # TODO!!!! SHOULD WAIT UNTIL THE THREAD FINISHES
+            #   if self._simulator_thread:
+            #       self._simulator_thread.join()
             self.ns3.Simulator.Destroy()
+        
         self._ns3 = None
         sys.stdout.flush()
         sys.stderr.flush()