| 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 | 10 import time |
| 11 import traceback | 11 import traceback |
| 12 | 12 |
| 13 import watchdog_timer | 13 import watchdog_timer |
| 14 | 14 |
| 15 | 15 |
| 16 class TimeoutError(Exception): | 16 class TimeoutError(Exception): |
| 17 """Module-specific timeout exception.""" | 17 """Module-specific timeout exception.""" |
| 18 pass | 18 pass |
| 19 | 19 |
| 20 | 20 |
| 21 class ReraiserThread(threading.Thread): | 21 class ReraiserThread(threading.Thread): |
| 22 """Thread class that can reraise exceptions.""" | 22 """Thread class that can reraise exceptions.""" |
| 23 | 23 |
| 24 def __init__(self, func, args=[], kwargs={}): | 24 def __init__(self, func, args=[], kwargs={}, name=None): |
| 25 super(ReraiserThread, self).__init__() | 25 """Initialize thread. |
| 26 |
| 27 Args: |
| 28 func: callable to call on a new thread. |
| 29 args: list of positional arguments for callable, defaults to empty. |
| 30 kwargs: dictionary of keyword arguments for callable, defaults to empty. |
| 31 name: thread name, defaults to Thread-N. |
| 32 """ |
| 33 super(ReraiserThread, self).__init__(name=name) |
| 26 self.daemon = True | 34 self.daemon = True |
| 27 self._func = func | 35 self._func = func |
| 28 self._args = args | 36 self._args = args |
| 29 self._kwargs = kwargs | 37 self._kwargs = kwargs |
| 30 self._exc_info = None | 38 self._exc_info = None |
| 31 | 39 |
| 32 def ReraiseIfException(self): | 40 def ReraiseIfException(self): |
| 33 """Reraise exception if an exception was raised in the thread.""" | 41 """Reraise exception if an exception was raised in the thread.""" |
| 34 if self._exc_info: | 42 if self._exc_info: |
| 35 raise self._exc_info[0], self._exc_info[1], self._exc_info[2] | 43 raise self._exc_info[0], self._exc_info[1], self._exc_info[2] |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 Reraises exceptions raised by the child threads and supports breaking | 105 Reraises exceptions raised by the child threads and supports breaking |
| 98 immediately on exceptions raised on the main thread. Unfinished threads' | 106 immediately on exceptions raised on the main thread. Unfinished threads' |
| 99 stacks will be logged on watchdog timeout. | 107 stacks will be logged on watchdog timeout. |
| 100 | 108 |
| 101 Args: | 109 Args: |
| 102 watcher: Watchdog object providing timeout, by default waits forever. | 110 watcher: Watchdog object providing timeout, by default waits forever. |
| 103 """ | 111 """ |
| 104 try: | 112 try: |
| 105 self._JoinAll(watcher) | 113 self._JoinAll(watcher) |
| 106 except TimeoutError: | 114 except TimeoutError: |
| 107 alive_thread_ids = (t.ident for t in self._threads if t.isAlive()) | 115 for thread in (t for t in self._threads if t.isAlive()): |
| 108 for thread_id in alive_thread_ids: | 116 stack = sys._current_frames()[thread.ident] |
| 109 stack = sys._current_frames()[thread_id] | |
| 110 logging.critical('*' * 80) | 117 logging.critical('*' * 80) |
| 111 logging.critical('Stack dump for timed out ThreadId = %s', thread_id) | 118 logging.critical('Stack dump for timed out thread \'%s\'', thread.name) |
| 112 logging.critical('*' * 80) | 119 logging.critical('*' * 80) |
| 113 for filename, lineno, name, line in traceback.extract_stack(stack): | 120 for filename, lineno, name, line in traceback.extract_stack(stack): |
| 114 logging.critical('File: "%s", line %d, in %s', filename, lineno, name) | 121 logging.critical('File: "%s", line %d, in %s', filename, lineno, name) |
| 115 if line: | 122 if line: |
| 116 logging.critical(' %s', line.strip()) | 123 logging.critical(' %s', line.strip()) |
| 117 logging.critical('*' * 80) | 124 logging.critical('*' * 80) |
| 118 raise | 125 raise |
| OLD | NEW |