Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5176)

Unified Diff: command_wrapper/bin/command_wrapper.py

Issue 3085016: Adding timeout for log server access + more safety with bad response.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/tools/
Patch Set: Created 10 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: command_wrapper/bin/command_wrapper.py
===================================================================
--- command_wrapper/bin/command_wrapper.py (revision 54521)
+++ command_wrapper/bin/command_wrapper.py (working copy)
@@ -15,13 +15,18 @@
import optparse
import os
import platform
+import socket
import subprocess
import sys
+import threading
import time
import urllib
import uuid
+LOG_TIMEOUT = 10
+
+
def LogCommand(options, command_id,
attempt, cmd, returncode, stdout, stderr, runtime):
"""Log a command invocation and result to a central location.
@@ -60,9 +65,22 @@
f = urllib.urlopen(options.logurl, params)
ret = f.read()
f.close()
- return int(ret) != 0
+ try:
+ return int(ret) != 0
+ except ValueError:
+ return 0
+def RunWithTimeout(timeout, func, *args, **kwargs):
+ result = None
+ def CallFunc():
+ result = func(*args, **kwargs)
+ th = threading.Thread(target=CallFunc)
+ th.start()
+ th.join(timeout)
+ return result
+
+
def main(argv):
parser = optparse.OptionParser()
parser.add_option('-r', '--retries', dest='retries',
@@ -73,8 +91,17 @@
help='URL to log invocations/failures to')
(options, args) = parser.parse_args(args=argv[1:])
+ # Limit tcp connnection timeouts to 10 seconds.
+ socket.setdefaulttimeout(10)
+
command_id = uuid.uuid1()
cmd = ' '.join(args)
+
+ # Log that we're even starting.
+ RunWithTimeout(LOG_TIMEOUT, LogCommand,
+ options, command_id, -1, cmd, -1, '', '', 0)
+
+ # Try up to a certain number of times.
for r in range(options.retries):
tm = time.time()
p = subprocess.Popen(cmd, shell=True,
@@ -84,8 +111,9 @@
sys.stdout.write(p_stdout)
sys.stderr.write(p_stderr)
runtime = time.time() - tm
- accept = LogCommand(options, command_id, r, cmd,
- p.returncode, p_stdout, p_stderr, runtime)
+ accept = RunWithTimeout(LOG_TIMEOUT, LogCommand,
+ options, command_id, r, cmd,
+ p.returncode, p_stdout, p_stderr, runtime)
if accept:
return p.returncode
if p.returncode == 0:
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698