|
3 | 3 | import re
|
4 | 4 | import platform
|
5 | 5 | import tempfile
|
| 6 | +import threading |
6 | 7 |
|
7 | 8 | import lit.ShUtil as ShUtil
|
8 | 9 | import lit.Test as Test
|
@@ -33,28 +34,127 @@ def __init__(self, cwd, env):
|
33 | 34 | self.cwd = cwd
|
34 | 35 | self.env = dict(env)
|
35 | 36 |
|
36 |
| -def executeShCmd(cmd, shenv, results): |
| 37 | +class TimeoutHelper(object): |
| 38 | + """ |
| 39 | + Object used to helper manage enforcing a timeout in |
| 40 | + _executeShCmd(). It is passed through recursive calls |
| 41 | + to collect processes that have been executed so that when |
| 42 | + the timeout happens they can be killed. |
| 43 | + """ |
| 44 | + def __init__(self, timeout): |
| 45 | + self.timeout = timeout |
| 46 | + self._procs = [] |
| 47 | + self._timeoutReached = False |
| 48 | + self._doneKillPass = False |
| 49 | + # This lock will be used to protect concurrent access |
| 50 | + # to _procs and _doneKillPass |
| 51 | + self._lock = None |
| 52 | + self._timer = None |
| 53 | + |
| 54 | + def cancel(self): |
| 55 | + if not self.active(): |
| 56 | + return |
| 57 | + self._timer.cancel() |
| 58 | + |
| 59 | + def active(self): |
| 60 | + return self.timeout > 0 |
| 61 | + |
| 62 | + def addProcess(self, proc): |
| 63 | + if not self.active(): |
| 64 | + return |
| 65 | + needToRunKill = False |
| 66 | + with self._lock: |
| 67 | + self._procs.append(proc) |
| 68 | + # Avoid re-entering the lock by finding out if kill needs to be run |
| 69 | + # again here but call it if necessary once we have left the lock. |
| 70 | + # We could use a reentrant lock here instead but this code seems |
| 71 | + # clearer to me. |
| 72 | + needToRunKill = self._doneKillPass |
| 73 | + |
| 74 | + # The initial call to _kill() from the timer thread already happened so |
| 75 | + # we need to call it again from this thread, otherwise this process |
| 76 | + # will be left to run even though the timeout was already hit |
| 77 | + if needToRunKill: |
| 78 | + assert self.timeoutReached() |
| 79 | + self._kill() |
| 80 | + |
| 81 | + def startTimer(self): |
| 82 | + if not self.active(): |
| 83 | + return |
| 84 | + |
| 85 | + # Do some late initialisation that's only needed |
| 86 | + # if there is a timeout set |
| 87 | + self._lock = threading.Lock() |
| 88 | + self._timer = threading.Timer(self.timeout, self._handleTimeoutReached) |
| 89 | + self._timer.start() |
| 90 | + |
| 91 | + def _handleTimeoutReached(self): |
| 92 | + self._timeoutReached = True |
| 93 | + self._kill() |
| 94 | + |
| 95 | + def timeoutReached(self): |
| 96 | + return self._timeoutReached |
| 97 | + |
| 98 | + def _kill(self): |
| 99 | + """ |
| 100 | + This method may be called multiple times as we might get unlucky |
| 101 | + and be in the middle of creating a new process in _executeShCmd() |
| 102 | + which won't yet be in ``self._procs``. By locking here and in |
| 103 | + addProcess() we should be able to kill processes launched after |
| 104 | + the initial call to _kill() |
| 105 | + """ |
| 106 | + with self._lock: |
| 107 | + for p in self._procs: |
| 108 | + lit.util.killProcessAndChildren(p.pid) |
| 109 | + # Empty the list and note that we've done a pass over the list |
| 110 | + self._procs = [] # Python2 doesn't have list.clear() |
| 111 | + self._doneKillPass = True |
| 112 | + |
| 113 | +def executeShCmd(cmd, shenv, results, timeout=0): |
| 114 | + """ |
| 115 | + Wrapper around _executeShCmd that handles |
| 116 | + timeout |
| 117 | + """ |
| 118 | + # Use the helper even when no timeout is required to make |
| 119 | + # other code simpler (i.e. avoid bunch of ``!= None`` checks) |
| 120 | + timeoutHelper = TimeoutHelper(timeout) |
| 121 | + if timeout > 0: |
| 122 | + timeoutHelper.startTimer() |
| 123 | + finalExitCode = _executeShCmd(cmd, shenv, results, timeoutHelper) |
| 124 | + timeoutHelper.cancel() |
| 125 | + timeoutInfo = None |
| 126 | + if timeoutHelper.timeoutReached(): |
| 127 | + timeoutInfo = 'Reached timeout of {} seconds'.format(timeout) |
| 128 | + |
| 129 | + return (finalExitCode, timeoutInfo) |
| 130 | + |
| 131 | +def _executeShCmd(cmd, shenv, results, timeoutHelper): |
| 132 | + if timeoutHelper.timeoutReached(): |
| 133 | + # Prevent further recursion if the timeout has been hit |
| 134 | + # as we should try avoid launching more processes. |
| 135 | + return None |
| 136 | + |
37 | 137 | if isinstance(cmd, ShUtil.Seq):
|
38 | 138 | if cmd.op == ';':
|
39 |
| - res = executeShCmd(cmd.lhs, shenv, results) |
40 |
| - return executeShCmd(cmd.rhs, shenv, results) |
| 139 | + res = _executeShCmd(cmd.lhs, shenv, results, timeoutHelper) |
| 140 | + return _executeShCmd(cmd.rhs, shenv, results, timeoutHelper) |
41 | 141 |
|
42 | 142 | if cmd.op == '&':
|
43 | 143 | raise InternalShellError(cmd,"unsupported shell operator: '&'")
|
44 | 144 |
|
45 | 145 | if cmd.op == '||':
|
46 |
| - res = executeShCmd(cmd.lhs, shenv, results) |
| 146 | + res = _executeShCmd(cmd.lhs, shenv, results, timeoutHelper) |
47 | 147 | if res != 0:
|
48 |
| - res = executeShCmd(cmd.rhs, shenv, results) |
| 148 | + res = _executeShCmd(cmd.rhs, shenv, results, timeoutHelper) |
49 | 149 | return res
|
50 | 150 |
|
51 | 151 | if cmd.op == '&&':
|
52 |
| - res = executeShCmd(cmd.lhs, shenv, results) |
| 152 | + res = _executeShCmd(cmd.lhs, shenv, results, timeoutHelper) |
53 | 153 | if res is None:
|
54 | 154 | return res
|
55 | 155 |
|
56 | 156 | if res == 0:
|
57 |
| - res = executeShCmd(cmd.rhs, shenv, results) |
| 157 | + res = _executeShCmd(cmd.rhs, shenv, results, timeoutHelper) |
58 | 158 | return res
|
59 | 159 |
|
60 | 160 | raise ValueError('Unknown shell command: %r' % cmd.op)
|
@@ -206,6 +306,8 @@ def executeShCmd(cmd, shenv, results):
|
206 | 306 | stderr = stderr,
|
207 | 307 | env = cmd_shenv.env,
|
208 | 308 | close_fds = kUseCloseFDs))
|
| 309 | + # Let the helper know about this process |
| 310 | + timeoutHelper.addProcess(procs[-1]) |
209 | 311 | except OSError as e:
|
210 | 312 | raise InternalShellError(j, 'Could not create process ({}) due to {}'.format(executable, e))
|
211 | 313 |
|
@@ -271,7 +373,7 @@ def to_string(bytes):
|
271 | 373 | except:
|
272 | 374 | err = str(err)
|
273 | 375 |
|
274 |
| - results.append((cmd.commands[i], out, err, res)) |
| 376 | + results.append((cmd.commands[i], out, err, res, timeoutHelper.timeoutReached())) |
275 | 377 | if cmd.pipe_err:
|
276 | 378 | # Python treats the exit code as a signed char.
|
277 | 379 | if exitCode is None:
|
@@ -309,22 +411,25 @@ def executeScriptInternal(test, litConfig, tmpBase, commands, cwd):
|
309 | 411 | cmd = ShUtil.Seq(cmd, '&&', c)
|
310 | 412 |
|
311 | 413 | results = []
|
| 414 | + timeoutInfo = None |
312 | 415 | try:
|
313 | 416 | shenv = ShellEnvironment(cwd, test.config.environment)
|
314 |
| - exitCode = executeShCmd(cmd, shenv, results) |
| 417 | + exitCode, timeoutInfo = executeShCmd(cmd, shenv, results, timeout=litConfig.maxIndividualTestTime) |
315 | 418 | except InternalShellError:
|
316 | 419 | e = sys.exc_info()[1]
|
317 | 420 | exitCode = 127
|
318 |
| - results.append((e.command, '', e.message, exitCode)) |
| 421 | + results.append((e.command, '', e.message, exitCode, False)) |
319 | 422 |
|
320 | 423 | out = err = ''
|
321 |
| - for i,(cmd, cmd_out,cmd_err,res) in enumerate(results): |
| 424 | + for i,(cmd, cmd_out, cmd_err, res, timeoutReached) in enumerate(results): |
322 | 425 | out += 'Command %d: %s\n' % (i, ' '.join('"%s"' % s for s in cmd.args))
|
323 | 426 | out += 'Command %d Result: %r\n' % (i, res)
|
| 427 | + if litConfig.maxIndividualTestTime > 0: |
| 428 | + out += 'Command %d Reached Timeout: %s\n\n' % (i, str(timeoutReached)) |
324 | 429 | out += 'Command %d Output:\n%s\n\n' % (i, cmd_out)
|
325 | 430 | out += 'Command %d Stderr:\n%s\n\n' % (i, cmd_err)
|
326 | 431 |
|
327 |
| - return out, err, exitCode |
| 432 | + return out, err, exitCode, timeoutInfo |
328 | 433 |
|
329 | 434 | def executeScript(test, litConfig, tmpBase, commands, cwd):
|
330 | 435 | bashPath = litConfig.getBashPath();
|
@@ -359,8 +464,13 @@ def executeScript(test, litConfig, tmpBase, commands, cwd):
|
359 | 464 | # run on clang with no real loss.
|
360 | 465 | command = litConfig.valgrindArgs + command
|
361 | 466 |
|
362 |
| - return lit.util.executeCommand(command, cwd=cwd, |
363 |
| - env=test.config.environment) |
| 467 | + try: |
| 468 | + out, err, exitCode = lit.util.executeCommand(command, cwd=cwd, |
| 469 | + env=test.config.environment, |
| 470 | + timeout=litConfig.maxIndividualTestTime) |
| 471 | + return (out, err, exitCode, None) |
| 472 | + except lit.util.ExecuteCommandTimeoutException as e: |
| 473 | + return (e.out, e.err, e.exitCode, e.msg) |
364 | 474 |
|
365 | 475 | def parseIntegratedTestScriptCommands(source_path, keywords):
|
366 | 476 | """
|
@@ -573,16 +683,23 @@ def _runShTest(test, litConfig, useExternalSh, script, tmpBase):
|
573 | 683 | if isinstance(res, lit.Test.Result):
|
574 | 684 | return res
|
575 | 685 |
|
576 |
| - out,err,exitCode = res |
| 686 | + out,err,exitCode,timeoutInfo = res |
577 | 687 | if exitCode == 0:
|
578 | 688 | status = Test.PASS
|
579 | 689 | else:
|
580 |
| - status = Test.FAIL |
| 690 | + if timeoutInfo == None: |
| 691 | + status = Test.FAIL |
| 692 | + else: |
| 693 | + status = Test.TIMEOUT |
581 | 694 |
|
582 | 695 | # Form the output log.
|
583 |
| - output = """Script:\n--\n%s\n--\nExit Code: %d\n\n""" % ( |
| 696 | + output = """Script:\n--\n%s\n--\nExit Code: %d\n""" % ( |
584 | 697 | '\n'.join(script), exitCode)
|
585 | 698 |
|
| 699 | + if timeoutInfo != None: |
| 700 | + output += """Timeout: %s\n""" % (timeoutInfo,) |
| 701 | + output += "\n" |
| 702 | + |
586 | 703 | # Append the outputs, if present.
|
587 | 704 | if out:
|
588 | 705 | output += """Command Output (stdout):\n--\n%s\n--\n""" % (out,)
|
|
0 commit comments