qemu: Address race condition on instantiation, where the cleanup
thread would remove a VM being born. Allow 5 seconds for the launch of
the kvm process.

git-svn-id: https://svn.apache.org/repos/asf/incubator/tashi/trunk@1360409 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/tashi/nodemanager/vmcontrol/qemu.py b/src/tashi/nodemanager/vmcontrol/qemu.py
index d00d07d..76a495d 100644
--- a/src/tashi/nodemanager/vmcontrol/qemu.py
+++ b/src/tashi/nodemanager/vmcontrol/qemu.py
@@ -170,6 +170,17 @@
 
 		for vmId in vmIds:
 			child = self.controlledVMs[vmId]
+
+			# check to see if the child was just started.
+			# Only try to check on it if startup was more
+			# than 5 seconds in the past
+			if "startTime" in child.__dict__:
+				if child.startTime + 5 < time.time():
+					del child.startTime
+				else:
+					log.info("Not processing vmId %d because it is newly started" % (vmId))
+					continue
+
 			instance = child.instance
 			name = instance.name
 
@@ -577,9 +588,15 @@
 		child.ptyFile = None
 		child.vncPort = -1
 		child.instance.vmId = child.pid
+
+		# Add a token to this new child object so that
+		# we don't mistakenly clean up when matchHostPids
+		# runs and the child process hasn't exec'ed yet.
+		child.startTime = time.time()
+
 		self.__saveChildInfo(child)
-		self.controlledVMs[child.pid] = child
 		log.info("Adding vmId %d" % (child.pid))
+		self.controlledVMs[child.pid] = child
 		return (child.pid, cmd)
 
 	def __getPtyInfo(self, child, issueContinue):