| OLD | NEW |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """Thread and ThreadGroup that reraise exceptions on the main thread.""" | 5 """Thread and ThreadGroup that reraise exceptions on the main thread.""" |
| 6 | 6 |
| 7 import logging | 7 import logging |
| 8 import sys | 8 import sys |
| 9 import threading | 9 import threading |
| 10 import time | |
| 11 import traceback | 10 import traceback |
| 12 | 11 |
| 13 import watchdog_timer | 12 from . import watchdog_timer |
| 14 | 13 |
| 15 | 14 |
| 16 class TimeoutError(Exception): | 15 class TimeoutError(Exception): |
| 17 """Module-specific timeout exception.""" | 16 """Module-specific timeout exception.""" |
| 18 pass | 17 pass |
| 19 | 18 |
| 20 | 19 |
| 21 def LogThreadStack(thread): | 20 def LogThreadStack(thread): |
| 22 """Log the stack for the given thread. | 21 """Log the stack for the given thread. |
| 23 | 22 |
| 24 Args: | 23 Args: |
| 25 thread: a threading.Thread instance. | 24 thread: a threading.Thread instance. |
| 26 """ | 25 """ |
| 27 stack = sys._current_frames()[thread.ident] | 26 stack = sys._current_frames()[thread.ident] |
| 28 logging.critical('*' * 80) | 27 logging.critical('*' * 80) |
| 29 logging.critical('Stack dump for thread \'%s\'', thread.name) | 28 logging.critical('Stack dump for thread \'%s\'', thread.name) |
| 30 logging.critical('*' * 80) | 29 logging.critical('*' * 80) |
| 31 for filename, lineno, name, line in traceback.extract_stack(stack): | 30 for filename, lineno, name, line in traceback.extract_stack(stack): |
| 32 logging.critical('File: "%s", line %d, in %s', filename, lineno, name) | 31 logging.critical('File: "%s", line %d, in %s', filename, lineno, name) |
| 33 if line: | 32 if line: |
| 34 logging.critical(' %s', line.strip()) | 33 logging.critical(' %s', line.strip()) |
| 35 logging.critical('*' * 80) | 34 logging.critical('*' * 80) |
| 36 | 35 |
| 37 | 36 |
| 38 class ReraiserThread(threading.Thread): | 37 class ReraiserThread(threading.Thread): |
| 39 """Thread class that can reraise exceptions.""" | 38 """Thread class that can reraise exceptions.""" |
| 40 | 39 |
| 41 def __init__(self, func, args=[], kwargs={}, name=None): | 40 def __init__(self, func, args=None, kwargs=None, name=None): |
| 42 """Initialize thread. | 41 """Initialize thread. |
| 43 | 42 |
| 44 Args: | 43 Args: |
| 45 func: callable to call on a new thread. | 44 func: callable to call on a new thread. |
| 46 args: list of positional arguments for callable, defaults to empty. | 45 args: list of positional arguments for callable, defaults to empty. |
| 47 kwargs: dictionary of keyword arguments for callable, defaults to empty. | 46 kwargs: dictionary of keyword arguments for callable, defaults to empty. |
| 48 name: thread name, defaults to Thread-N. | 47 name: thread name, defaults to Thread-N. |
| 49 """ | 48 """ |
| 50 super(ReraiserThread, self).__init__(name=name) | 49 super(ReraiserThread, self).__init__(name=name) |
| 50 if not args: |
| 51 args = [] |
| 52 if not kwargs: |
| 53 kwargs = {} |
| 51 self.daemon = True | 54 self.daemon = True |
| 52 self._func = func | 55 self._func = func |
| 53 self._args = args | 56 self._args = args |
| 54 self._kwargs = kwargs | 57 self._kwargs = kwargs |
| 55 self._exc_info = None | 58 self._exc_info = None |
| 56 | 59 |
| 57 def ReraiseIfException(self): | 60 def ReraiseIfException(self): |
| 58 """Reraise exception if an exception was raised in the thread.""" | 61 """Reraise exception if an exception was raised in the thread.""" |
| 59 if self._exc_info: | 62 if self._exc_info: |
| 60 raise self._exc_info[0], self._exc_info[1], self._exc_info[2] | 63 raise self._exc_info[0], self._exc_info[1], self._exc_info[2] |
| 61 | 64 |
| 62 #override | 65 #override |
| 63 def run(self): | 66 def run(self): |
| 64 """Overrides Thread.run() to add support for reraising exceptions.""" | 67 """Overrides Thread.run() to add support for reraising exceptions.""" |
| 65 try: | 68 try: |
| 66 self._func(*self._args, **self._kwargs) | 69 self._func(*self._args, **self._kwargs) |
| 67 except: | 70 except: |
| 68 self._exc_info = sys.exc_info() | 71 self._exc_info = sys.exc_info() |
| 69 raise | 72 raise |
| 70 | 73 |
| 71 | 74 |
| 72 class ReraiserThreadGroup(object): | 75 class ReraiserThreadGroup(object): |
| 73 """A group of ReraiserThread objects.""" | 76 """A group of ReraiserThread objects.""" |
| 74 | 77 |
| 75 def __init__(self, threads=[]): | 78 def __init__(self, threads=None): |
| 76 """Initialize thread group. | 79 """Initialize thread group. |
| 77 | 80 |
| 78 Args: | 81 Args: |
| 79 threads: a list of ReraiserThread objects; defaults to empty. | 82 threads: a list of ReraiserThread objects; defaults to empty. |
| 80 """ | 83 """ |
| 84 if not threads: |
| 85 threads = [] |
| 81 self._threads = threads | 86 self._threads = threads |
| 82 | 87 |
| 83 def Add(self, thread): | 88 def Add(self, thread): |
| 84 """Add a thread to the group. | 89 """Add a thread to the group. |
| 85 | 90 |
| 86 Args: | 91 Args: |
| 87 thread: a ReraiserThread object. | 92 thread: a ReraiserThread object. |
| 88 """ | 93 """ |
| 89 self._threads.append(thread) | 94 self._threads.append(thread) |
| 90 | 95 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 | 130 |
| 126 Args: | 131 Args: |
| 127 watcher: Watchdog object providing timeout, by default waits forever. | 132 watcher: Watchdog object providing timeout, by default waits forever. |
| 128 """ | 133 """ |
| 129 try: | 134 try: |
| 130 self._JoinAll(watcher) | 135 self._JoinAll(watcher) |
| 131 except TimeoutError: | 136 except TimeoutError: |
| 132 for thread in (t for t in self._threads if t.isAlive()): | 137 for thread in (t for t in self._threads if t.isAlive()): |
| 133 LogThreadStack(thread) | 138 LogThreadStack(thread) |
| 134 raise | 139 raise |
| OLD | NEW |