Skip to content

Commit

Permalink
[Android] Fix a race condition in run_command.py.
Browse files Browse the repository at this point in the history
This patch makes limited changes to minimize the likelyhood of breaking
dependent scripts.

NOTRY=True
TBR=brettw@chromium.org
BUG=180587
TEST=ran run_command.RunOnce with a timeout of 0.

Review URL: https://codereview.chromium.org/13047009

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@190524 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
craigdh@chromium.org committed Mar 26, 2013
1 parent 1dc400b commit 3c9ea0f
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 36 deletions.
2 changes: 2 additions & 0 deletions third_party/android_testrunner/README.chromium
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Local Modifications:
suppressed.
2. Changed error message handling in |StartInstrumentation| from |shortMsg| to
|longMsg| to provide more information when debugging.
3. Applied the patch file patch.diff to fix a race condition in
run_command.RunOnce.

Here is the detail steps
1. Checkout Android source code
Expand Down
87 changes: 87 additions & 0 deletions third_party/android_testrunner/patch.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
diff --git a/third_party/android_testrunner/run_command.py b/third_party/android_testrunner/run_command.py
index d398daa..812037d 100644
--- a/third_party/android_testrunner/run_command.py
+++ b/third_party/android_testrunner/run_command.py
@@ -80,29 +80,28 @@ def RunOnce(cmd, timeout_time=None, return_output=True, stdin_input=None):
"""
start_time = time.time()
so = []
- pid = []
global _abort_on_error, error_occurred
error_occurred = False

+ if return_output:
+ output_dest = subprocess.PIPE
+ else:
+ # None means direct to stdout
+ output_dest = None
+ if stdin_input:
+ stdin_dest = subprocess.PIPE
+ else:
+ stdin_dest = None
+ pipe = subprocess.Popen(
+ cmd,
+ executable='/bin/bash',
+ stdin=stdin_dest,
+ stdout=output_dest,
+ stderr=subprocess.STDOUT,
+ shell=True)
+
def Run():
global error_occurred
- if return_output:
- output_dest = subprocess.PIPE
- else:
- # None means direct to stdout
- output_dest = None
- if stdin_input:
- stdin_dest = subprocess.PIPE
- else:
- stdin_dest = None
- pipe = subprocess.Popen(
- cmd,
- executable='/bin/bash',
- stdin=stdin_dest,
- stdout=output_dest,
- stderr=subprocess.STDOUT,
- shell=True)
- pid.append(pipe.pid)
try:
output = pipe.communicate(input=stdin_input)[0]
if output is not None and len(output) > 0:
@@ -119,27 +118,17 @@ def RunOnce(cmd, timeout_time=None, return_output=True, stdin_input=None):

t = threading.Thread(target=Run)
t.start()
-
- break_loop = False
- while not break_loop:
- if not t.isAlive():
- break_loop = True
-
- # Check the timeout
- if (not break_loop and timeout_time is not None
- and time.time() > start_time + timeout_time):
- try:
- os.kill(pid[0], signal.SIGKILL)
- except OSError:
- # process already dead. No action required.
- pass
-
+ t.join(timeout_time)
+ if t.isAlive():
+ try:
+ pipe.kill()
+ except OSError:
+ # Can't kill a dead process.
+ pass
+ finally:
logger.SilentLog("about to raise a timeout for: %s" % cmd)
raise errors.WaitForResponseTimedOutError
- if not break_loop:
- time.sleep(0.1)

- t.join()
output = "".join(so)
if _abort_on_error and error_occurred:
raise errors.AbortError(msg=output)
61 changes: 25 additions & 36 deletions third_party/android_testrunner/run_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,29 +80,28 @@ def RunOnce(cmd, timeout_time=None, return_output=True, stdin_input=None):
"""
start_time = time.time()
so = []
pid = []
global _abort_on_error, error_occurred
error_occurred = False

if return_output:
output_dest = subprocess.PIPE
else:
# None means direct to stdout
output_dest = None
if stdin_input:
stdin_dest = subprocess.PIPE
else:
stdin_dest = None
pipe = subprocess.Popen(
cmd,
executable='/bin/bash',
stdin=stdin_dest,
stdout=output_dest,
stderr=subprocess.STDOUT,
shell=True)

def Run():
global error_occurred
if return_output:
output_dest = subprocess.PIPE
else:
# None means direct to stdout
output_dest = None
if stdin_input:
stdin_dest = subprocess.PIPE
else:
stdin_dest = None
pipe = subprocess.Popen(
cmd,
executable='/bin/bash',
stdin=stdin_dest,
stdout=output_dest,
stderr=subprocess.STDOUT,
shell=True)
pid.append(pipe.pid)
try:
output = pipe.communicate(input=stdin_input)[0]
if output is not None and len(output) > 0:
Expand All @@ -119,27 +118,17 @@ def Run():

t = threading.Thread(target=Run)
t.start()

break_loop = False
while not break_loop:
if not t.isAlive():
break_loop = True

# Check the timeout
if (not break_loop and timeout_time is not None
and time.time() > start_time + timeout_time):
try:
os.kill(pid[0], signal.SIGKILL)
except OSError:
# process already dead. No action required.
pass

t.join(timeout_time)
if t.isAlive():
try:
pipe.kill()
except OSError:
# Can't kill a dead process.
pass
finally:
logger.SilentLog("about to raise a timeout for: %s" % cmd)
raise errors.WaitForResponseTimedOutError
if not break_loop:
time.sleep(0.1)

t.join()
output = "".join(so)
if _abort_on_error and error_occurred:
raise errors.AbortError(msg=output)
Expand Down

0 comments on commit 3c9ea0f

Please sign in to comment.