Repository: ambari
Updated Branches:
  refs/heads/branch-2.1 8a211325e -> c3bec2242


AMBARI-11956. HBase Master stop is hanging when enabling security (Ivan Kozlov 
via smohanty)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/c3bec224
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/c3bec224
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/c3bec224

Branch: refs/heads/branch-2.1
Commit: c3bec2242a4aec8f2c5ccaf2ea28a9c4c384ecba
Parents: 8a21132
Author: Sumit Mohanty <smoha...@hortonworks.com>
Authored: Tue Jun 16 12:37:03 2015 -0700
Committer: Sumit Mohanty <smoha...@hortonworks.com>
Committed: Tue Jun 16 12:43:25 2015 -0700

----------------------------------------------------------------------
 .../python/resource_management/core/shell.py    | 42 +++++++++++++-------
 1 file changed, 27 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/c3bec224/ambari-common/src/main/python/resource_management/core/shell.py
----------------------------------------------------------------------
diff --git a/ambari-common/src/main/python/resource_management/core/shell.py 
b/ambari-common/src/main/python/resource_management/core/shell.py
index 75b5fcf..75d4e56 100644
--- a/ambari-common/src/main/python/resource_management/core/shell.py
+++ b/ambari-common/src/main/python/resource_management/core/shell.py
@@ -212,9 +212,9 @@ def _call(command, logoutput=None, throw_on_failure=True, 
stdout=subprocess.PIPE
                             preexec_fn=preexec_fn)
     
     if timeout:
-      timeout_happened=False
-      start = time.time()
-      end = start+timeout
+      timeout_event = threading.Event()
+      t = threading.Timer( timeout, _on_timeout, [proc, timeout_event] )
+      t.start()
       
     if not wait_for_finish:
       return proc
@@ -235,11 +235,13 @@ def _call(command, logoutput=None, throw_on_failure=True, 
stdout=subprocess.PIPE
     all_output = ""
                   
     while read_set:
-      if timeout and time.time()> end:
-        timeout_happened=True
-        proc.kill()
+
+      is_proccess_running = (proc.poll() == None)
+      ready, _, _ = select.select(read_set, [], [], 1)
+
+      if not is_proccess_running and not ready:
         break
-      ready, _, _ = select.select(read_set, [], [])
+
       for out_fd in read_set:
         if out_fd in ready:
           line = os.read(out_fd.fileno(), 1024)
@@ -247,6 +249,7 @@ def _call(command, logoutput=None, throw_on_failure=True, 
stdout=subprocess.PIPE
           if not line:
             read_set = copy.copy(read_set)
             read_set.remove(out_fd)
+            out_fd.close()
             continue
           
           fd_to_string[out_fd] += line
@@ -263,12 +266,7 @@ def _call(command, logoutput=None, throw_on_failure=True, 
stdout=subprocess.PIPE
             _print(line)    
   
     # Wait for process to terminate
-    while proc.poll() == None:
-      if timeout and time.time()> end:
-        timeout_happened=True
-        proc.kill()
-        break
-      time.sleep(1)
+    proc.wait()
 
   finally:
     for fp in files_to_close:
@@ -279,7 +277,10 @@ def _call(command, logoutput=None, throw_on_failure=True, 
stdout=subprocess.PIPE
   all_output = all_output.strip('\n')
   
   if timeout: 
-    if timeout_happened:
+    if not timeout_event.is_set():
+      t.cancel()
+    # timeout occurred
+    else:
       err_msg = ("Execution of '%s' was killed due timeout after %d seconds") 
% (command, timeout)
       raise ExecuteTimeoutException(err_msg)
    
@@ -360,4 +361,15 @@ def string_cmd_from_args_list(command, auto_escape=True):
 
 def _print(line):
   sys.stdout.write(line)
-  sys.stdout.flush()
\ No newline at end of file
+  sys.stdout.flush()
+
+def _on_timeout(proc, timeout_event):
+  timeout_event.set()
+  if proc.poll() == None:
+    try:
+      proc.terminate()
+      proc.wait()
+    # catch race condition if proc already dead
+    except OSError:
+      pass
+

Reply via email to