utils.KillProcess: Use waitpid() to wait for child processes
authorMichael Hanselmann <hansmi@google.com>
Wed, 17 Dec 2008 11:24:12 +0000 (11:24 +0000)
committerMichael Hanselmann <hansmi@google.com>
Wed, 17 Dec 2008 11:24:12 +0000 (11:24 +0000)
Sometimes the proc filesystem doesn't reflect the current status of
a process. By calling waitpid(), we make sure to get the current
information, at least for child processes. The timeout is still
kept for child processes to make sure the proc filesystem is updated.

Reviewed-by: iustinp

lib/utils.py

index 25bb9a4..5f65920 100644 (file)
@@ -1469,10 +1469,24 @@ def KillProcess(pid, signal_=signal.SIGTERM, timeout=30,
   _helper(pid, signal_, waitpid)
   if timeout <= 0:
     return
+
+  # Wait up to $timeout seconds
   end = time.time() + timeout
+  wait = 0.01
   while time.time() < end and IsProcessAlive(pid):
-    time.sleep(0.1)
+    try:
+      (result_pid, _) = os.waitpid(pid, os.WNOHANG)
+      if result_pid > 0:
+        break
+    except OSError:
+      pass
+    time.sleep(wait)
+    # Make wait time longer for next try
+    if wait < 0.1:
+      wait *= 1.5
+
   if IsProcessAlive(pid):
+    # Kill process if it's still alive
     _helper(pid, signal.SIGKILL, waitpid)